信息使用Scikit-learn计算收益

Cha*_*eae 19 python machine-learning feature-selection scikit-learn text-classification

我正在使用Scikit-learn进行文本分类.我想针对(稀疏)文档 - 术语矩阵中的类计算每个属性的信息增益.信息增益定义为H(类) - H(类|属性),其中H是熵.

使用weka,可以使用InfoGainAttribute完成.但我还没有在scikit-learn中找到这个措施.

但是,有人建议上面的信息增益公式与互信息相同.这也与维基百科中的定义相匹配.

是否可以在scikit中使用特定设置来交互信息 - 学习完成此任务?

sgD*_*ion 21

你可以使用scikit-learn mutual_info_classif 这里的例子

from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_selection import mutual_info_classif
from sklearn.feature_extraction.text import CountVectorizer

categories = ['talk.religion.misc',
              'comp.graphics', 'sci.space']
newsgroups_train = fetch_20newsgroups(subset='train',
                                      categories=categories)

X, Y = newsgroups_train.data, newsgroups_train.target
cv = CountVectorizer(max_df=0.95, min_df=2,
                                     max_features=10000,
                                     stop_words='english')
X_vec = cv.fit_transform(X)

res = dict(zip(cv.get_feature_names(),
               mutual_info_classif(X_vec, Y, discrete_features=True)
               ))
print(res)
Run Code Online (Sandbox Code Playgroud)

这将输出每个属性的字典,即词汇表中的项目作为键,其信息作为值获得

这是输出的示例

{'bible': 0.072327479595571439,
 'christ': 0.057293733680219089,
 'christian': 0.12862867565281702,
 'christians': 0.068511328611810071,
 'file': 0.048056478042481157,
 'god': 0.12252523919766867,
 'gov': 0.053547274485785577,
 'graphics': 0.13044709565039875,
 'jesus': 0.09245436105573257,
 'launch': 0.059882179387444862,
 'moon': 0.064977781072557236,
 'morality': 0.050235104394123153,
 'nasa': 0.11146392824624819,
 'orbit': 0.087254803670582998,
 'people': 0.068118370234354936,
 'prb': 0.049176995204404481,
 'religion': 0.067695617096125316,
 'shuttle': 0.053440976618359261,
 'space': 0.20115901737978983,
 'thanks': 0.060202010019767334}
Run Code Online (Sandbox Code Playgroud)

  • 这是信息增益还是互信息增益?他们一样吗?@sgDysregulation (3认同)