【问题标题】:How to find top-k accuracy for SVM and Naive Bayes如何找到 SVM 和朴素贝叶斯的 top-k 准确度
【发布时间】:2021-09-16 14:35:08
【问题描述】:

我的问题是一个有 50 个类的多类分类问题。我正在尝试找到 SVM 和 NB 算法的 top-k 分类准确度。

X_train, X_test, y_train, y_test = train_test_split(sentences, labels, test_size=0.3, random_state = 42)

nb = Pipeline([('vect', CountVectorizer(min_df=1, dtype=np.int32, vocabulary= vocab_data, ngram_range=(1, 2))),
           ('tfidf', TfidfTransformer(use_idf=False)),
           ('chi',  SelectKBest(chi2, k='all')),
           ('clf', OneVsRestClassifier(MultinomialNB(alpha=0.001))),
          ])

y_pred = nb.predict(X_test)

print('accuracy %s' % accuracy_score(y_pred, y_test))

我能够找到准确率精度和召回值。有没有办法找到 top k 准确率?

【问题讨论】:

  • 你试过sklearn.metrics.top_k_accuracy_score吗?

标签: python scikit-learn svm naivebayes


【解决方案1】:
probs = nb.predict_proba(X_test)
print('accuracy %s' % top_k_accuracy_score(y_test, probs, k=5))

这会打印前 k 个准确度

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 2016-08-12
    • 2020-01-18
    • 2020-01-13
    • 2012-01-30
    • 2011-10-03
    • 2013-04-13
    • 2012-02-21
    • 2011-12-28
    相关资源
    最近更新 更多