-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
228 lines (192 loc) · 6.92 KB
/
evaluation.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
#!/usr/bin/env python3
import pandas as pd
import numpy as np
from typing import Generator
from sklearn.linear_model import SGDClassifier
def _create_train_test_idx(
n: int,
frac_training: float=2./3.,
shuffle: bool=False,
seed: int=1234
) -> tuple[float]:
"""Create training and test indeces for a dataset
with 'n' samples.
The indeces indicate which of the 'n' data samples
should be dedicated as trainig and test data.
Args:
n (int): How many samples are in the dataset?
frac_training (float, optional): Fraction of
samples used for training. Defaults to 2./3..
shuffle (bool, optional): Whether to shuffle
index before creating the training / test split.
Defaults to False.
seed (int, optional): Random seed for shuffling.
Defaults to 1234.
Returns:
tuple of training and test indeces
"""
idx = np.arange(n)
if shuffle:
np.random.seed(seed)
np.random.shuffle(idx)
split = int(frac_training * idx.size)
return (idx[:split], idx[split:])
def _yield_cv_train_val_idx(
n: int,
folds: int=3,
shuffle: bool=False,
seed: int=1234
) -> Generator[int, tuple[float], None]:
"""Yield cross-validation indeces for training and validation
datasets for each cross-validation fold, given a dataset
with 'n' samples.
Args:
n (int): How many samples are in the dataset?
folds (int, optional): Number of cross-validation folds.
Defaults to 3.
shuffle (bool, optional): Whether to shuffle
index before creating the training / test split.
Otherwise data are separated into chunks in sequential order.
Defaults to False.
seed (int, optional): Random seed for shuffling.
Defaults to 1234.
Raises:
ValueError: 'n' needs to larger than 'folds'
Yields:
Generator[int, tuple[float], None]: training and validation
index for each fold
"""
idx = np.arange(n)
if idx.size < folds:
raise ValueError(
'n needs to be larger than folds.'
)
if shuffle:
np.random.seed(seed)
np.random.shuffle(idx)
n_val = int(n / folds)
for fold in range(folds):
idx_val = idx[n_val*fold:n_val*(fold+1)]
idx_train = np.array([i for i in idx if i not in idx_val])
yield (idx_train, idx_val)
def cross_validate(
X: np.ndarray,
y: np.array,
clf: SGDClassifier,
steps: int,
folds: int=3,
) -> pd.DataFrame:
"""Cross-validate classifier performance.
Args:
X (np.ndarray): Input data with shape (samples x features)
y (np.array): Data labels with shape (samples)
clf (sklearn classifier): Classifier to use. Classifier needs
to be compatible with sklearn'S classifier specification
and implement {.score, .partial_fit} methods.
Defaults to sklearns 'SGDClassifier'.
steps (int): How many training steps?
Each step represents one call to .partial_fit
folds (int, optional): Number of cross-validation
folds. Defaults to 3.
Returns:
pd.DataFrame: Cross-validation results
"""
cv_results = []
cv_iterator = _yield_cv_train_val_idx(
n=y.size,
folds=folds
)
for i, (idx_cv_train, idx_cv_val) in enumerate(cv_iterator):
for _ in range(steps):
clf.partial_fit(
X=X[idx_cv_train],
y=y[idx_cv_train],
classes=np.unique(y)
)
score = clf.score(
X=X[idx_cv_val],
y=y[idx_cv_val],
)
res = {
'CV-Fold': i,
'Regularisation (Alpha)': clf.get_params()['alpha'],
'Learning Rate': clf.get_params()['eta0'],
'Steps': steps,
'Random State': clf.get_params()['random_state'],
'Accuracy' : score
}
cv_results.append(
pd.DataFrame(res, index=[i])
)
cv_results = pd.concat(cv_results)
cv_results['Mean Accuracy'] = cv_results['Accuracy'].mean()
return cv_results
def evaluate_hyper_params_effects(
X: np.ndarray,
y: np.array,
classifier: SGDClassifier,
hyper_params: dict,
folds: int=3,
n_jobs=2,
default_classifier_args: dict={
'alpha': 0.0001,
'eta0': 0.1,
'steps': 50,
'random_state': 999
}
) -> pd.DataFrame:
"""Evaluate effect of idividually varying {alpha, eta0, steps}
(while holding everything else constant ) on classifier
performance in {X, y}.
Classifier performance is estimated by the use of cross-validation.
Args:
X (np.ndarray): Input data with shape (samples x features)
y (np.array): Data labels with shape (samples)
classifier (sklearn classifier): Classifier to use. Classifier needs to be
compatible with sklearn's classifier specification and
implement {.score, .partial_fit} methods.
Defaults to sklearns 'SGDClassifier'.
hyper_params (dict): Hyper-parameter values to
evaluate; can be created by calling
hyper.sample_hyper_params()
folds (int, optional): Number of cross-validation folds.
Defaults to 3.
n_jobs (int, optional): How many parallel jobs for fitting?
Defaults to 2.
default_classifier_args (dict, optional): Default classifier arguments
that are used while individual factors are varied.
Defaults to { 'alpha': 0.0001,
'eta0': 0.1,
'steps': 50,
'random_state': 999 }.
Returns:
pd.DataFrame: Cross-validation results
"""
i, results = 0, []
for par, values in hyper_params.items():
for val in values:
clf_args = dict(default_classifier_args)
clf_args[par] = val
clf = classifier(
**{k: clf_args[k] for k in ["alpha", "eta0", "random_state"]},
shuffle=True,
learning_rate='constant',
max_iter=clf_args["steps"],
n_jobs=n_jobs
)
cv_results = cross_validate(
X=X,
y=y,
clf=clf,
steps=clf_args["steps"],
folds=folds
)
cv_results = cv_results.set_index(np.arange(i,i+folds))
i += 1
cv_results["Varied"] = ' '.join(par.capitalize().split('_')) if '_' in par else par.capitalize()
if par == 'eta0':
cv_results["Varied"] = "Learning rate"
elif par == 'alpha':
cv_results["Varied"] = "Regularisation"
results.append(cv_results)
return pd.concat(results)