Calculate the AUC code-python

Source: Internet
Author: User

Write your own code to calculate the AUC, using Scikit-learn's AUC to calculate the function sklearn.metrics. auc (x, y, reorder=false) Do some tests, the result is the same, if there are errors, please correct me.

Idea: 1. First sort the predicted values, using Python's own function sorted, see comments.

2. For all samples according to the predicted value from small to large mark Rank,rank is actually Index+1,index is the index in the sorted sorted_pred array

3. Add the rank of all positive samples to the same predicted value, regardless of the positive or negative of the sample, and add the average to rank.

4. The case where rank is added and subtracted from the positive sample after the positive sample, divided by the total number of combinations, to obtain the AUC

1 #-*-coding:utf-8-*-2 """3 Created on Wed 3 10:48:284 5 @author: Vincent6 """7 ImportNumPy as NP8  fromSklearnImportMetrics9y = Np.array ([1, 0, 0, 1, 1, 1, 0, 1, 1, 1])Tenpred = Np.array ([0.9, 0.9, 0.8, 0.8, 0.7,0.7,0.7,0.6,0.5,0.4]) OneFPR, TPR, thresholds = Metrics.roc_curve (y, pred, pos_label=1) A Print(METRICS.AUC (FPR, TPR)) - defGetauc (labels, pred): -     " "The index value of the pred array is ordered by the size of pred[i], and the returned sorted_pred is a new array . the Sorted_pred[0] is the value of the smallest I in pred[i], for this example, Sorted_pred[0]=8 -     " " -sorted_pred = sorted (range (len (pred)), key =LambdaI:pred[i]) -pos = 0.0#number of positive samples +Neg = 0.0#number of negative samples -AUC = 0.0 +Last_pre =Pred[sorted_pred[0]] ACount = 0.0 atPre_sum = 0.0#The sum of rank is equal to the predicted value before the current position, rank is 1, so in the following code is i+1 -Pos_count = 0.0#count the number of positive samples in a sample with equal predicted values -      forIinchRange (len (sorted_pred)): -         ifLabels[sorted_pred[i]] >0: -pos + = 1 -         Else: inNeg + = 1 -         ifLast_pre! = Pred[sorted_pred[i]]:#the current predicted probability value is not the same as the previous value to             #for sample rank with equal predicted values, the average is required and the rank is summed +AUC + = Pos_count * Pre_sum/Count -Count = 1 thePre_sum = i + 1#Update to current rank *Last_pre =Pred[sorted_pred[i]] $             ifLabels[sorted_pred[i]] >0:Panax NotoginsengPos_count = 1#If the current sample is a positive sample, set to 1 -             Else: thePos_count = 0#the reverse is 0 . +         Else: APre_sum + = i + 1#record Rank's and theCount + = 1#record rank and the corresponding number of samples, Pre_sum/count is the average. +             ifLabels[sorted_pred[i]] > 0:#if it is a positive sample -Pos_count + = 1#positive sample count plus 1 $AUC + = Pos_count * Pre_sum/count#plus the last sample group with the same predicted value $AUC-= pos * (pos + 1)/2#minus the positive sample before the positive sample -AUC = AUC/(POS * neg)#divided by the total number of combinations -     returnAUC the Print(Getauc (y, pred))

Calculate the AUC code-python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.