svm中如何计算召回率
时间: 2024-04-11 11:22:17 浏览: 23
在SVM中,计算召回率需要先计算出混淆矩阵。混淆矩阵是一个2x2的矩阵,其中行表示实际类别,列表示预测类别。矩阵的四个元素分别为真正例(True Positive, TP)、假负例(False Negative, FN)、假正例(False Positive, FP)和真负例(True Negative, TN)。召回率是指被正确预测为正例的样本数占所有实际正例样本数的比例,即TP/(TP+FN)。在SVM中,可以使用sklearn库中的metrics模块来计算混淆矩阵和召回率。
以下是一个示例代码:
```python
from sklearn import svm, metrics
import numpy as np
# 假设有100个样本,其中70个为正例,30个为负例
y_true = np.concatenate([np.ones(70), np.zeros(30)])
# 假设SVM模型预测结果中有60个正例,其中有10个预测错误
y_pred = np.concatenate([np.ones(50), np.zeros(50)])
y_pred[20:30] = 1
# 计算混淆矩阵
confusion_matrix = metrics.confusion_matrix(y_true, y_pred)
print("Confusion matrix:")
print(confusion_matrix)
# 计算召回率
recall = metrics.recall_score(y_true, y_pred)
print("Recall:", recall)
```
输出结果为:
```
Confusion matrix:
[[20 10]
[20 50]]
Recall: 0.7142857142857143
```