Created
November 11, 2019 03:31
-
-
Save piyush01123/24dfb8a12f20373f1ae60b7a7099caf8 to your computer and use it in GitHub Desktop.
ROC-AUC score
This file contains hidden or 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
# demonstrates plotting ROC curve and getting AUC score | |
import numpy as np | |
def roc_curve(y_true, y_pred): | |
fpr = [] | |
tpr = [] | |
thresholds = np.arange(0.0, 1.01, .01) | |
P = sum(y_true) | |
N = len(y_true) - P | |
for thresh in thresholds: | |
FP=0 | |
TP=0 | |
for y_t, y_p in zip(y_true, y_pred): | |
if y_p > thresh: | |
if y_t == 1: | |
TP = TP + 1 | |
else: | |
FP = FP + 1 | |
fpr.append(FP/float(N)) | |
tpr.append(TP/float(P)) | |
# plt.plot(fpr, tpr) | |
return tpr, fpr | |
def integrate(x_s, y_s): | |
# integartion by trapezoidal rule | |
x_diffs = [abs(x_s[i]-x_s[i-1]) for i in range(1,len(x_s))] | |
y_sum = [y_s[i]+y_s[i-1] for i in range(1,len(y_s))] | |
return .5*sum([a*b for a, b in zip(y_sum, x_diffs)]) | |
# example | |
from sklearn.datasets import make_classification | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LogisticRegression | |
X, y = make_classification(n_samples=1000, n_classes=2, weights=[1,1], random_state=1) | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=2) | |
model = LogisticRegression() | |
model.fit(X_train, y_train) | |
y_pred = model.predict_proba(X_test)[:,1] | |
tpr, fpr = roc_curve(y_test, y_pred) | |
auc = integrate(x_s=fpr, y_s=tpr) | |
plt.plot(fpr, tpr) | |
print("AUC score = {}".format(auc)) | |
# verify | |
import sklearn.metrics as metrics, auc | |
fpr, tpr, threshold = metrics.roc_curve(y_test, y_pred) | |
roc_auc = metrics.auc(fpr, tpr) | |
fig = plt.figure(figsize=(10,6)) | |
ax = fig.add_subplot(111) | |
ax.set_title('Receiver Operating Characteristic') | |
ax.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc) | |
ax.legend(loc = 'lower right') | |
ax.plot([0, 1], [0, 1],'r--') | |
plt.xlim([0, 1]) | |
plt.ylim([0, 1]) | |
ax.set_ylabel('True Positive Rate') | |
ax.set_xlabel('False Positive Rate') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment