本文和大家分享的主要是python計(jì)算auc指標(biāo)相關(guān)內(nèi)容,一起來看看吧,希望對大家學(xué)習(xí)python有所幫助。
1.安裝scikit-learn
1.1Scikit-learn 依賴
· Python (>= 2.6 or >= 3.3),
· NumPy (>= 1.6.1),
· SciPy (>= 0.9).
分別查看上述三個(gè)依賴的版本,
python -V 結(jié)果:Python 2.7.3
python -c 'import scipy; print scipy.version.version' scipy版本結(jié)果:0.9.0
python -c "import numpy; print numpy.version.version" numpy結(jié)果:1.10.2
1.2 Scikit-learn安裝
如果你已經(jīng)安裝了NumPy、SciPy和python并且均滿足1.1中所需的條件,那么可以直接運(yùn)行sudo
pip install - U scikit - learn
執(zhí)行安裝。
2.計(jì)算auc指標(biāo)
1 import numpy as np
2 from sklearn.metrics import roc_auc_score
3 y_true = np.array([0, 0, 1, 1])
4 y_scores = np.array([0.1, 0.4, 0.35, 0.8])
5 roc_auc_score(y_true, y_scores)
輸出:0.75
3.計(jì)算roc曲線
1 import numpy as np
2 from sklearn import metrics
3 y = np.array([1, 1, 2, 2]) #實(shí)際值
4 scores = np.array([0.1, 0.4, 0.35, 0.8]) #預(yù)測值
5 fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2) #pos_label=2,表示值為2的實(shí)際值為正樣本
6 print fpr
7 print tpr
8 print thresholds
輸出:
array([ 0. , 0.5, 0.5, 1. ])
array([ 0.5, 0.5, 1. , 1. ])
array([ 0.8 , 0.4 , 0.35, 0.1 ])
來源:博客園