[英]How to get most informative features for scikit-learn classifier for different class?
[英]How to get most informative features for scikit-learn classifiers?
liblinear 和 nltk 等机器学习包中的分类器提供了一种方法show_most_informative_features()
,这对于调试功能非常有帮助:
viagra = None ok : spam = 4.5 : 1.0
hello = True ok : spam = 4.5 : 1.0
hello = None spam : ok = 3.3 : 1.0
viagra = True spam : ok = 3.3 : 1.0
casino = True spam : ok = 2.0 : 1.0
casino = None ok : spam = 1.5 : 1.0
我的问题是是否为 scikit-learn 中的分类器实现了类似的功能。 我搜索了文档,但找不到类似的东西。
如果还没有这样的功能,有人知道如何获得这些值的解决方法吗?
分类器本身不记录特征名称,它们只看到数字数组。 但是,如果您使用Vectorizer
/ CountVectorizer
/ TfidfVectorizer
/ DictVectorizer
提取特征,并且您使用的是线性模型(例如LinearSVC
或朴素贝叶斯),那么您可以应用文档分类示例使用的相同技巧。 示例(未经测试,可能包含一两个错误):
def print_top10(vectorizer, clf, class_labels):
"""Prints features with the highest coefficient values, per class"""
feature_names = vectorizer.get_feature_names()
for i, class_label in enumerate(class_labels):
top10 = np.argsort(clf.coef_[i])[-10:]
print("%s: %s" % (class_label,
" ".join(feature_names[j] for j in top10)))
这是用于多类分类; 对于二进制情况,我认为您应该只使用clf.coef_[0]
。 您可能需要对class_labels
进行排序。
在 larsmans 代码的帮助下,我想出了这个二进制案例的代码:
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)
为了添加更新, RandomForestClassifier
现在支持.feature_importances_
属性。 该属性告诉您该特征解释了多少观察到的方差。 显然,所有这些值的总和必须 <= 1。
我发现这个属性在执行特征工程时非常有用。
感谢 scikit-learn 团队和贡献者实现这一点!
编辑:这适用于 RandomForest 和 GradientBoosting。 所以RandomForestClassifier
、 RandomForestRegressor
、 GradientBoostingClassifier
和GradientBoostingRegressor
都支持这个。
我们最近发布了一个库( https://github.com/TeamHG-Memex/eli5 ),它允许这样做:它处理来自 scikit-learn、二进制/多类情况的各种分类器,允许根据特征值突出显示文本, 与 IPython 等集成。
我实际上必须在我的 NaiveBayes 分类器上找出特征重要性,虽然我使用了上述函数,但我无法根据类获得特征重要性。 我浏览了 scikit-learn 的文档并稍微调整了上述功能以发现它可以解决我的问题。 希望对你也有帮助!
def important_features(vectorizer,classifier,n=20):
class_labels = classifier.classes_
feature_names =vectorizer.get_feature_names()
topn_class1 = sorted(zip(classifier.feature_count_[0], feature_names),reverse=True)[:n]
topn_class2 = sorted(zip(classifier.feature_count_[1], feature_names),reverse=True)[:n]
print("Important words in negative reviews")
for coef, feat in topn_class1:
print(class_labels[0], coef, feat)
print("-----------------------------------------")
print("Important words in positive reviews")
for coef, feat in topn_class2:
print(class_labels[1], coef, feat)
请注意,您的分类器(在我的情况下是 NaiveBayes)必须具有属性 feature_count_ 才能工作。
您还可以执行以下操作以按顺序创建重要性特征图:
importances = clf.feature_importances_
std = np.std([tree.feature_importances_ for tree in clf.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
# Print the feature ranking
#print("Feature ranking:")
# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(train[features].shape[1]), importances[indices],
color="r", yerr=std[indices], align="center")
plt.xticks(range(train[features].shape[1]), indices)
plt.xlim([-1, train[features].shape[1]])
plt.show()
RandomForestClassifier
还没有coef_
属性,但我认为它会在 0.17 版本中出现。 但是,请参阅使用 scikit-learn 对随机森林进行递归特征消除中的RandomForestClassifierWithCoef
类。 这可能会给您一些解决上述限制的想法。
不完全是您要查找的内容,而是获取最大幅度系数的快速方法(假设 Pandas 数据框列是您的特征名称):
您训练模型如下:
lr = LinearRegression()
X_train, X_test, y_train, y_test = train_test_split(df, Y, test_size=0.25)
lr.fit(X_train, y_train)
获取 10 个最大的负系数值(或更改为 reverse=True 以获取最大正值),例如:
sorted(list(zip(feature_df.columns, lr.coef_)), key=lambda x: x[1],
reverse=False)[:10]
首先列一个列表,我给这个列表命名标签。 之后提取所有功能名称和列名称,我添加到标签列表中。 这里我使用朴素贝叶斯模型。 在朴素贝叶斯模型中,feature_log_prob_ 给出特征的概率。
def top20(model,label):
feature_prob=(abs(model.feature_log_prob_))
for i in range(len(feature_prob)):
print ('top 20 features for {} class'.format(i))
clas = feature_prob[i,:]
dictonary={}
for count,ele in enumerate(clas,0):
dictonary[count]=ele
dictonary=dict(sorted(dictonary.items(), key=lambda x: x[1], reverse=True)[:20])
keys=list(dictonary.keys())
for i in keys:
print(label[i])
print('*'*1000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.