Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 3195
1
# separation plot
2
# Author: Cameron Davidson-Pilon,2013
3
# see https://onlinelibrary.wiley.com/doi/10.1111/j.1540-5907.2011.00525.x
4
5
6
import matplotlib.pyplot as plt
7
import numpy as np
8
9
10
11
def separation_plot( p, y, **kwargs ):
12
"""
13
This function creates a separation plot for logistic and probit classification.
14
See https://onlinelibrary.wiley.com/doi/10.1111/j.1540-5907.2011.00525.x
15
16
p: The proportions/probabilities, can be a nxM matrix which represents M models.
17
y: the 0-1 response variables.
18
19
"""
20
assert p.shape[0] == y.shape[0], "p.shape[0] != y.shape[0]"
21
n = p.shape[0]
22
23
try:
24
M = p.shape[1]
25
except:
26
p = p.reshape( n, 1 )
27
M = p.shape[1]
28
29
colors_bmh = np.array( ["#eeeeee", "#348ABD"] )
30
31
32
fig = plt.figure( )
33
34
for i in range(M):
35
ax = fig.add_subplot(M, 1, i+1)
36
ix = np.argsort( p[:,i] )
37
#plot the different bars
38
bars = ax.bar( np.arange(n), np.ones(n), width=1.,
39
color = colors_bmh[ y[ix].astype(int) ],
40
edgecolor = 'none')
41
ax.plot( np.arange(n+1), np.append(p[ix,i], p[ix,i][-1]), "k",
42
linewidth = 1.,drawstyle="steps-post" )
43
#create expected value bar.
44
ax.vlines( [(1-p[ix,i]).sum()], [0], [1] )
45
plt.xlim( 0, n)
46
47
plt.tight_layout()
48
49
return
50
51
52
53
54