真正例率 TPR = TP / (TP + FN)
表示,預測為正例且真實情況為正例的,占所有真實情況中正例的比率。
假正例率 FPR = FP / (TN + FP)
表示的,預測為正例但真實情況為反例的,占所有真實情況中反例的比率。
CODE
依然使用前面的極度有偏數字資料集
def TPR(y_ture,y_predict):
tp = TP(y_ture,y_predict)
fn = FN(y_ture,y_predict)
try:
return tp / (tp + fn)
except:
return 0.0
def FPR(y_ture,y_predict):
fp = FP(y_ture,y_predict)
tn = TN(y_ture,y_predict)
try:
return fp / (fp + tn)
except:
return 0.0
#ROC曲線
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 sklearn.metrics import precision_score
from sklearn.metrics import recall_score
fprs = []
tprs = []
thresholds = np.arange(np.min(decision_scores),np.max(decision_scores))
# print(thresholds)
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))
import matplotlib.pyplot as plt
plt.plot(fprs,tprs)
scikit-learn中的ROC曲線
#scikit-learn中的ROC曲線
from sklearn.metrics import roc_curve
fprs,tprs,threshold = roc_curve(y_test,decision_scores)