-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaml.py
397 lines (314 loc) · 12.5 KB
/
aml.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""This module contains helper functions for code in the aml repository.
Includes functions:
fancy_print
plot_validation_curve
plot_grid_search_results
plot_algorithm_and_hyperparameter_comparison
compare_models_cross_validation
features_by_importance
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from plotly import express as px
from sklearn import dummy, ensemble, linear_model, naive_bayes, neighbors, svm, tree
from sklearn.model_selection import cross_validate
def fancy_print(*args, parse_dict=True):
"""Print stuff in a crisp, sexy fashion.
Parameters
----------
*args
One or two positional arguments to be printed, fancily.
If a single positional argument is passed, it will be printed as a
title (i.e. underlined), unless it is a dictionary and `parse_dict`
is set to True, in which case separate lines of key-value pairs will
be printed.
If two positional arguments are passed, they will be printed in the
same line, separated by dots.
parse_dict : bool, default True
If True, and only a single positional argument is passed, and it is a
dictionary, key-value pairs will be printed on separate lines, fancily.
Returns
-------
None
Raises
------
TypeError
If a positional argument has no `__str__` method.
TypeError
If the number of passed positional arguments is not 1 or 2.
Examples
--------
>>> fancy_print('Very nice title')
Very nice title
---------------
>>> fancy_print('Number', 13)
Number ................................. 13
>>> my_dict = {123: 'Potato',
... 'pi': 3.14,
... 'jobs': {'Alice': 'Farmer', 'Bob': 'Fishmonger'}}
>>> fancy_print(my_dict, parse_dict=True)
123 .................................... Potato
pi ..................................... 3.14
jobs ................................... {'Alice': 'Farmer', 'Bob': 'Fishmonger'}
"""
for argument in args:
if '__str__' not in dir(argument):
raise TypeError(f"no method '__str__' for instance '{argument}'")
if len(args) == 1:
argument, = args
if isinstance(argument, dict) and parse_dict:
for key, value in argument.items():
fancy_print(key, value)
else:
title_length = len(str(argument))
print(f"\n{argument}\n{title_length * '-'}")
elif len(args) == 2:
argument_1, argument_2 = args
print(f"{str(argument_1) + ' ':.<40}", argument_2)
else:
raise TypeError(f'expected 1 or 2 positional arguments, got {len(args)} instead')
def plot_validation_curve(
train_scores, test_scores, param_range, title='Validation Curve', xlabel='Hyperparameter', linewidth=2):
"""Plot validation curve from train and test scores obtained from sklearn.model_selection.validation_curve.
Parameters
----------
train_scores : ndarray
Output from sklearn.model_selection.validation_curve.
test_scores : ndarray
Output from sklearn.model_selection.validation_curve.
param_range : iterable
Hyperparameter values.
title : str, default 'Validation Curve'
Plot title, should contain model name.
xlabel : str, default 'Hyperparameter'
Label fo x-axis, should contain hyperparameter name.
linewidth: int, default 2
Width of plot lines.
Returns
-------
None
"""
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
fig, ax = plt.subplots()
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel("Score")
ax.set(ylim=(0.0, 1.1))
ax.plot(
param_range, train_scores_mean, label="Training score", color="darkorange", lw=linewidth)
plt.fill_between(
param_range,
train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std,
alpha=0.2,
color="darkorange",
lw=linewidth)
ax.plot(
param_range, test_scores_mean, label="Cross-validation score", color="navy", lw=linewidth)
plt.fill_between(
param_range,
test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std,
alpha=0.2,
color="navy",
lw=linewidth)
plt.legend(loc="best")
plt.show()
def plot_grid_search_results(grid_search, hyp_par_1, hyp_par_2):
"""Plot heatmap of prediction scores over a 2D hyperparameter grid.
Parameters
----------
grid_search : sklearn.model_selection.GridSearchCV
GridSearchCV instance which was used to perform the grid search.
hyp_par_1 : str
Name of hyperparameter to be shown on the y-axis.
hyp_par_2 : str
Name of hyperparameter to be shown on the x-axis.
Returns
-------
None
"""
hp1 = grid_search.param_grid[hyp_par_1]
hp2 = grid_search.param_grid[hyp_par_2]
score = grid_search.cv_results_['mean_test_score'].reshape(len(hp1), len(hp2))
plt.figure(figsize=(6, 4), dpi=150)
plt.title('Validation accuracy')
plt.imshow(score)
plt.yticks(np.arange(len(hp1)), hp1)
plt.xticks(np.arange(len(hp2)), hp2, rotation=45)
plt.ylabel(hyp_par_1)
plt.xlabel(hyp_par_2)
plt.colorbar()
plt.show()
def _fill_missing_values(x):
"""Apply on series: fill missing values with `numpy.nan`.
Helper function for `plot_algorithm_and_hyperparameter_comparison`.
"""
if x:
return x[0]
return np.nan
def _algorithm_name_from_int(n, algorithm_names):
"""Apply on dataframe: replace integers with appropriate name strings.
Helper function for `plot_algorithm_and_hyperparameter_comparison`.
"""
for i, algorithm_name in enumerate(algorithm_names):
if n == i:
return algorithm_name
def _add_hover_data(fig, df, algorithm_name):
"""On hover, display hyperparameters for specified algorithm.
Helper function for `plot_algorithm_and_hyperparameter_comparison`.
"""
df_for_algorithm = df[df['algorithm'] == algorithm_name].dropna(axis=1)
columns = df_for_algorithm.columns
customdata = df.loc[df_for_algorithm.index, columns]
hovertemplate = '<br>'.join([f'{column}: %{{customdata[{i}]}}' for i, column in enumerate(columns)])
selector = {'name': algorithm_name}
fig.update_traces(customdata=customdata, hovertemplate=hovertemplate, selector=selector)
return fig
def plot_algorithm_and_hyperparameter_comparison(trials, algorithm_names, label='algorithm'):
"""Plot results of optimisation with the `hyperopt` library.
Parameters
----------
trials : hyperopt.Trials
After concluded optimisation, already containing trial data.
algorithm_names : list of str
Names of algorithms. These are required to be the same names used in
the `space` kwarg of `hyperopt.fmin`, listed in the same order.
label : str, default 'algorithm'
Name of the key used in the `space` dictionary to represent the
compared algorithms.
Returns
-------
None
"""
trials_df = pd.DataFrame([pd.Series(t['misc']['vals']).apply(_fill_missing_values) for t in trials])
trials_df['loss'] = [t['result']['loss'] for t in trials]
trials_df['trial_number'] = trials_df.index
trials_df[label] = trials_df[label].apply(lambda x: _algorithm_name_from_int(x, algorithm_names))
fig = px.scatter(trials_df, x='trial_number', y='loss', color=label)
for algorithm_name in algorithm_names:
fig = _add_hover_data(fig, trials_df, algorithm_name)
fig.show()
def compare_models_cross_validation(X, y, which='regression', model_names=None, scoring=None, hyperparameters=None):
"""Compare cross-validated metrics for different models.
Parameters
----------
X : pandas.DataFrame
Features.
y : pandas.Series
Target variable.
which: {'regression', 'classification'}, optional
Type of models to be evaluated.
(defaults to 'regression')
model_names : list of str, optional
Names of models to be evaluated.
(defaults to all models of type `which`)
scoring : list of str, optional
Names of metrics to evaluate models in.
(defaults to all metrics for type `which`)
hyperparameters : dict, optional
Keys are model names, values are dictionaries. The keys of those inner dictionaries are hyperparameter names
for that model, while their values are the corresponding hyperparameter values.
(defaults to default hyperparameter values for each model)
Returns
-------
pandas.DataFrame
Indexed by chosen models, columns are chosen metrics.
"""
models = {
'regression': {
'decision_tree_regressor': tree.DecisionTreeRegressor,
'dummy_regressor': dummy.DummyRegressor,
'k_neighbors_regressor': neighbors.KNeighborsRegressor,
'linear_regression': linear_model.LinearRegression,
'random_forest_regressor': ensemble.RandomForestRegressor,
'SVR': svm.SVR
},
'classification': {
'decision_tree_classifier': tree.DecisionTreeClassifier,
'dummy_classifier': dummy.DummyClassifier,
'gaussian_nb': naive_bayes.GaussianNB,
'k_neighbors_classifier': neighbors.KNeighborsClassifier,
'random_forest_classifier': ensemble.RandomForestClassifier,
}
}
metrics = {
'regression': ['neg_mean_absolute_percentage_error',
'neg_mean_squared_error',
'r2'],
'classification': ['accuracy',
'f1_micro',
'f1_macro',
'roc_auc']
}
if model_names is None:
model_names = models.get(which).keys()
if scoring is None:
scoring = metrics.get(which)
if hyperparameters is None:
hyperparameters = {}
rows = []
for mn in model_names:
if mn not in hyperparameters.keys():
hyperparameters[mn] = {}
m = models.get(which).get(mn)(**hyperparameters.get(mn))
m.fit(X, y)
cv = cross_validate(m, X, y, scoring=tuple(scoring))
r = [mn]
for s in scoring:
r.append(np.mean(cv[f'test_{s}']))
rows.append(r)
columns = [['model'] + scoring]
comparison = pd.DataFrame(rows, columns=columns).set_index('model')
return comparison
def features_by_importance(X, y, n=5, model='random_forest_classifier', random_state=None, plot=False):
"""Return list of most important features.
Parameters
----------
X : pandas.DataFrame
Features.
y : pandas.Series
Target variable.
n : int
Number of features to take.
model : {'decision_tree_classifier', 'random_forest_classifier'}, optional
Model to base importance on.
(defaults to 'random_forest_classifier')
random_state : int, optional
Random seed (set for repeatable function calls).
(defaults to None)
plot : bool, optional
If True, a bar plot of features by importance is displayed.
(defaults to False)
Returns
-------
list of str
The `n` most important features.
"""
models = {'decision_tree_classifier': tree.DecisionTreeClassifier(random_state=random_state),
'random_forest_classifier': ensemble.RandomForestClassifier(random_state=random_state)
}
m = models.get(model)
m.fit(X, y)
# Borrowed code
features = X.columns
importances = m.feature_importances_
sorted_ids = np.argsort(-importances)
sorted_importances = importances[sorted_ids]
sorted_features = features[sorted_ids]
names = list(sorted_features[:n])
if plot:
# Borrowed code
xs = range(len(features))
plt.figure()
plt.bar(xs, sorted_importances)
plt.xticks(xs, sorted_features, rotation='vertical')
plt.ylabel('Importance')
plt.title('Features by Importance')
plt.tight_layout()
plt.show()
return names