-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAUC.cpp
132 lines (118 loc) · 2.93 KB
/
AUC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//cat label_score.txt | ./AUC
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct label_score
{
string label;
double score;
public:
label_score(char _label[], double _score)
{
label = _label;
score = _score;
}
bool operator<(const label_score& obj) const
{
return score < obj.score;
}
bool operator>(const label_score& obj) const
{
return score > obj.score;
}
};
int main()
{
long long lineNum = 0;
double rankSum = 0.0;
long long P = 0;
long long N = 0;
long long lastScoreNum = 0;
long long lastScorePNum = 0;
double lastScore = -10000.0;
string lastLabel = "";
char label[100];
double score;
vector<label_score> vecLabelScore;
while(EOF != scanf("%s %lf", label, &score))
{
label_score ls(label, score);
vecLabelScore.push_back(ls);
}//end while
sort(vecLabelScore.begin(), vecLabelScore.end(), less<label_score>());
for(int i = 0; i < vecLabelScore.size(); ++i)
{
const string& label = vecLabelScore[i].label;
score = vecLabelScore[i].score;
lineNum++;
if(lastLabel != "")
{
lastScoreNum++;
if("+1" == lastLabel || "1" == lastLabel)
{
P++;
lastScorePNum++;
}
else if("-1" == lastLabel || "0" == lastLabel)
{
N++;
}
else
{
printf("data error\n");
exit(0);
}
if(lastScore != score)
{
if(lastScorePNum > 0)
{
rankSum += (lineNum - 1 + lineNum - lastScoreNum)/2.0 * lastScorePNum;
}
lastScoreNum = 0;
lastScorePNum = 0;
}
}
lastScore = score;
lastLabel = label;
}//end for
lineNum++;
if(lastLabel != "")
{
lastScoreNum++;
if("+1" == lastLabel || "1" == lastLabel)
{
P++;
lastScorePNum++;
}
else if("-1" == lastLabel || "0" == lastLabel)
{
N++;
}
else
{
printf("data error\n");
exit(0);
}
if(true)
{
if(lastScorePNum > 0)
{
rankSum += (lineNum - 1 + lineNum - lastScoreNum)/2.0 * lastScorePNum;
}
lastScoreNum = 0;
lastScorePNum = 0;
}
}
//printf("P = %lld\n", P);
//printf("N = %lld\n", N);
//printf("lineNum = %lld\n", lineNum - 1);
//printf("rankSum = %lf\n", rankSum);
double numerator = rankSum - P * (P + 1)/2.0;
//printf("AUC = %lf/%lld = %lf\n", numerator, P * N, numerator / (P * N));
printf("%lf\n", numerator / (P * N));
return 0;
}