import pandas as pd
import numpy as np
y_true = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1])
y_pred = np.array([0, 0, 1, 1, 1, 1, 1, 0, 0, 0])
# 计算模型得到的正确结果
true_positives = ((y_pred == y_true) & (y_pred == 1)).sum()
print(true_positives)
true_negatives = ((y_pred == y_true) & (y_pred == 0)).sum()
print(true_negatives)
# 计算模型得到的错误结果
false_positives = ((y_pred != y_true) & (y_pred == 1)).sum()
print(false_positives)
false_negatives = ((y_pred != y_true) & (y_pred == 0)).sum()
print(false_negatives)
# 误差矩阵或混淆矩阵
confusion = [[true_positives, false_positives],
[false_negatives, true_negatives]]
print(confusion)
confusion = pd.DataFrame(confusion, columns=[1, 0], index=[1, 0])
confusion.index.name = r'pred \ truth'
print(confusion)
# 正确率
precision = true_positives / (true_positives + false_positives)
print(precision)
# 召回率也被称为灵敏度、真阳率或查全率
recall = true_positives / (true_positives + false_negatives)
print(recall)