Last active
May 4, 2019 16:02
-
-
Save mikigom/f0db5f52568009fbc631d6d9ada8dfef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.metrics import roc_auc_score | |
n_class_0 = 50000 | |
n_class_1 = 100 | |
def main(): | |
# Suppose imblanced classification dataset | |
labels = n_class_0 * [False] + n_class_1 * [True] | |
y_true = n_class_0 * [0] + n_class_1 * [1] | |
# Suppose totally random classifier | |
y_scores = np.random.uniform(size=n_class_0 + n_class_1) | |
result = [] | |
for i in range(n_class_0 + n_class_1): | |
result.append((labels[i], y_scores[i])) | |
y_scores = [x[1] for x in result] | |
try: | |
auc = roc_auc_score(y_true, y_scores) | |
except: | |
auc = 0 | |
print("AUC ", auc) | |
# normal class | |
X1 = [x[1] for x in result if x[0]] | |
# abnormal class | |
Y1 = [x[1] for x in result if not x[0]] | |
minP = min([x[1] for x in result]) - 1 | |
maxP = max([x[1] for x in result]) + 1 | |
################################################################## | |
# AUPR IN | |
################################################################## | |
auprin = 0.0 | |
recallTemp = 1.0 | |
for e in np.arange(minP, maxP, 0.2): | |
tp = np.sum(np.greater_equal(X1, e)) | |
fp = np.sum(np.greater_equal(Y1, e)) | |
if tp + fp == 0: | |
continue | |
precision = tp / (tp + fp) | |
recall = tp / np.float(len(X1)) | |
auprin += (recallTemp - recall) * precision | |
recallTemp = recall | |
auprin += recall * precision | |
print("auprin: ", auprin) | |
################################################################## | |
# AUPR OUT | |
################################################################## | |
minp, maxP = -maxP, -minP | |
X1 = [-x for x in X1] | |
Y1 = [-x for x in Y1] | |
auprout = 0.0 | |
recallTemp = 1.0 | |
for e in np.arange(minP, maxP, 0.2): | |
tp = np.sum(np.greater_equal(Y1, e)) | |
fp = np.sum(np.greater_equal(X1, e)) | |
if tp + fp == 0: | |
continue | |
precision = tp / (tp + fp) | |
recall = tp / np.float(len(Y1)) | |
auprout += (recallTemp - recall) * precision | |
recallTemp = recall | |
auprout += recall * precision | |
print("auprout: ", auprout) | |
if __name__ == '__main__': | |
main() | |
""" | |
AUC 0.5059258 | |
auprin: 0.00226314447686 | |
auprout: 0.997870047959 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment