【发布时间】:2019-03-09 17:20:41
【问题描述】:
# Implementing Linear_SGD classifier
clf = linear_model.SGDClassifier(max_iter=1000)
Cs = [0.0001,0.001, 0.01, 0.1, 1, 10]
tuned_parameters = [{'alpha': Cs}]
model = GridSearchCV(clf, tuned_parameters, scoring = 'accuracy', cv=2)
model.fit(x_train, Y_train)
如何从以下代码中找到最重要的功能,因为它显示错误 feature_count_。
这里我的矢量化器是 BOW,分类器是带有铰链损失的 SGD 分类器
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")
我尝试使用上面的代码,但它显示错误为
AttributeError Traceback (most recent call last)
<ipython-input-77-093048fb461e> in <module>()
----> 1 important_features(Timesort_X_vec,model)
<ipython-input-75-10b9d6ee3f81> in important_features(vectorizer,
classifier, n)
2 class_labels = classifier.classes_
3 feature_names =vectorizer.get_feature_names()
----> 4 topn_class1 = sorted(zip(classifier.feature_count_[0],
feature_names),reverse=True)[:n]
5 topn_class2 = sorted(zip(classifier.feature_count_[1],
feature_names),reverse=True)[:n]
6 print("Important words in negative reviews")
AttributeError: 'GridSearchCV' object has no attribute 'feature_count_'.
由于我是编程新手,请帮我解答。谢谢你
【问题讨论】:
标签: python machine-learning scikit-learn grid-search