Forked from danyashorokh/[Python] GINI, KS, Plotting ROC curve
Created
April 11, 2021 19:22
-
-
Save shandou/8b13103466bb7e4be243ad9fd970c7e1 to your computer and use it in GitHub Desktop.
[Python] GINI, KS, Plotting ROC curve
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
from sklearn.metrics import roc_curve, roc_auc_score, auc | |
from scipy import stats | |
import matplotlib.pyplot as plt | |
# %matplotlib inline | |
def plot_roc_curve(y, y_pred, gini, ks): | |
fpr, tpr, thresholds = roc_curve(y, y_pred) | |
roc_auc = auc(fpr, tpr) | |
fig = plt.figure() | |
plt.plot(fpr, tpr, 'b--', label='%s AUC = %0.4f, GINI = %0.2f, KS = %s' % ('Model: ', roc_auc, gini, ks)) | |
plt.plot([0, 1], [0, 1], 'k--') | |
plt.xlim([0.0, 1.0]) | |
plt.ylim([0.0, 1.0]) | |
plt.xlabel('False Positive Rate') | |
plt.ylabel('True Positive Rate') | |
plt.legend(loc=0, fontsize='small') | |
plt.show() | |
gini = 2 * roc_auc_score(df['y'], df['y_pred']) - 1 | |
ks = stats.ks_2samp(df[df['y'] == 0]['y_pred'], df[df['y'] == 1]['y_pred']).statistic | |
print('GINI = %s, KS = %s' % (gini, ks)) | |
plot_roc_curve(y, y_pred, gini, ks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment