第1关:为什么要有训练集与测试集
第2关:欠拟合与过拟合
第3关:偏差与方差
第4关:验证集与交叉验证
第5关:衡量回归的性能指标
第6关:准确度的陷阱与混淆矩阵
import numpy as np
def confusion_matrix(y_true, y_predict):
'''
构建二分类的混淆矩阵,并将其返回
:param y_true: 真实类别,类型为ndarray
:param y_predict: 预测类别,类型为ndarray
:return: shape为(2, 2)的ndarray
'''
#********* Begin *********#
def TN(y_true, y_predict):
return np.sum((y_true == 0) & (y_predict== 0))
def FP(y_true, y_predict):
return np.sum((y_true == 0) & (y_predict == 1))
def FN(y_true, y_predict):
return np.sum((y_true == 1) & (y_predict == 0))
def TP(y_true, y_predict):
return np.sum((y_true == 1) & (y_predict == 1))
return np.array([
[TN(y_true, y_predict), FP(y_true, y_predict)],
[FN(y_true, y_predict), TP(y_true, y_predict)]
])
#********* End *********#
第7关:精准率与召回率
import numpy as np
def precision_score(y_true, y_predict):
'''
计算精准率并返回
:param y_true: 真实类别,类型为ndarray
:param y_predict: 预测类别,类型为ndarray
:return: 精准率,类型为float
'''
#********* Begin *********#
TN = np.sum((y_true == 0) & (y_predict == 0))
FP = np.sum((y_true == 0) & (y_predict == 1))
FN = np.sum((y_true == 1) & (y_predict == 0))
TP = np.sum((y_true == 1) & (y_predict == 1))
precision = TP/(TP + FP)
return precision
#********* End *********#
def recall_score(y_true, y_predict):
'''
计算召回率并召回
:param y_true: 真实类别,类型为ndarray
:param y_predict: 预测类别,类型为ndarray
:return: 召回率,类型为float
'''
#********* Begin *********#
TN = np.sum((y_true == 0) & (y_predict == 0))
FP = np.sum((y_true == 0) & (y_predict == 1))
FN = np.sum((y_true == 1) & (y_predict == 0))
TP = np.sum((y_true == 1) & (y_predict == 1))
recall = TP/(FN + TP)
return recall
#********* End *********#
第8关:F1 Score
import numpy as np
def f1_score(precision, recall):
'''
计算f1 score并返回
:param precision: 模型的精准率,类型为float
:param recall: 模型的召回率,类型为float
:return: 模型的f1 score,类型为float
'''
#********* Begin *********#
f1 = 2 * precision * recall / (precision + recall)
return f1
#********* End ***********#
第9关:ROC曲线与AUC
import numpy as np
def calAUC(prob, labels):
'''
计算AUC并返回
:param prob: 模型预测样本为Positive的概率列表,类型为ndarray
:param labels: 样本的真实类别列表,其中1表示Positive,0表示Negtive,类型为ndarray
:return: AUC,类型为float
'''
#********* Begin *********#
f=list(zip(prob, labels))
rank=[values2 for valuesl,values2 in sorted(f,key=lambda x:x[0])]
rankList=[i+1 for i in range(len(rank)) if rank[i]==1]
posNum=0
negNum=0
for i in range(len(labels)):
if(labels[i]==1):
posNum+=1
else:
negNum+=1
auc=(sum(rankList) -(posNum*(posNum+1))/2)/(posNum*negNum)
return auc
#********* End *********#
第10关:sklearn中的分类性能指标
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
def classification_performance(y_true, y_pred, y_prob):
'''
返回准确度、精准率、召回率、f1 Score和AUC
:param y_true:样本的真实类别,类型为`ndarray`
:param y_pred:模型预测出的类别,类型为`ndarray`
:param y_prob:模型预测样本为`Positive`的概率,类型为`ndarray`
:return:
'''
#********* Begin *********#
a=accuracy_score(y_true, y_pred)
b=precision_score(y_true,y_pred)
c=recall_score(y_true,y_pred)
d=f1_score(y_true,y_pred)
e=roc_auc_score(y_true,y_prob)
return (a,b,c,d,e)
#********* End *********#