# 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_'.
由于我是编程的新手,请帮助我解决您的问题。谢谢
答案 0 :(得分:0)
发生错误的原因是您使用的SGDClassifier没有feature_count_
属性(请检查docs中的可用属性):
from sklearn.linear_model import SGDClassifier
X = [[0., 0.], [1., 1.]]
y = [0, 1]
clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=5)
clf.fit(X, y)
clf.feature_count_
[...]
AttributeError: 'SGDClassifier' object has no attribute 'feature_count_'
最初,我认为问题在于您使用的是GridSearchCV对象,但事实并非如此,因为函数中的行class_labels = classifier.classes_
不会引发任何错误;尽管从文档来看,SGDClassifier似乎甚至没有classes_
属性,但实际上它确实具有:
clf.classes_
# array([0, 1])
我知道scikit-learn中唯一包含feature_count_
属性的分类器是BernoulliNB,MultinomialNB和ComplementNB,它们都是Naive Bayes家族的,尽管我不太确定它是否可以使用,因为您打算在这里使用它...