先发代码,其他的过会儿再补。
double get_auc(vector<double>& prob, vector<int>& label) {
int n = label.size(), pos = 0;
vector<pair<double, int>> pl;
for (int i = 0; i < n; i++) {
pos += label[i];
pl.emplace_back(prob[i], label[i]);
}
sort(pl.begin(), pl.end());
double pos_sum = 0;
for (int left = 0, right = 0; right < n; left = right) {
double sum = 0, cnt = 0;
while (right < n && pl[right].first == pl[left].first) {
cnt += pl[right++].second;
sum += right + 1;
}
pos_sum += sum * cnt / (right - left);
}
return (pos_sum - (pos * (pos + 1) / 2)) / (pos * (n - pos));
}
double get_auc(vector<double>& prob, vector<int>& label) {
int n = label.size(), pos = 0;
vector<pair<double, int>> pl;
for (int i = 0; i < n; i++) {
pos += label[i];
pl.emplace_back(prob[i], label[i]);
}
sort(pl.begin(), pl.end(), [&](auto a, auto b){return a.first > b.first;});
double auc = 0;
for (int left = 0, right = 0, pre_tp = 0; right < n; left = right) {
int delta_fp = 0, delta_tp = 0;
while (right < n && pl[right].first == pl[left].first)
pl[right++].second ? delta_tp++ : delta_fp++;
auc += (2 * pre_tp + delta_tp) * delta_fp / 2.;
pre_tp += delta_tp;
}
return auc / (pos * (n - pos));
}
原文:https://www.cnblogs.com/tmpUser/p/15092467.html