3 回答

TA貢獻1780條經驗 獲得超5個贊
分類器本身不記錄要素名稱,它們僅顯示數字數組。但是,如果您使用Vectorizer/ CountVectorizer/ TfidfVectorizer/ 提取了特征DictVectorizer,并且使用的是線性模型(例如LinearSVCNaive Bayes或Naive Bayes),則可以應用文檔分類示例所使用的技巧。示例(未經測試,可能包含一個或兩個錯誤):
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。

TA貢獻1951條經驗 獲得超3個贊
在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)

TA貢獻1833條經驗 獲得超4個贊
實際上,我必須在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_屬性才能起作用。
添加回答
舉報