📚 The CoCalc Library - books, templates and other resources
License: OTHER
# separation plot1# Author: Cameron Davidson-Pilon,20132# see http://mdwardlab.com/sites/default/files/GreenhillWardSacks.pdf345import matplotlib.pyplot as plt6import numpy as np78910def separation_plot( p, y, **kwargs ):11"""12This function creates a separation plot for logistic and probit classification.13See http://mdwardlab.com/sites/default/files/GreenhillWardSacks.pdf1415p: The proportions/probabilities, can be a nxM matrix which represents M models.16y: the 0-1 response variables.1718"""19assert p.shape[0] == y.shape[0], "p.shape[0] != y.shape[0]"20n = p.shape[0]2122try:23M = p.shape[1]24except:25p = p.reshape( n, 1 )26M = p.shape[1]2728colors_bmh = np.array( ["#eeeeee", "#348ABD"] )293031fig = plt.figure( )3233for i in range(M):34ax = fig.add_subplot(M, 1, i+1)35ix = np.argsort( p[:,i] )36#plot the different bars37bars = ax.bar( np.arange(n), np.ones(n), width=1.,38color = colors_bmh[ y[ix].astype(int) ],39edgecolor = 'none')40ax.plot( np.arange(n+1), np.append(p[ix,i], p[ix,i][-1]), "k",41linewidth = 1.,drawstyle="steps-post" )42#create expected value bar.43ax.vlines( [(1-p[ix,i]).sum()], [0], [1] )44plt.xlim( 0, n)4546plt.tight_layout()4748return495051525354