【问题标题】:How to get precision and recall using Linear svc of SVM?如何使用 SVM 的线性 svc 获得精度和召回率?
【发布时间】:2019-02-12 09:33:03
【问题描述】:

我使用 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)

【问题讨论】:

标签: python scikit-learn


【解决方案1】:

你可以这样做:

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 文档了解更多信息

【讨论】:

  • 这里,“train_test_split”将训练数据和测试数据分开。取而代之的是,我们可以为算法提供单独的训练和测试数据文件吗?
  • train_test_split 根据您提供的参数将给定数据拆分为训练和测试。我们使用训练数据训练模型(以学习权重/系数)并使用测试数据测量性能。我们使用交叉验证数据进行超参数调整
  • 嗨@Kaslsi,你能看看这个网址吗:stackoverflow.com/questions/52250852/…
猜你喜欢
  • 2021-09-01
  • 2015-12-05
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 2018-01-20
相关资源
最近更新 更多