机器学习 --- 模型评估、选择与验证

机器学习的目的是使学到的模型不仅对已知数据而且对未知数据都能有很好的预测能力。不同的学习方法会训练出不同的模型,不同的模型可能会对未知数据作出不同的预测,然而我们手上并没有“未知”的数据。所以,如何评价模型好坏,并选择出好的模型是我们这个实训需要掌握的内容。

准确度的陷阱与混淆矩阵

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 *********#

精准率与召回率

import numpy as np

def precision_score(y_true, y_predict):
    '''
    计算精准率并返回
    :param y_true: 真实类别,类型为ndarray
    :param y_predict: 预测类别,类型为ndarray
    :return: 精准率,类型为float
    '''

    #********* Begin *********#
    def TP(y_true, y_predict):
        return np.sum((y_true ==1)&(y_predict == 1))
    def FP(y_true,y_predict):
        return np.sum((y_true ==0)&(y_predict==1))
    tp =TP(y_true, y_predict)
    fp =FP(y_true, y_predict)
    try:
        return tp /(tp+fp)
    except:
        return 0.0
    #********* End *********#


def recall_score(y_true, y_predict):
    '''
    计算召回率并召回
    :param y_true: 真实类别,类型为ndarray
    :param y_predict: 预测类别,类型为ndarray
    :return: 召回率,类型为float
    '''

    #********* Begin *********#
    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))
    fn =FN(y_true, y_predict)
    tp =TP(y_true, y_predict)
    try:
        return tp /(tp+fn)
    except:
        return 0.0

    
    #********* End *********#

F1 Score

import numpy as np

def f1_score(precision, recall):
    '''
    计算f1 score并返回
    :param precision: 模型的精准率,类型为float
    :param recall: 模型的召回率,类型为float
    :return: 模型的f1 score,类型为float
    '''

    #********* Begin *********#
    try:
        return 2*precision*recall / (precision+recall)
    except:
        return 0.0
    #********* End ***********#

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 *********#
    a= list(zip(prob,labels))
    rank =[values2 for values1,values2 in sorted(a, 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 *********#

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 *********#
    return accuracy_score(y_true, y_pred),precision_score(y_true, y_pred),recall_score(y_true, y_pred),f1_score(y_true, y_pred),roc_auc_score(y_true, y_prob)
    #********* End *********#

感谢大家的支持!!!!!记得关注哈!!!记得点赞!!!!!!

猜你喜欢

转载自blog.csdn.net/weixin_44196785/article/details/109511216
今日推荐