对于二分类问题,分类器在测试集上的预测或者正确或者不正确,令
真值情况 | 预测结果 | |
---|---|---|
正类 | 反类 | |
正类 | TP | FN |
反类 | FP | TN |
2.1原型
sklearn.metrics.confusion_matrix(y_true,y_pred,labels=None)
2.2参数
2.3示例代码
from sklearn.metrics import confusion_matrix
y_true = [1,1,1,1,1,0,0,0,0,0]
y_pred = [0,0,1,1,0,0,0,0,0,0]
print("Confusion Matrix:\n",confusion_matrix(y_true,y_pred))
Confusion Matrix:
[[5 0]
[3 2]]
多分类仍然可以使用混淆矩阵
from sklearn.metrics import confusion_matrix
y_true = [1,2,2,1,1,0,1,1,0,2]
y_pred = [1,2,1,1,1,0,2,1,0,1]
print("Confusion Matrix:\n",confusion_matrix(y_true,y_pred))
Confusion Matrix:
[[2 0 0]
[0 4 1]
[0 2 1]]
3.1原型
3.2参数
3.3示例代码
from sklearn.metrics import accuracy_score,precision_score
y_true = [1,1,1,1,1,0,0,0,0,0]
y_pred = [0,0,1,1,0,0,0,0,0,0]
print("Accuracy Score:",accuracy_score(y_true,y_pred,normalize=True))
print("Precision Score",precision_score(y_true,y_pred))
Accuracy Score: 0.7
Precision Score 1.0
3.1原型
3.2参数
3.3示例代码
from sklearn.metrics import accuracy_score,precision_score,recall_score
y_true = [1,1,1,1,1,0,0,0,0,0]
y_pred = [0,0,1,1,0,0,0,0,0,0]
print("Accuracy Score:",accuracy_score(y_true,y_pred,normalize=True))
print("Precision Score",precision_score(y_true,y_pred))
print("Recall Score",recall_score(y_true,y_pred))
Accuracy Score: 0.7
Precision Score 1.0
Recall Score 0.4