Last active
February 2, 2018 19:25
-
-
Save arendu-zz/2235880f8d02f5accf5d94fe7cd61d86 to your computer and use it in GitHub Desktop.
auc
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 | |
actual = [1, 0, 0, 1, 1, 0, 0, 1, 0, 1] | |
predicted = [0.8, 0.2, 0.6, 0.3, 0.1, 0.2, 0.3, 0.9, 0.2, 0.7] | |
y_true = np.array(actual) | |
y_pred = np.array(predicted) | |
heaviside_new = np.vectorize(lambda x : 0.0 if x<0 else (.5 if x == 0 else 1.0)) | |
heaviside = np.vectorize(lambda x : 0 if x<0 else .5 if x == 0 else 1) #buggy | |
def Broadcast_AUC(y_true, y_pred): | |
#the predictions of our classifier for all the positive labeled data | |
pos_pred = y_pred[ y_true == 1] | |
#the predictions of our classifier for all the negative labeled data | |
neg_pred = y_pred[ y_true == 0] | |
#creates a matrix that have pairwise difference between all the observations | |
pairwise_matrix = pos_pred[:, np.newaxis] - neg_pred | |
transform = heaviside(pairwise_matrix) | |
return transform.mean(), transform | |
def Broadcast_new_AUC(y_true, y_pred): | |
#the predictions of our classifier for all the positive labeled data | |
pos_pred = y_pred[ y_true == 1] | |
#the predictions of our classifier for all the negative labeled data | |
neg_pred = y_pred[ y_true == 0] | |
#creates a matrix that have pairwise difference between all the observations | |
pairwise_matrix = pos_pred[:, np.newaxis] - neg_pred | |
transform = heaviside_new(pairwise_matrix) | |
return transform.mean(), transform | |
def Loop_AUC(y_true, y_pred): | |
#the predictions of our classifier for all the positive labeled data | |
pos_pred = y_pred[ y_true == 1] | |
P = len(pos_pred) #the number of the positive population | |
#the predictions of our classifier for all the negative labeled data | |
neg_pred = y_pred[ y_true == 0] | |
N = len(neg_pred) #the number of the negative population | |
AUC = 0 | |
matrix = np.ones((len(pos_pred), len(neg_pred))) | |
for p_idx, pos in enumerate(pos_pred): | |
for n_idx, neg in enumerate(neg_pred): | |
AUC += heaviside(pos - neg) | |
matrix[p_idx, n_idx] = heaviside(pos - neg) | |
return AUC/(P*N), matrix | |
loop_auc, loop_matrix = Loop_AUC(y_true, y_pred) | |
b_auc, b_matrix = Broadcast_AUC(y_true, y_pred) | |
b_new_auc, b_new_matrix = Broadcast_new_AUC(y_true, y_pred) | |
print 'sklearn', roc_auc_score(y_true, y_pred) | |
print 'loop_auc', loop_auc | |
print 'b_auc', b_auc #buggy output | |
print 'b_new_auc', b_new_auc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment