标签:scores spl 学习 fun pos near target ima .data
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.
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.
import numpy as np from sklearn import datasets digits = datasets.load_digits() X = digits.data y = digits.target.copy() y[digits.target==9] = 1 y[digits.target!=9] = 0 from sklearn.model_selection import train_test_split 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_scores = log_reg.decision_function(X_test) 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: # dtype=‘int‘:将数据类型从 bool 型转为 int 型; y_predict = np.array(decision_scores >= threshold, dtype=‘int‘) fprs.append(FPR(y_test, y_predict)) tprs.append(TPR(y_test, y_predict))
import matplotlib.pyplot as plt plt.plot(fprs, tprs) plt.show()
from sklearn.metrics import roc_curve fprs, tprs, thresholds = roc_curve(y_test, decision_scores)
from sklearn.metrics import roc_auc_score roc_auc_score(y_test, decision_scores)
标签:scores spl 学习 fun pos near target ima .data
原文地址:https://www.cnblogs.com/volcao/p/9404519.html