Skip to content

Instantly share code, notes, and snippets.

@franchb
Created September 5, 2018 12:09
Show Gist options
  • Save franchb/3a0a78a17be66bd296821fc39efb20fd to your computer and use it in GitHub Desktop.
Save franchb/3a0a78a17be66bd296821fc39efb20fd to your computer and use it in GitHub Desktop.
Separation plot
# separation plot
# Author: Cameron Davidson-Pilon,2013
# see http://mdwardlab.com/sites/default/files/GreenhillWardSacks.pdf
import matplotlib.pyplot as plt
import numpy as np
def separation_plot( p, y, **kwargs ):
"""
This function creates a separation plot for logistic and probit classification.
See http://mdwardlab.com/sites/default/files/GreenhillWardSacks.pdf
p: The proportions/probabilities, can be a nxM matrix which represents M models.
y: the 0-1 response variables.
"""
assert p.shape[0] == y.shape[0], "p.shape[0] != y.shape[0]"
n = p.shape[0]
try:
M = p.shape[1]
except:
p = p.reshape( n, 1 )
M = p.shape[1]
colors_bmh = np.array( ["#eeeeee", "#348ABD"] )
fig = plt.figure( )
for i in range(M):
ax = fig.add_subplot(M, 1, i+1)
ix = np.argsort( p[:,i] )
#plot the different bars
bars = ax.bar( np.arange(n), np.ones(n), width=1.,
color = colors_bmh[ y[ix].astype(int) ],
edgecolor = 'none')
ax.plot( np.arange(n+1), np.append(p[ix,i], p[ix,i][-1]), "k",
linewidth = 1.,drawstyle="steps-post" )
#create expected value bar.
ax.vlines( [(1-p[ix,i]).sum()], [0], [1] )
plt.xlim( 0, n)
plt.tight_layout()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment