-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm_regressionA.py
302 lines (241 loc) · 10.9 KB
/
mm_regressionA.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 11:24:31 2020
@author: miriam
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mm_1 import *
from ann_validate import *
import seaborn as sns; sns.set()
from sklearn import preprocessing
from matplotlib.pyplot import figure, subplot, hist, xlabel, xticks, xlim, ylim, show, boxplot, bar, legend, clim
from matplotlib.pylab import (figure, semilogx, loglog, xlabel, ylabel, legend, title, subplot, show, grid)
from scipy.stats import zscore
import sklearn.linear_model as lm
from sklearn import model_selection, tree
from toolbox_02450 import feature_selector_lr, bmplot
from toolbox_02450 import rlr_validate
# Extract vector y, convert to NumPy array
y_c = np.asarray([classDict[value] for value in classLabels])
#REGRESSION
y_r=df['Score'].values
# Compute values of N, M and C.
N = len(y)
M = len(attributeNames)
C = len(classNames)
# separate the data from the target attributes
varplot= attributeNames
# normalize the data attributes
#Normalization refers to rescaling real valued numeric attributes into the range 0 and 1.
#It is useful to scale the input attributes for a model that relies on
#the magnitude of values, such as distance measures used in
#k-nearest neighbors and in the preparation of coefficients in regression.
X_n = preprocessing.normalize(X)
# standardize the data attributes
#Standardization refers to shifting the distribution of each attribute to have
#a mean of zero and a standard deviation of one (unit variance).
#X_std = preprocessing.scale(X)
X_std=zscore(X, ddof=1)
#########################################################
X=X_n
y=y_r
# Values of lambda
lambdas = np.power(10.,range(-5,9))
#lambdas = np.logspace(-10, -1, 50)
K=10
CV = model_selection.KFold(K, shuffle=True)
Error_train = np.empty((K,1))
Error_test = np.empty((K,1))
Error_train_rlr = np.empty((K,len(lambdas)))
Error_test_rlr = np.empty((K,len(lambdas)))
Error_train_nofeatures = np.empty((K,1))
Error_test_nofeatures = np.empty((K,1))
mu = np.empty((K, M-1))
sigma = np.empty((K, M-1))
w_rlr = np.empty((M,K))
w_noreg = np.empty((M,K))
w = np.empty((M,K,len(lambdas)))
train_err_vs_lambda= np.empty((K,1))
test_err_vs_lambda =np.empty((K,1))
mean_w_vs_lambda=np.empty((K,1))
N_k=np.empty((K,1))
k=0
for train_index, test_index in CV.split(X,y):
# extract training and test set for current CV fold
X_train = X[train_index]
y_train = y[train_index]
X_test = X[test_index]
y_test = y[test_index]
N_k[k]=X_test.shape[0]
# Standardize outer fold based on training set, and save the mean and standard
# deviations since they're part of the model
mu[k, :] = np.mean(X_train[:, 1:], 0)
sigma[k, :] = np.std(X_train[:, 1:], 0)
X_train[:, 1:] = (X_train[:, 1:] - mu[k, :] ) / sigma[k, :]
X_test[:, 1:] = (X_test[:, 1:] - mu[k, :] ) / sigma[k, :]
Xty = X_train.T @ y_train
XtX = X_train.T @ X_train
# Compute mean squared error without using the input data at all
Error_train_nofeatures[k] = np.square(y_train-y_train.mean()).sum(axis=0)/y_train.shape[0]
Error_test_nofeatures[k] = np.square(y_test-y_test.mean()).sum(axis=0)/y_test.shape[0]
for l in range(0,len(lambdas)):
# Estimate weights for the optimal value of lambda, on entire training set
lambdaI = lambdas[l]* np.eye(M)
lambdaI[0,0] = 0 # Do no regularize the bias term
w[:,k,l] = np.linalg.solve(XtX+lambdaI,Xty).squeeze()
# Evaluate training and test performance
Error_train_rlr[k,l] = np.power(y_train-X_train @ w[:,k,l].T,2).mean(axis=0)
Error_test_rlr[k,l] = np.power(y_test-X_test @ w[:,k,l].T,2).mean(axis=0)
# opt_val_err = np.min(np.mean(test_error,axis=0))
# opt_lambda = lambdas[np.argmin(np.mean(test_error,axis=0))]
# train_err_vs_lambda = np.mean(train_error,axis=0)
# test_err_vs_lambda = np.mean(test_error,axis=0)
# mean_w_vs_lambda = np.squeeze(np.mean(w,axis=1))
# Estimate weights for unregularized linear regression, on entire training set
w_noreg[:,k] = np.linalg.solve(XtX,Xty).squeeze()
# Compute mean squared error without regularization
Error_train[k] = np.square(y_train-X_train @ w_noreg[:,k]).sum(axis=0)/y_train.shape[0]
Error_test[k] = np.square(y_test-X_test @ w_noreg[:,k]).sum(axis=0)/y_test.shape[0]
# OR ALTERNATIVELY: you can use sklearn.linear_model module for linear regression:
#m = lm.LinearRegression().fit(X_train, y_train)
#Error_train[k] = np.square(y_train-m.predict(X_train)).sum()/y_train.shape[0]
#Error_test[k] = np.square(y_test-m.predict(X_test)).sum()/y_test.shape[0]
# ### Compute squared error with all features selected (no feature selection)
# m = lm.LinearRegression(fit_intercept=True).fit(X_train, y_train)
# Error_train[k] = np.square(y_train-m.predict(X_train)).sum()/y_train.shape[0]
# Error_test[k] = np.square(y_test-m.predict(X_test)).sum()/y_test.shape[0]
k+=1
gen_err=np.empty((len(lambdas),1))
gen_err2=np.empty((len(lambdas),1))
for i in range(0,len(lambdas)):
gen_err2[i] = Error_test_rlr[:,i].mean()
gen_err[i] = Error_test_rlr[:,i].sum()*(12/127)
min_gen_err = gen_err.min()
optimum_lambda = lambdas[np.argmin(gen_err)]
train_err=np.empty((len(lambdas),1))
for i in range(0,len(lambdas)):
train_err[i] = Error_train_rlr[:,i].mean()
figure( figsize=(12,8))
title('Generalization error')
loglog(lambdas,gen_err, 'r.-', lambdas,train_err,'b')
xlabel('Regularization factor (λ)')
ylabel('Generalization error (crossvalidation)')
legend(['Generalization error','Train error'])
grid()
opt_model=lambdas[np.where(gen_err==gen_err.min())[0]]
# Fit ordinary least squares regression model
model = lm.Ridge(alpha=10)
model.fit(X,y)
# Predict alcohol content
y_est = model.predict(X)
residual = y_est-y
# Display scatter plot
figure()
subplot(2,1,1)
plot(y, y_est, '.')
xlabel('Happiness score (true)'); ylabel('HAppiness score (estimated)');
subplot(2,1,2)
hist(residual,40)
show()
#########################################################
X=X_n
y=y_r
# Add offset attribute
X = np.concatenate((np.ones((X.shape[0],1)),X),1)
attributeNames = [u'Offset']+attributeNames
M = M+1
## Crossvalidation
# Create crossvalidation partition for evaluation
K = 10
CV = model_selection.KFold(K, shuffle=True)
#CV = model_selection.KFold(K, shuffle=False)
# Values of lambda
lambdas = np.power(10.,range(-5,9))
# Initialize variables
#T = len(lambdas)
Error_train = np.empty((K,1))
Error_test = np.empty((K,1))
Error_train_rlr = np.empty((K,1))
Error_test_rlr = np.empty((K,1))
Error_train_nofeatures = np.empty((K,1))
Error_test_nofeatures = np.empty((K,1))
opt_lambda= np.empty((K,1))
w_rlr = np.empty((M,K))
mu = np.empty((K, M-1))
sigma = np.empty((K, M-1))
w_noreg = np.empty((M,K))
k=0
for train_index, test_index in CV.split(X,y):
# extract training and test set for current CV fold
X_train = X[train_index]
y_train = y[train_index]
X_test = X[test_index]
y_test = y[test_index]
internal_cross_validation = 10
opt_val_err, opt_lambda[k], mean_w_vs_lambda, train_err_vs_lambda, test_err_vs_lambda = rlr_validate(X_train, y_train, lambdas, internal_cross_validation)
# Standardize outer fold based on training set, and save the mean and standard
# deviations since they're part of the model (they would be needed for
# making new predictions) - for brevity we won't always store these in the scripts
mu[k, :] = np.mean(X_train[:, 1:], 0)
sigma[k, :] = np.std(X_train[:, 1:], 0)
X_train[:, 1:] = (X_train[:, 1:] - mu[k, :] ) / sigma[k, :]
X_test[:, 1:] = (X_test[:, 1:] - mu[k, :] ) / sigma[k, :]
Xty = X_train.T @ y_train
XtX = X_train.T @ X_train
# Compute mean squared error without using the input data at all
Error_train_nofeatures[k] = np.square(y_train-y_train.mean()).sum(axis=0)/y_train.shape[0]
Error_test_nofeatures[k] = np.square(y_test-y_test.mean()).sum(axis=0)/y_test.shape[0]
# Estimate weights for the optimal value of lambda, on entire training set
lambdaI = opt_lambda[k] * np.eye(M)
lambdaI[0,0] = 0 # Do no regularize the bias term
w_rlr[:,k] = np.linalg.solve(XtX+lambdaI,Xty).squeeze()
# Compute mean squared error with regularization with optimal lambda
Error_train_rlr[k] = np.square(y_train-X_train @ w_rlr[:,k]).sum(axis=0)/y_train.shape[0]
Error_test_rlr[k] = np.square(y_test-X_test @ w_rlr[:,k]).sum(axis=0)/y_test.shape[0]
# Estimate weights for unregularized linear regression, on entire training set
w_noreg[:,k] = np.linalg.solve(XtX,Xty).squeeze()
# Compute mean squared error without regularization
Error_train[k] = np.square(y_train-X_train @ w_noreg[:,k]).sum(axis=0)/y_train.shape[0]
Error_test[k] = np.square(y_test-X_test @ w_noreg[:,k]).sum(axis=0)/y_test.shape[0]
# OR ALTERNATIVELY: you can use sklearn.linear_model module for linear regression:
#m = lm.LinearRegression().fit(X_train, y_train)
#Error_train[k] = np.square(y_train-m.predict(X_train)).sum()/y_train.shape[0]
#Error_test[k] = np.square(y_test-m.predict(X_test)).sum()/y_test.shape[0]
# Display the results for the last cross-validation fold
if k == K-1:
figure(k, figsize=(12,8))
subplot(1,2,1)
semilogx(lambdas,mean_w_vs_lambda.T[:,1:],'.-') # Don't plot the bias term
xlabel('Regularization factor')
ylabel('Mean Coefficient Values')
grid()
# You can choose to display the legend, but it's omitted for a cleaner
# plot, since there are many attributes
#legend(attributeNames[1:], loc='best')
subplot(1,2,2)
title('Optimal lambda: 1e{0}'.format(np.log10(opt_lambda[k])))
loglog(lambdas,train_err_vs_lambda.T,'b.-',lambdas,test_err_vs_lambda.T,'r.-')
xlabel('Regularization factor')
ylabel('Squared error (crossvalidation)')
legend(['Train error','Validation error'])
grid()
int('Test indices: {0}\n'.format(test_index))
k+=1
show()
# Display results
print('Linear regression without feature selection:')
print('- Training error: {0}'.format(Error_train.mean()))
print('- Test error: {0}'.format(Error_test.mean()))
print('- R^2 train: {0}'.format((Error_train_nofeatures.sum()-Error_train.sum())/Error_train_nofeatures.sum()))
print('- R^2 test: {0}\n'.format((Error_test_nofeatures.sum()-Error_test.sum())/Error_test_nofeatures.sum()))
print('Regularized linear regression:')
print('- Training error: {0}'.format(Error_train_rlr.mean()))
print('- Test error: {0}'.format(Error_test_rlr.mean()))
print('- R^2 train: {0}'.format((Error_train_nofeatures.sum()-Error_train_rlr.sum())/Error_train_nofeatures.sum()))
print('- R^2 test: {0}\n'.format((Error_test_nofeatures.sum()-Error_test_rlr.sum())/Error_test_nofeatures.sum()))
print('Weights in last fold:')
for m in range(M):
print('{:>15} {:>15}'.format(attributeNames[m], np.round(w_rlr[m,-1],2)))
###################################################################################