Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shandou/8b13103466bb7e4be243ad9fd970c7e1 to your computer and use it in GitHub Desktop.
Save shandou/8b13103466bb7e4be243ad9fd970c7e1 to your computer and use it in GitHub Desktop.
[Python] GINI, KS, Plotting ROC curve
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