机器学习——评价标准
文章目录
-
混淆矩阵
-
- precision recall
- F1 Score
- Precision-Recall 的 平衡
-
AUC . ROC
-
- AUC (Area Under Curve)
- ROC(receiver operating characteristic curve )
- TPR FPR
-
- ROC曲线的绘制
-
sklearn 调用
-
手打python实现
问题 :在存在极度偏斜的数据中,应用分类准确度 来评价分类算法的好坏是远远不够的。
eg:如果癌症的产生概率只有0.1%,那么系统只要预测所有人都是健康的就可以达到99.9%的准确率,因此虽然准确率很高,但是预测系统实际上没有发挥什么作用。
混淆矩阵

precision recall
召回率(recall):
recall=\frac{T P}{T P+F N}
精准率(precision):
precision=\frac{TP}{T P+F P}

F1 Score
\frac{1}{F 1}=\frac{1}{2}\left(\frac{1}{\text {precision}}+\frac{1}{\text {recall}}\right)
F 1=\frac{2 \cdot \text { precision } \cdot \text { recall }}{\text { precision }+\text { recall }}
F1 Score是presion和recall的调和平均值
Precision-Recall 的 平衡
精准率常用于:股票预测
召回率常用与:病人诊断
精准率与召回率是相反的,一个变大,另一个就会变小,所以我们要根据使用的场景,找一个合适的值。
决策边界:
\theta^{T} \cdot x_{b}=t h r e s h o l d

AUC . ROC
AUC (Area Under Curve)
AUC: ROC曲线下的面积
ROC(receiver operating characteristic curve )
TPR FPR
TPR(=recall召回率):
T P R=\frac{T P}{T P+F N}
FPR :
F P R=\frac{F P}{T N+F P}
公式中,TP+FN 就是所有样本中正样本的数量,我们将所有的正样本记为P ,TN+FP 记为负样本 ,将其记为N,假设当前的概率划分阈值是0.5,TP代表所有样本预测概率大于0.5的样本中实际是正样本的数量,FP代表预测概率大于0.5的样本中实际是负样本的数量 ,而TP+FP就是模型给出的预测结果中预测概率大于0.5的总和。


TPR 与 FPR 趋势相同
ROC曲线的绘制
ROC曲线的X轴是假阳性率(FP),Y轴为真阳性率(TP)。
我们在使用一个二分类算法做预测时,一般的是会给出样本属于正样本的概率Pi 。
然后,我们会选定一个阈值P0,当Pi >=P0时,预测为正样本;
当Pi至此,选定一个算法A,每选定一个阈值P0,都可以算出FP/TP,也就对应了ROC曲线上的一个点。
ROC曲线其实有三个要素:算法A,测试数据D,阈值集合P 。
它评价了一个算法A,在测试数据D上,选取不同的阈值P时,假阳性率和真阳性率的表现。

sklearn 调用

# 混淆矩阵
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_log_predict)
# 精准率
from sklearn.metrics import precision_score
precision_score(y_test, y_log_predict)
# 召回率
from sklearn.metrics import recall_score
recall_score(y_test, y_log_predict)
# F1 Score
from sklearn.metrics import f1_score
f1_score(y_test, y_predict)
# 绘制precision-recall曲线
from sklearn.metrics import precision_recall_curve
precisions, recalls, thresholds = precision_recall_curve(y_test, decision_scores)
# 绘制ROC曲线
from sklearn.metrics import roc_curve
fprs, tprs, thresholds = roc_curve(y_test, decision_scores)
plt.plot(fprs, tprs)
plt.show()
# roc下方面积auc
from sklearn.metrics import roc_auc_score
roc_auc_score(y_test, decision_scores)
手打python实现
# TN 预测为假 且预测正确(真实为假)
def TN(y_true, y_predict):
assert len(y_true) == len(y_predict)
return np.sum((y_true == 0) & (y_predict == 0)) # 都为真
# FP 预测为真 且预测错误(真实为假)
def FP(y_true, y_predict):
assert len(y_true) == len(y_predict)
return np.sum((y_true == 0) & (y_predict == 1))
# FN 预测为假 且预测错误(真实为真)
def FN(y_true, y_predict):
assert len(y_true) == len(y_predict)
return np.sum((y_true == 1) & (y_predict == 0))
# TP 预测为真 且预测正确(真实为真)
def TP(y_true, y_predict):
assert len(y_true) == len(y_predict)
return np.sum((y_true == 1) & (y_predict == 1))
# 混淆矩阵
def confusion_matrix(y_true, y_predict):
return np.array([
[TN(y_true, y_predict), FP(y_true, y_predict)],
[FN(y_true, y_predict), TP(y_true, y_predict)]
])
# 精确率
def precision_score(y_true, y_predict):
tp = TP(y_true, y_predict)
fp = FP(y_true, y_predict)
try:
return tp / (tp + fp)
except:
return 0.0
# 召回率
def recall_score(y_true, y_predict):
tp = TP(y_true, y_predict)
fn = FN(y_true, y_predict)
try:
return tp / (tp + fn)
except:
return 0.0
# F1 Score
def f1_score(precision, recall):
try:
return 2 * precision * recall / (precision + recall)
except:
return 0.0
# 绘制precision-recall曲线
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
precisions = []
recalls = []
thresholds = np.arange(np.min(decision_scores), np.max(decision_scores), 0.1)
for threshold in thresholds:
y_predict = np.array(decision_scores >= threshold, dtype='int')
precisions.append(precision_score(y_test, y_predict))
recalls.append(recall_score(y_test, y_predict))
# TPR
def TPR(y_true, y_predict):
tp = TP(y_true, y_predict)
fn = FN(y_true, y_predict)
try:
return tp / (tp + fn)
except:
return 0.
# FPR
def FPR(y_true, y_predict):
fp = FP(y_true, y_predict)
tn = TN(y_true, y_predict)
try:
return fp / (fp + tn)
except:
return 0.
# 绘制ROC曲线
from playML.metrics import FPR, TPR
fprs = []
tprs = []
thresholds = np.arange(np.min(decision_scores), np.max(decision_scores), 0.1)
for threshold in thresholds:
y_predict = np.array(decision_scores >= threshold, dtype='int')
fprs.append(FPR(y_test, y_predict))
tprs.append(TPR(y_test, y_predict))
