【问题标题】:shape of input to calculate information gain计算信息增益的输入形状
【发布时间】:2019-04-29 18:12:48
【问题描述】:

我想在20_newsgroup 数据集上计算information gain。

我正在使用代码here(我也将代码的副本放在问题的下方)。

如您所见,算法的输入是X,y 我的困惑是,X 将成为matrix,documents 在行中,features as column。 (根据 20_newsgroup 是 11314,1000 如果我只考虑了 1000 个特征)。

但是根据信息增益的概念,应该为each feature.计算信息增益

(所以我希望看到代码以循环遍历每个特征的方式,所以函数的输入是一个矩阵,其中行是特征,列是类)

但是X在这里不是特征,而是X代表文档,我看不到代码中处理这部分的部分! (我的意思是考虑每个文档,然后遍历该文档的每个特征;就像遍历行但同时遍历列,因为特征存储在列中)。

我已经阅读了this 和this 以及许多类似的问题,但它们在输入矩阵形状方面并不清楚。

这是阅读20_newsgroup的代码:

newsgroup_train = fetch_20newsgroups(subset='train')
X,y = newsgroup_train.data,newsgroup_train.target

cv = CountVectorizer(max_df=0.99,min_df=0.001, max_features=1000,stop_words='english',lowercase=True,analyzer='word')
X_vec = cv.fit_transform(X)

(X_vec.shape) 是 (11314,1000),它不是 20_newsgroup 数据集中的特征。我在想我是否以不正确的方式计算信息增益?

这是Information gain的代码:

def information_gain(X, y):

    def _calIg():
        entropy_x_set = 0
        entropy_x_not_set = 0
        for c in classCnt:
            probs = classCnt[c] / float(featureTot)
            entropy_x_set = entropy_x_set - probs * np.log(probs)
            probs = (classTotCnt[c] - classCnt[c]) / float(tot - featureTot)
            entropy_x_not_set = entropy_x_not_set - probs * np.log(probs)
        for c in classTotCnt:
            if c not in classCnt:
                probs = classTotCnt[c] / float(tot - featureTot)
                entropy_x_not_set = entropy_x_not_set - probs * np.log(probs)
        return entropy_before - ((featureTot / float(tot)) * entropy_x_set
                             +  ((tot - featureTot) / float(tot)) * entropy_x_not_set)

    tot = X.shape[0]
    classTotCnt = {}
    entropy_before = 0
    for i in y:
        if i not in classTotCnt:
            classTotCnt[i] = 1
        else:
            classTotCnt[i] = classTotCnt[i] + 1
    for c in classTotCnt:
        probs = classTotCnt[c] / float(tot)
        entropy_before = entropy_before - probs * np.log(probs)

    nz = X.T.nonzero()
    pre = 0
    classCnt = {}
    featureTot = 0
    information_gain = []
    for i in range(0, len(nz[0])):
        if (i != 0 and nz[0][i] != pre):
            for notappear in range(pre+1, nz[0][i]):
                information_gain.append(0)
            ig = _calIg()
            information_gain.append(ig)
            pre = nz[0][i]
            classCnt = {}
            featureTot = 0
        featureTot = featureTot + 1
        yclass = y[nz[1][i]]
        if yclass not in classCnt:
            classCnt[yclass] = 1
        else:
            classCnt[yclass] = classCnt[yclass] + 1
    ig = _calIg()
    information_gain.append(ig)

    return np.asarray(information_gain)

【问题讨论】:

    标签: machine-learning scikit-learn feature-extraction feature-selection entropy


    【解决方案1】:

    嗯,在详细浏览了代码之后,我对X.T.nonzero()有了更多的了解。

    其实信息增益需要循环特征是正确的。 同样正确的是,scikit-learn 在这里给我们的矩阵是基于doc-features。

    但是:

    在代码中它使用X.T.nonzero() 从技术上将所有非零值转换为数组。然后在下一行循环遍历该数组范围的长度(0,len(X.T.nonzero()[0]).

    总的来说,这部分 X.T.nonzero()[0] 将所有非零特征返回给我们:)

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2018-03-26
      • 2015-07-28
      • 2010-12-27
      • 2011-07-24
      • 2017-06-27
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      相关资源
      最近更新 更多