Skip to content

Instantly share code, notes, and snippets.

@danyashorokh
Last active June 23, 2021 08:55
Show Gist options
  • Save danyashorokh/57ccc60520c292f90cee67e6ad1196b0 to your computer and use it in GitHub Desktop.
Save danyashorokh/57ccc60520c292f90cee67e6ad1196b0 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