-
Notifications
You must be signed in to change notification settings - Fork 0
/
visuals.py
233 lines (195 loc) · 9.05 KB
/
visuals.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
###########################################
# Suppress matplotlib user warnings
# Necessary for newer version of matplotlib
import warnings
warnings.filterwarnings("ignore", category = UserWarning, module = "matplotlib")
#
# Display inline matplotlib plots with IPython
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
###########################################
import matplotlib.pyplot as pl
import matplotlib.patches as mpatches
import numpy as np
import pandas as pd
from time import time
from sklearn.metrics import f1_score, accuracy_score
def distribution(data, transformed = False):
"""
Visualization code for displaying skewed distributions of features
"""
# Create figure
fig = pl.figure(figsize = (11,5));
# Skewed feature plotting
for i, feature in enumerate(['loan_amnt','emp_length','annual_inc','dti','delinq_2yrs','earliest_cr_line','inq_last_6mths','open_acc','pub_rec','revol_bal','revol_util','total_acc','collections_12_mths_ex_med']):
ax = fig.add_subplot(3, 5, i+1)
ax.hist(data[feature], bins = 25, color = '#00A0A0')
ax.set_title("'%s'"%(feature), fontsize = 14)
ax.set_xlabel("Value")
ax.set_ylabel("Num. Records")
# ax.set_ylim((0, 10000))
# ax.set_yticks([0, 500, 1000, 1500, 2000])
# ax.set_yticklabels([0, 500, 1000, 1500, ">5000"])
# Plot aesthetics
if transformed:
fig.suptitle("Log-transformed Distributions of Continuous Census Data Features", fontsize = 16, y = 1.03)
else:
fig.suptitle("Skewed Distributions of Continuous Census Data Features", fontsize = 16, y = 1.03)
fig.tight_layout()
fig.show()
def evaluate(results, mse, r2):
"""
Visualization code to display results of various learners.
inputs:
- learners: a list of supervised learners
- stats: a list of dictionaries of the statistic results from 'train_predict()'
- accuracy: The score for the naive predictor
- f1: The score for the naive predictor
"""
# Create figure
fig, ax = pl.subplots(2, 3, figsize = (11,7))
# Constants
bar_width = 0.3
colors = ['#A00000','#00A0A0','#00A000']
# Super loop to plot four panels of data
for k, learner in enumerate(results.keys()):
for j, metric in enumerate(['train_time', 'mse_train', 'R2_train', 'pred_time', 'mse_test', 'R2_test']):
for i in np.arange(3):
# Creative plot code
ax[j%2, j%3].bar(i+k*bar_width, results[learner][i][metric], width = bar_width, color = colors[k])
ax[j%2, j%3].set_xticks([0.45, 1.45, 2.45])
ax[j%2, j%3].set_xticklabels(["1%", "10%", "100%"])
ax[j%2, j%3].set_xlabel("Training Set Size")
ax[j%2, j%3].set_xlim((-0.1, 3.0))
# Add unique y-labels
ax[0, 0].set_ylabel("Time (in seconds)")
ax[0, 1].set_ylabel("MSE")
ax[0, 2].set_ylabel("R^2")
ax[1, 0].set_ylabel("Time (in seconds)")
ax[1, 1].set_ylabel("MSE")
ax[1, 2].set_ylabel("R^2")
# Add titles
ax[0, 0].set_title("Model Training")
ax[0, 1].set_title("MSE on Training Subset")
ax[0, 2].set_title("R^2 on Training Subset")
ax[1, 0].set_title("Model Predicting")
ax[1, 1].set_title("MSE on Testing Set")
ax[1, 2].set_title("R^2 on Testing Set")
# Add horizontal lines for naive predictors
ax[0, 1].axhline(y = mse, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[1, 1].axhline(y = mse, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[0, 2].axhline(y = r2, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[1, 2].axhline(y = r2, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
# Set y-limits for score panels
ax[0, 1].set_ylim((-.5, 1))
ax[0, 2].set_ylim((-.5, 1))
ax[1, 1].set_ylim((-.5, 1))
ax[1, 2].set_ylim((-.5, 1))
# Create patches for the legend
patches = []
for i, learner in enumerate(results.keys()):
patches.append(mpatches.Patch(color = colors[i], label = learner))
pl.legend(handles = patches, bbox_to_anchor = (-.80, 2.53), \
loc = 'upper center', borderaxespad = 0., ncol = 3, fontsize = 'x-large')
# Aesthetics
pl.suptitle("Performance Metrics for Three Supervised Learning Models", fontsize = 16, y = 1.10)
pl.tight_layout()
pl.show()
def classevaluate(results, ROCAUC, prec):
"""
Visualization code to display results of various learners.
inputs:
- learners: a list of supervised learners
- stats: a list of dictionaries of the statistic results from 'train_predict()'
- accuracy: The score for the naive predictor
- f1: The score for the naive predictor
"""
# Create figure
fig, ax = pl.subplots(2, 3, figsize = (11,7))
# Constants
bar_width = 0.3
colors = ['#A00000','#00A0A0','#00A000']
# Super loop to plot four panels of data
for k, learner in enumerate(results.keys()):
for j, metric in enumerate(['train_time', 'ROC_AUC_train', 'precision_train', 'pred_time', 'ROC_AUC_test', 'precision_test']):
for i in np.arange(3):
# Creative plot code
ax[j%2, j%3].bar(i+k*bar_width, results[learner][i][metric], width = bar_width, color = colors[k])
ax[j%2, j%3].set_xticks([0.45, 1.45, 2.45])
ax[j%2, j%3].set_xticklabels(["1%", "10%", "100%"])
ax[j%2, j%3].set_xlabel("Training Set Size")
ax[j%2, j%3].set_xlim((-0.1, 3.0))
# Add unique y-labels
ax[0, 0].set_ylabel("Time (in seconds)")
ax[0, 1].set_ylabel("ROC AUC")
ax[0, 2].set_ylabel("Precisiion")
ax[1, 0].set_ylabel("Time (in seconds)")
ax[1, 1].set_ylabel("ROC AUC")
ax[1, 2].set_ylabel("Precision")
# Add titles
ax[0, 0].set_title("Model Training")
ax[0, 1].set_title("ROC AUC on Training Subset")
ax[0, 2].set_title("Precision on Training Subset")
ax[1, 0].set_title("Model Predicting")
ax[1, 1].set_title("ROC AUC on Testing Set")
ax[1, 2].set_title("Precision on Testing Set")
# Add horizontal lines for naive predictors
ax[0, 1].axhline(y = ROCAUC, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[1, 1].axhline(y = ROCAUC, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[0, 2].axhline(y = prec, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[1, 2].axhline(y = prec, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
# Set y-limits for score panels
ax[0, 1].set_ylim((0, 1))
ax[0, 2].set_ylim((0, 1))
ax[1, 1].set_ylim((0, 1))
ax[1, 2].set_ylim((0, 1))
# Create patches for the legend
patches = []
for i, learner in enumerate(results.keys()):
patches.append(mpatches.Patch(color = colors[i], label = learner))
pl.legend(handles = patches, bbox_to_anchor = (-.80, 2.53), \
loc = 'upper center', borderaxespad = 0., ncol = 3, fontsize = 'x-large')
# Aesthetics
pl.suptitle("Performance Metrics for Three Supervised Learning Models", fontsize = 16, y = 1.10)
pl.tight_layout()
pl.show()
# def feature_plot(importances, X_train, y_train):
# Display the five most important features
# indices = np.argsort(importances)[::-1]
# columns = X_train.columns.values[indices[:5]]
# values = importances[indices][:5]
#
# # Creat the plot
# fig = pl.figure(figsize = (9,5))
# pl.title("Normalized Weights for First Five Most Predictive Features", fontsize = 16)
# pl.bar(np.arange(5), values, width = 0.6, align="center", color = '#00A000', \
# label = "Feature Weight")
# pl.bar(np.arange(5) - 0.3, np.cumsum(values), width = 0.2, align = "center", color = '#00A0A0', \
# label = "Cumulative Feature Weight")
# pl.xticks(np.arange(5), columns)
# pl.xlim((-0.5, 4.5))
# pl.ylabel("Weight", fontsize = 12)
# pl.xlabel("Feature", fontsize = 12)
#
# pl.legend(loc = 'upper center')
# pl.tight_layout()
# pl.show()
def feature_plot(importances, X_train, y_train):
# Display the five most important features
indices = np.argsort(importances)[::-1]
columns = X_train.columns.values[indices[:10]]
values = importances[indices][:10]
# Create the plot
fig = pl.figure(figsize = (10,5))
pl.title("Normalized Weights for First Ten Most Predictive Features", fontsize = 16)
pl.bar(np.arange(10), values, width = 0.6, align="center", color = '#00A000', \
label = "Feature Weight")
pl.bar(np.arange(10) - 0.3, np.cumsum(values), width = 0.2, align = "center", color = '#00A0A0', \
label = "Cumulative Feature Weight")
pl.xticks(np.arange(10), columns)
pl.xlim((-0.5, 9.5))
pl.ylabel("Weight", fontsize = 12)
pl.xlabel("Feature", fontsize = 12)
pl.legend(loc = 'upper center')
pl.tight_layout()
pl.show()