ROC曲线

下面是精准率和召回率

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

def TN(y_true, y_predict):
    assert len(y_true) == len(y_predict)
    return np.sum((y_true == 0) & (y_predict == 0))

def FP(y_true, y_predict):
    assert len(y_true) == len(y_predict)
    return np.sum((y_true == 0) & (y_predict == 1))

def FN(y_true,y_predict):
    assert len(y_true) == len(y_predict)
    return np.sum((y_true == 1) & (y_predict == 0))

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
    
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.0

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.0

自己动手实现:

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

digits = datasets.load_digits()
X = digits.data
y = digits.target.copy()
y[digits.target == 9] = 1
y[digits.target != 9] = 0
X_train, X_test,y_train, y_test = train_test_split(X,y,random_state=666)

from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X_train,y_train)
decision_score = log_reg.decision_function(X_test)

fprs= []
tprs = []
threshholds = np.arange(np.min(decision_score),np.max(decision_score),0.1)
for threshhold in threshholds:
    y_predict = np.array(decision_score >= threshhold, dtype='int')
    fprs.append(FPR(y_test, y_predict))
    tprs.append(TPR(y_test, y_predict))
plt.plot(fprs, tprs)
plt.show()

在这里插入图片描述
下面用sklearn中的ROC

from sklearn.metrics import roc_curve
fprs, tprs ,threshholds = roc_curve(y_test, decision_score)
plt.plot(fprs, tprs)
plt.show()

在这里插入图片描述
上面的曲线下面的面积越大,模型的准确度越好,下面是求曲线下面的面积:

from sklearn.metrics import roc_auc_score
roc_auc_score(y_test,decision_score)

多分类问题的混淆矩阵

digits = datasets.load_digits()
X = digits.data
y = digits.target.copy()
X_train, X_test,y_train, y_test = train_test_split(X,y,random_state=666)
log_reg = LogisticRegression()
log_reg.fit(X_train,y_train)
y_predict = log_reg.predict(X_test)


from sklearn.metrics import precision_score
precision_score(y_test, y_predict,average="micro")   # 0.9555555555555556
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_predict)

cfm = confusion_matrix(y_test, y_predict)
plt.matshow(cfm, cmap=plt.cm.gray)
plt.show()

在这里插入图片描述
错误矩阵:

row_sums = np.sum(cfm, axis=1)
err_matrix = cfm/row_sums
np.fill_diagonal(err_matrix,0)
err_matrix

plt.matshow(err_matrix, cmap=plt.cm.gray)
plt.show()

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值