如何计算OneVsRestClassifier的预测准确性?

时间:2019-09-05 09:34:52

标签: python machine-learning scikit-learn multilabel-classification

如何计算OneVsRestClassifier的预测准确性和F1得分?

>>> from sklearn import datasets
>>> from sklearn.multiclass import OneVsRestClassifier
>>> from sklearn.svm import LinearSVC
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> OneVsRestClassifier(LinearSVC(random_state=0)).fit(X, y).predict(X)

1 个答案:

答案 0 :(得分:0)

您可以使用sklearn的指标模块。

from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score, f1_score

iris = datasets.load_iris()
X, y = iris.data, iris.target
model = OneVsRestClassifier(LinearSVC(random_state=0))
model.fit(X, y)
yhat = model.predict(X)

print('Accuracy:', accuracy_score(y, yhat))
print('F1:', f1_score(y, yhat, average='micro'))

请注意,我将average参数设置为micro。您可以根据选项here进行更改。