【发布时间】:2015-07-04 05:33:00
【问题描述】:
由于我的分类器在测试数据上产生了大约 99% 的准确率,我有点怀疑,并想深入了解我的 NB 分类器中信息量最大的特征,看看它正在学习什么样的特征。以下主题非常有用:How to get most informative features for scikit-learn classifiers?
至于我的特征输入,我还在玩,目前我正在使用 CountVectorizer 测试一个简单的一元模型:
vectorizer = CountVectorizer(ngram_range=(1, 1), min_df=2, stop_words='english')
关于上述主题,我发现了以下功能:
def show_most_informative_features(vectorizer, clf, n=20):
feature_names = vectorizer.get_feature_names()
coefs_with_fns = sorted(zip(clf.coef_[0], feature_names))
top = zip(coefs_with_fns[:n], coefs_with_fns[:-(n + 1):-1])
for (coef_1, fn_1), (coef_2, fn_2) in top:
print "\t%.4f\t%-15s\t\t%.4f\t%-15s" % (coef_1, fn_1, coef_2, fn_2)
结果如下:
-16.2420 114th -4.0020 said
-16.2420 115 -4.6937 obama
-16.2420 136 -4.8614 house
-16.2420 14th -5.0194 president
-16.2420 15th -5.1236 state
-16.2420 1600 -5.1370 senate
-16.2420 16th -5.3868 new
-16.2420 1920 -5.4004 republicans
-16.2420 1961 -5.4262 republican
-16.2420 1981 -5.5637 democrats
-16.2420 19th -5.6182 congress
-16.2420 1st -5.7314 committee
-16.2420 31st -5.7732 white
-16.2420 3rd -5.8227 security
-16.2420 4th -5.8256 states
-16.2420 5s -5.8530 year
-16.2420 61 -5.9099 government
-16.2420 900 -5.9464 time
-16.2420 911 -5.9984 department
-16.2420 97 -6.0273 gop
它有效,但我想知道这个函数做了什么来解释结果。大多数情况下,我对“coef_”属性的作用感到困惑。
我知道左侧是系数最低的前 20 个特征名称,右侧是系数最高的特征名称。但是这究竟是如何工作的,我如何解释这个概述?这是否意味着左侧包含负类信息最多的特征,而右侧包含正类信息最多的特征?
另外,在左侧看起来好像功能名称是按字母顺序排序的,这样对吗?
【问题讨论】:
标签: python machine-learning scikit-learn classification text-classification