Skip to content

Instantly share code, notes, and snippets.

@lisitsyn
Created May 8, 2012 20:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lisitsyn/2638909 to your computer and use it in GitHub Desktop.
import numpy
def calcroc(out, lab):
"""
Computes the ROC curve.
Expects labels to be +/-1 and real-valued predictions
"""
idx = numpy.argsort(out)
tp=numpy.concatenate( ([1], 1-numpy.cumsum(lab[idx]>0)/float(numpy.sum(lab > 0))) )
fp=numpy.concatenate( ([1], 1-numpy.cumsum(lab[idx]<0)/float(numpy.sum(lab < 0))) )
score = 0.5*numpy.sum((fp[:-1]-fp[1:])*(tp[:-1]+tp[1:]))
curve= {'x' : fp,
'y' : tp,
'x_name' : 'False Positive Rate',
'y_name' : 'True Positive Rate'}
return (score, 'Curve', curve)
out = numpy.fromstring(""" 1.20814879 -1.20519035 1.18351523 1.30408547 -0.829074 0.34399558
-0.12408944 1.35452647 -1.18890897 -0.1704142 -0.07985624 -0.38371355
-0.20061302 1.26174435 0.47536179 1.24870554 1.1449951 -0.32134862
1.55818086 0.01502723 1.48630395 -0.12853333 1.24255885 0.42125256
1.23344994 -1.11213674 -0.45374504 1.58192206 1.49294562 -0.40708663
1.40598961 -0.25317132 -0.26124339 0.32749844 -0.89486076 1.43840149
1.21455612 1.59469835 1.24307617 -0.3293348 -0.27694969 0.02421238
1.37141201 -0.42178588 -0.49880041 -0.27321279 -0.46191969 -0.9048492
-0.52106143 1.31464331 1.34131406 -0.27303605 -0.1426731 1.10468863
0.19983258 -0.05861071 -0.46358963 1.32970318 1.44140974 1.41103469
1.45018209 1.41712216 1.56132351 1.39734724 1.25066088 0.00805265
-0.4933586 1.33791319 -0.20301414 1.38923381 1.22966203 1.60813376
-0.2459112 1.39797941 -0.2837912 1.57661269 1.44500238 -0.2931903
1.3881395 1.40177086 -0.69153593 -0.24933656 -0.7677776 0.3735544
1.2948281 -0.17445144 1.34688304 -1.09813021 1.16444493 -0.28409996
-0.49338806 -0.29508186 -0.15702927 -0.1608949 -0.26445382 1.55138675
-0.41720733 1.41307413 -0.82596606 1.33951396 -1.12481411 0.01874601
0.363431 1.34466679 -0.09915826 -0.26437377 0.34488553 -0.87959946
1.46258572 -0.70342085 1.47135662 -0.25955281 1.55672091 1.15881765
-0.02765325 0.20046099 -0.60237139 0.07938684 -0.10247525 1.4384692
-0.42011945 -1.04505801 1.42475404 1.21367847 1.26701798 1.27190156
-0.24435375 1.37045138 1.31024913 1.63179319 1.32824412 1.46042338
1.49865543 -0.32856775 1.25024408 1.41210192 0.32239787 1.34357644
1.28664846 0.00645303 1.35400006 1.25166359 0.24039335 0.43980396
1.32054768 -0.15348377 0.39205796 1.40221006 0.27166449 -0.26840071
1.37564887 1.45508804 1.38337695 1.25022562 1.58092002 -0.96048635
1.42731913 1.25900796 1.19826586 1.5585777 1.37656704 -0.51923879
0.31536045 1.38079555 1.32614868 -0.53199447 1.50113568 1.50226874
1.45888304 1.26667517 1.3464073 1.42324529 1.24406252 1.37897276
1.41257515 1.29730252 1.3974269 -0.26562188 1.23975899 -0.41019315
-0.29953402 -0.64174589 1.38576296 -0.26017425 -0.2651825 -0.25192384
1.21423903 1.43779664 0.37109365 1.42415 -0.01532335 1.27835699
0.1615466 1.09982736 1.11295451 -0.56052643 -0.21219515 -1.15182665
1.45472743 -0.03203304 -0.55777225 -0.51359495 1.44609761 1.28492515
1.28306531 -0.27095297 -0.27058033 -0.75118883 0.15628692 -0.33206902
-0.51780193 -0.45134105 1.34542696 1.53692877 -0.87439486 -0.34145148
-0.54989189 -0.51413126 -0.39985681 1.67348586 -0.03939613 1.391089
-0.54071536 1.56609025 1.09406996 0.87724013 1.39707569 1.60342283
-0.25180362 -0.80831272 0.29919683 -0.24746919 -0.27193478 1.39174792
1.32016065 1.33450529 1.44612825 -0.44779152 -0.26753214 -0.94491894
1.26372938 -0.0706527 1.39719217 1.31855467 -0.64429095 1.5509055
-0.20732851 -0.18275436 -0.06366657 -0.26418673 0.38903933 -0.02554227
-0.14264985 -0.81133663 -0.51944151 -1.34563473 1.10087924 0.21343395
1.44506291 1.34772238 -0.27785932 1.38084655 1.23928975 -0.26489361
-0.19934363 -0.55485676 1.48693411 -0.41071997 0.09163196 1.29629726
1.28989152 1.37095176 1.54226198 1.38744331 1.410159 1.35088929
1.40355568 -0.92702479 1.26765997 0.29744412 -0.54074394 1.25639951
1.28339621 -0.09142579 1.57139363 1.51246413 0.35946714 -1.00237466
-0.25147851 -0.34483809 1.35067285 -0.4986694 1.49708623 1.3115098
-0.1974041 1.32082658 -0.26954463 1.48970335 1.36117067 1.29629275
1.21910877 -1.10284708 1.15957754 1.31663038 -0.48858879 0.29671287
1.41244178 0.02551083 -0.86930815 1.4137561 1.62814444 1.32734536
1.41744999 1.28686955 1.24859866 1.44411827 -0.25852608 0.22163239
1.41770633 -0.50740413 1.60373654 -0.45030053 -0.88914568 -0.26616766
0.04545318 1.43798934 -0.29079585 0.18823247 -0.58076609 1.3974561
-0.40946996 1.63180206 -1.02840843 1.38245618 1.33989586 1.06724286
1.42713167 -1.00794106 1.17199288 0.24465449 0.17411407 1.28154005
-0.52221933 1.5215364 1.28565085 -0.4016757 1.31994157 1.47494903
-0.26636116 1.66980577 1.28543665 1.5059909 -0.20840131 1.30179627
-0.31669426 1.27605353 1.50329567 1.63339835 1.17962901 1.41019826
1.37954909 1.21493577 -0.33103366 -0.68165254 -0.24734911 -0.25541656
0.45602905 1.54664424 0.03687144 -0.00937711 1.37358308 -0.65785001
1.57914044 1.27182414 -0.2622183 -0.56435084 1.32197369 -0.45988541
1.39986758 0.0944783 -0.79034887 -0.31730307 1.44864322 0.38702019
0.29093558 1.44552155 1.25329033 1.30946299 1.15965443 0.20490242
-0.17558339 1.19722856 0.30042523 -0.18663713 -0.56284721 1.33236331
1.66080655 1.37628858 -0.34022988 1.38459747""",sep=' ')
lab = numpy.fromstring("""1. -1. 1. 1. -1. -1. -1. 1. -1. -1. -1. -1. -1. 1. -1. 1. 1. -1.
1. -1. 1. -1. 1. -1. 1. -1. -1. 1. 1. -1. 1. -1. -1. -1. -1. 1.
1. 1. 1. -1. -1. -1. 1. -1. -1. -1. -1. -1. -1. 1. 1. -1. -1. 1.
-1. -1. -1. 1. 1. 1. 1. 1. 1. 1. 1. -1. -1. 1. -1. 1. 1. 1.
-1. 1. -1. 1. 1. -1. 1. 1. -1. -1. -1. -1. 1. -1. 1. -1. 1. -1.
-1. -1. -1. -1. -1. 1. -1. 1. -1. 1. -1. -1. -1. 1. -1. -1. -1. -1.
1. -1. 1. -1. 1. 1. -1. -1. -1. -1. -1. 1. -1. -1. 1. 1. 1. 1.
-1. 1. 1. 1. 1. 1. 1. -1. 1. 1. -1. 1. 1. -1. 1. 1. -1. -1.
1. -1. -1. 1. -1. -1. 1. 1. 1. 1. 1. -1. 1. 1. 1. 1. 1. -1.
-1. 1. 1. -1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. -1. 1. -1.
-1. -1. 1. -1. -1. -1. 1. 1. -1. 1. -1. 1. -1. 1. 1. -1. -1. -1.
1. -1. -1. -1. 1. 1. 1. -1. -1. -1. -1. -1. -1. -1. 1. 1. -1. -1.
-1. -1. -1. 1. -1. 1. -1. 1. 1. 1. 1. 1. -1. -1. -1. -1. -1. 1.
1. 1. 1. -1. -1. -1. 1. -1. 1. 1. -1. 1. -1. -1. -1. -1. -1. -1.
-1. -1. -1. -1. 1. -1. 1. 1. -1. 1. 1. -1. -1. -1. 1. -1. -1. 1.
1. 1. 1. 1. 1. 1. 1. -1. 1. -1. -1. 1. 1. -1. 1. 1. -1. -1.
-1. -1. 1. -1. 1. 1. -1. 1. -1. 1. 1. 1. 1. -1. 1. 1. -1. -1.
1. -1. -1. 1. 1. 1. 1. 1. 1. 1. -1. -1. 1. -1. 1. -1. -1. -1.
-1. 1. -1. -1. -1. 1. -1. 1. -1. 1. 1. 1. 1. -1. 1. -1. -1. 1.
-1. 1. 1. -1. 1. 1. -1. 1. 1. 1. -1. 1. -1. 1. 1. 1. 1. 1.
1. 1. -1. -1. -1. -1. -1. 1. -1. -1. 1. -1. 1. 1. -1. -1. 1. -1.
1. -1. -1. -1. 1. -1. -1. 1. 1. 1. 1. -1. -1. 1. -1. -1. -1. 1.
1. 1. -1. 1. """,sep=' ')
print out
print lab
print calcroc(out,lab)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment