如何使用SVM的线性svc获得精度和召回率?

时间:2018-09-07 05:02:28

标签: python scikit-learn

我已经使用SVM的Linear svc训练和测试数据。我可以在数据集中获得SVM的准确性。但是,除了准确性外,我还需要准确性和召回率。谁能建议我如何计算精度和召回率。

我的代码:

from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
with open("/Users/abc/Desktop/reviews.txt") as f:
    reviews = f.read().split("\n")
with open("/Users/abc/Desktop/labels.txt") as f:
    labels = f.read().split("\n")

reviews_tokens = [review.split() for review in reviews]


onehot_enc = MultiLabelBinarizer()
onehot_enc.fit(reviews_tokens)


X_train, X_test, y_train, y_test = train_test_split(reviews_tokens, labels, test_size=0.20, random_state=None)

lsvm = LinearSVC()
lsvm.fit(onehot_enc.transform(X_train), y_train)
score = lsvm.score(onehot_enc.transform(X_test), y_test)
print("Score of SVM:" , score)

1 个答案:

答案 0 :(得分:0)

您可以这样做:

from sklearn.metrics import confusion_matrix

predicted_y = lsvm.predict(X_test)
tn, fp, fn, tp = confusion_matrix(y_test, predicted_y).ravel()
precision_score = tp / (tp + fp)
recall_score = tp / (tp + fn)

有关更多信息,请参考confusion_matrix文档