-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotUtilities.py
96 lines (74 loc) · 3.25 KB
/
plotUtilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import numpy as np
import matplotlib.pyplot as plt
def plotHistogram(xSurvive, xDied, xAll, name, nBins, normed, axisRange):
# the histogram of the data
fig = plt.figure()
nPass, bins, patches = plt.hist(xSurvive, bins = nBins, range = axisRange, normed = normed, facecolor='g', alpha=0.75)
nAll, bins, patches = plt.hist(xAll, bins = nBins, range = axisRange, normed = normed, facecolor='g', alpha=0.75)
plt.close(fig)
print(name)
print("Pass",nPass)
print("All",nAll)
nPass = nPass/nAll
nPass = np.nan_to_num(nPass)
x = range(0,len(nPass))
yerr = nPass*(1-nPass)/nAll
yerr = np.nan_to_num(yerr)
yerr = np.sqrt(yerr)
fig = plt.figure(name)
plt.errorbar(x, nPass, yerr=yerr, fmt='o')
print("P: ",nPass)
plt.title(name)
plt.xlabel('Feature')
plt.ylabel('Probability')
plt.grid(True)
plt.show(block=False)
#####################################################################
#####################################################################
#####################################################################
def plotFR(x, modelResult, doBlock=True):
fig = plt.scatter(x, modelResult)
fig.set_title("")
fig.set_xlabel("Variable")
fig.set_ylabel("Model Prediction")
plt.show(block=doBlock)
#####################################################################
#####################################################################
#####################################################################
def plotVariable(x, y):
print (x.shape, y.shape)
y = np.broadcast_to(y,x.shape)
survivedIndexes = y[:,0]==1.0
diedIndexes = y[:,0]==0.0
allIndexes = y[:,0]>=0.0
survivedFeatures = x[survivedIndexes]
diedFeatures = x[diedIndexes]
allFeatures = x[allIndexes]
featuresNames = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Cabin", "Embarked"]
#featuresRanges = [(0,4), (-2,2), (0,100), (0,10), (0,10), (0,100), (-16,8), (0,5)]
featuresRanges = [(-1,2), (-1,2), (-1,2), (-1,2), (-1,2), (-1,2), (-1,2), (-1,2)]
nFeatures = 8
for iFeature in range(0, nFeatures):
plotHistogram(xSurvive = survivedFeatures[:,iFeature],
xDied = diedFeatures[:,iFeature],
xAll = allFeatures[:,iFeature],
name = featuresNames[iFeature],
nBins = 21, normed = 0,axisRange=featuresRanges[iFeature])
plt.show(block=True)
#####################################################################
#####################################################################
def plotDiscriminant(modelResult, labels, plotTitle, doBlock=True):
fig, (axis0, axis1) = plt.subplots(nrows=1, ncols=2, figsize=(9, 4), num = plotTitle)
axis0.scatter(modelResult, labels, c="b")
axis0.set_title("")
axis0.set_xlabel("Prediction")
axis0.set_ylabel("Target")
#error = (modelResult - labels)/labels
error = (modelResult - labels)
axis1.hist(error, bins=60, range=(-1,1))
#axis1.set_xlabel("(Prediction - Target)/Target")
axis1.set_xlabel("Prediction - Target")
axis1.set_title("Pull")
plt.show(block=doBlock)
#####################################################################
#####################################################################