This article and everyone to share is mainly Python to calculate the AUC index related content, together to see it, hope to learn python to help you.
1. Installing Scikit-learn
1.1scikit-learn Dependency
· Python (>= 2.6 or >= 3.3),
· NumPy (>= 1.6.1),
· SciPy (>= 0.9).
View each of the three dependent versions above,
Python-v Result: Python 2.7.3
Python-c ' Import scipy; Print scipy.version.version ' scipy version results: 0.9.0
Python-c "Import numpy; Print numpy.version.version "NumPy Result: 1.10.2
1.2 Scikit-learn Installation
If you have installed NumPy, scipy, and Python and all meet the required conditions in 1.1, you can run sudo directly
Pip Install-u Scikit-learn
Perform the installation.
2. Calculate the AUC indicator
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)
Output: 0.75
3. Calculate the ROC curve
1 Import numpy as NP
2 from Sklearn Import metrics
3 y = np. Array ([1, 1, 2, 2]) #实际值
4 scores = NP. Array ([0.1, 0.4, 0.35, 0.8]) #预测值
5 FPR, TPR, thresholds = Metrics.roc_curve (y, scores, pos_label=2) #pos_label = 2, representing the actual value of 2 as a positive sample
6 Print FPR
7 Print TPR
8 Print thresholds
Output:
Array ([0., 0.5, 0.5, 1.])
Array ([0.5, 0.5, 1., 1.])
Array ([0.8, 0.4, 0.35, 0.1])
Source: Blog Park
Python Calculates AUC metrics