使用 SGD 分类器和 GridsearchCV 查找顶级特征

# 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 和分类器是 SGDclassifier 与铰链损失


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_'.


Smart猫小萌
浏览 248回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python