-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilding_model.py
275 lines (226 loc) · 10.1 KB
/
building_model.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
import numpy as np
import pandas as pd
import scipy.stats as stats
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.linear_model import Lasso, Ridge
from sklearn.svm import SVR, SVC
from sklearn.model_selection import train_test_split, GroupShuffleSplit, ShuffleSplit, permutation_test_score
from sklearn.metrics import mean_absolute_error, r2_score, mean_squared_error, accuracy_score
def split_data(X,Y,group,procedure):
"""
Split the data according to the group parameters
to ensure that the train and test sets are completely independent
Parameters
----------
X: predictive variable
Y: predicted variable
group: group labels used for splitting the dataset
procedure: strategy to split the data
Returns
----------
X_train: train set containing the predictive variable
X_test: test set containing the predictive variable
y_train: train set containing the predicted variable
y_test: test set containing the predicted variable
"""
X_train = []
y_train = []
X_test = []
y_test = []
for train_idx, test_idx in procedure.split(X, Y, group):
X_train.append(X[train_idx])
X_test.append(X[test_idx])
y_train.append(Y[train_idx])
y_test.append(Y[test_idx])
return X_train, X_test, y_train, y_test
def verbose(splits, X_train, X_test, y_train, y_test, X_verbose = True, y_verbose = True):
"""
Print the mean and the standard deviation of the train and test sets
Parameters
----------
splits: number of splits used for the cross-validation
X_train: train set containing the predictive variable
X_test: test set containing the predictive variable
y_train: train set containing the predicted variable
y_test: test set containing the predicted variable
X_verbose (boolean): if X_verbose == True, print the descriptive stats for the X (train and test)
y_verbose (boolean): if y_verbose == True, print the descriptive stats for the y (train and test)
"""
for i in range(splits):
if X_verbose:
print(i,'X_Train: \n Mean +/- std = ', X_train[i][:][:].mean(),'+/-', X_train[i][:][:].std())
print(i,'X_Test: \n Mean +/- std = ', X_test[i][:][:].mean(),'+/-', X_test[i][:][:].std())
if y_verbose:
print(i,'y_Train: \n Mean +/- std = ', y_train[i][:].mean(),'+/-', y_train[i][:].std(), '\n Skew = ', stats.skew(y_train[i][:]), '\n Kurt = ', stats.kurtosis(y_train[i][:]))
print(i,'y_Test: \n Mean +/- std = ', y_test[i][:].mean(),'+/-', y_test[i][:].std(), '\n Skew = ', stats.skew(y_test[i][:]), '\n Kurt = ', stats.kurtosis(y_test[i][:]))
print('\n')
def compute_metrics(y_test, y_pred, df, fold, print_verbose):
"""
Compute different metrics and print them
Parameters
----------
y_test: ground truth
y_pred: predicted values
df: dataFrame containing the result of the metrics
fold: cross-validation fold for which the metrics are computed
Returns
----------
df_metrics: dataFrame containing the different metrics
"""
r2 = r2_score(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
df.loc[fold] = [r2, mae, mse, rmse]
if print_verbose:
print('------Metrics for fold {}------'.format(fold))
print('R2 value = {}'.format(r2))
print('MAE value = {}'.format(mae))
print('MSE value = {}'.format(mse))
print('RMSE value = {}'.format(rmse))
print('\n')
return df
def reg_PCA(n_component, reg = Lasso()):
"""
Parameters
----------
n_component: number of components to keep in the PCA
Returns
----------
pipe: pipeline to apply PCA and Lasso regression sequentially
"""
estimators = [('reduce_dim', PCA(n_component)), ('clf', reg)]
pipe = Pipeline(estimators)
return pipe
def train_test_model(X, y, gr, reg=Lasso(), splits=5,test_size=0.3, n_components=0.80, random_seed=42, print_verbose=True):
"""
Build and evaluate a regression model
First compute the PCA and then fit the regression technique specified on the PCs scores
Parameters
----------
X: predictive variable
y: predicted variable
gr: grouping variable
reg: regression technique to perform
splits: number of split for the cross-validation
test_size: percentage of the data in the test set
n_components: number of components to keep for the PCA
random_seed: controls the randomness of the train/test splits
print_verbose: either or not the verbose is printed
Returns
----------
X_train: list containing the training sets of the predictive variable
y_train: list containing the training sets of the predictive variable
X_test: list containing the training sets of the predictive variable
y_test: list containing the training sets of the predictive variable
y_pred: list containing the predicted values for each fold
model_voxel: list of arrays containing the coefficients of the model in the voxel space
df_metrics: DataFrame containing different metrics for each fold
"""
#Initialize the variables
y_pred = []
model = []
model_voxel = []
df_metrics = pd.DataFrame(columns=["r2", "mae", "mse", "rmse"])
#Strategy to split the data
if gr == None:
shuffle_method = ShuffleSplit(n_splits = splits, test_size = test_size, random_state = random_seed)
X_train, X_test, y_train, y_test = split_data(X, y, procedure=shuffle_method)
else:
shuffle_method = GroupShuffleSplit(n_splits = splits, test_size = test_size, random_state = random_seed)
X_train, X_test, y_train, y_test = split_data(X, y, gr, shuffle_method)
if print_verbose:
verbose(splits, X_train, X_test, y_train, y_test, X_verbose = True, y_verbose = True)
for i in range(splits):
###Build and test the model###
print("----------------------------")
print("Training model")
model_reg = reg_PCA(n_components,reg=reg)
model.append(model_reg.fit(X_train[i], y_train[i]))
y_pred.append(model[i].predict(X_test[i]))
###Scores###
df_metrics = compute_metrics(y_test[i], y_pred[i], df_metrics, i, print_verbose)
model_voxel.append(model[i][0].inverse_transform(model[i][1].coef_))
df_metrics.to_csv("dataframe_metrics.csv")
return X_train, y_train, X_test, y_test, y_pred, model, model_voxel
def train_test_classify(X, y, gr, C=1.0):
"""
Build and evaluate a classification model
Parameters
----------
X: predictive variable
y: predicted variable (binary variable)
gr: grouping variable
C: regularization parameter
Returns
----------
model: list containing the classifier model for each fold
accuracy: list containing the classifier accuracy across the folds
See also scikit-learn SVC documentation
"""
#Initialize the variables
y_pred = []
model = []
accuracy = []
#Strategy to split the data
if gr == None:
shuffle_method = ShuffleSplit(n_splits = splits, test_size = test_size, random_state = random_seed)
X_train, X_test, y_train, y_test = split_data(X, y, shuffle_method)
else:
shuffle_method = GroupShuffleSplit(n_splits = splits, test_size = test_size, random_state = random_seed)
X_train, X_test, y_train, y_test = split_data(X, y, gr, shuffle_method)
for i in range(splits):
###Build and test the model###
print("----------------------------")
print("Training model")
model_clf = SVC(C=C, kernel="linear")
model.append(model_clf.fit(X_train[i], y_train[i]))
y_pred.append(model[i].predict(X_test[i]))
###Scores###
accuracy.append(accuracy_score(y_test, y_pred))
return X_train, y_train, X_test, y_test, y_pred, model, accuracy
def compute_permutation(X, y, gr, reg, n_components=0.80, n_permutations=5000, scoring="r2", random_seed=42):
"""
Compute the permutation test for a specified metric (r2 by default)
Apply the PCA after the splitting procedure
Parameters
----------
X: predictive variable
y: predicted variable
gr: grouping variable
n_components: number of components to keep for the PCA
n_permutations: number of permuted iteration
scoring: scoring strategy
random_seed: controls the randomness
Returns
----------
score (float): true score
perm_scores (ndarray): scores for each permuted samples
pvalue (float): probability that the true score can be obtained by chance
See also scikit-learn permutation_test_score documentation
"""
if gr == None:
cv = ShuffleSplit(n_splits = 5, test_size = 0.3, random_state = random_seed)
else:
cv = GroupShuffleSplit(n_splits = 5, test_size = 0.3, random_state = random_seed)
score, perm_scores, pvalue = permutation_test_score(estimator=reg_PCA(n_components,reg=reg), X=X, y=y, groups= gr, scoring=scoring, cv=cv, n_permutations=n_permutations, random_state=42)
return score, perm_scores, pvalue
def predict_on_test(X_train, y_train, X_test, y_test, reg):
"""
Test the generability of a regression model on left out data
Parameters
----------
X_train: predictive variable to fit the model
y_train: predicted variable to fit the model
X_test: predictive variable to predict the model
y_test: predicted variable to predict the model
reg: regression technique to perform
Returns
----------
r2: metric to evaluate the performance of the model
"""
df_metrics = pd.DataFrame(columns=["r2", "mae", "mse", "rmse"])
final_model = reg.fit(X_train, y_train)
r2 = r2_score(y_test, final_model.predict(X_test))
return r2