-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression.py
372 lines (281 loc) · 12 KB
/
regression.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
"""
HousingData.csv
attributes :
crim : per capita crime rate by town
zn : proportion of residential land zoned for lots over 25,000 sq.ft.
indus
proportion of non-retail business acres per town.
chas
Charles River dummy variable (= 1 if tract bounds river; 0 otherwise).
nox
nitrogen oxides concentration (parts per 10 million).
rm
average number of rooms per dwelling.
age
proportion of owner-occupied units built prior to 1940.
dis
weighted mean of distances to five Boston employment centres.
rad
index of accessibility to radial highways.
tax
full-value property-tax rate per \$10,000.
ptratio
pupil-teacher ratio by town.
black
1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town.
lstat
lower status of the population (percent).
medv (variable to explain)
median value of owner-occupied homes in \$1000s
"""
import numpy as np
import pandas as pd
from sklearn import *
import math
import matplotlib.pyplot as plt
import copy as cp
from sklearn.linear_model import LinearRegression
from sklearn.utils import shuffle
from scipy.stats import norm
# print(X['MEDV'])
def get_raw_data(csv_path, variable_to_explain_name=None, prop_test_data=0.05, replace_missing_method=1):
#Thibaut
"""read the data from the csv, replace missing values, shuffle the lines and return the input matrix X
and its associated values y
y contains column variable_to_explain_name and X all the other columns
if variable_to_explain_name is none it is set to the last column
"""
data = pd.read_csv(csv_path)
data = shuffle(data)
if variable_to_explain_name is None:
variable_to_explain_name = data.columns[-1]
y = data[variable_to_explain_name]
X = data[[c for c in data.columns if c != variable_to_explain_name]]
X, attribute_names = one_hot_encode(X)
X = X.values
y = y.values
replace_missing_values(X, replace_missing_method)
n_train = int((1 - prop_test_data) * X.shape[0])
X_train, y_train, X_test, y_test = X[:n_train, :], y[:n_train], X[n_train:, :], y[n_train:]
return X_train, y_train, X_test, y_test, np.array(attribute_names)
def c_type(df, column):
#Sanaa
"""returns the type of data in column (the time of the first non NaN element"""
i = 0
data_array = df[column].values
while i < len(data_array):
if type(data_array[i]) == str: return str
if math.isnan(data_array[i]) == False:
c_type = type(data_array[i])
break
i = i + 1
return c_type
def one_hot_encode(df):
#Sanaa
"""detects and one hote encodes the categorical attributes of a pandas data set"""
columns = df.columns
#print(columns)
data = df
non_cat = [np.float64, np.int64, np.float32, np.int32]
ys = []
attribute_names = []
for i in columns:
if c_type(df, i) not in non_cat:
y = pd.get_dummies(df[i], prefix=i)
attribute_names += list(y.columns)
data = data.drop(columns=[i])
ys.append(y)
else:
attribute_names.append(i)
L = [data] + ys
result = pd.concat(L, axis=1, ignore_index=True)
return result, attribute_names
def replace_missing_values(data, method=1):
#Thibaut, Anthony
"""replace the missing values by the mean of the corresponding attribute (method 1) or by the value of the closest observation (method 2)"""
if method in [1, 'mean'] :
#Thibaut
for j in range(data.shape[1]):
mean_j = 0
cpt = 0
for i in range(data.shape[0]):
if not math.isnan(data[i][j]):
mean_j += data[i][j]
cpt += 1
mean_j/=cpt
for i in range(data.shape[0]):
if math.isnan(data[i][j]):
data[i][j] = mean_j
elif method in [2, 'closest'] :
#Anthony
for i in range(data.shape[0]) :
present_data = []
for j in range(data.shape[1]) :
if not math.isnan(data[i][j]) :
present_data.append(j)
if len(present_data) < data.shape[1] :
closest = 0
dist_closest = 100000000
for i_ in range(data.shape[0]) :
if i_ != i :
dist = np.sqrt(sum([(data[i][j] - data[i_][j]) **2 for j in present_data]))
if not math.isnan(dist) and dist < dist_closest :
all_missing_data_present = True
for j in range(data.shape[1]) :
if math.isnan(data[i][j]) :
if math.isnan(data[i_][j]) :
all_missing_data_present = False
break
if all_missing_data_present :
closest = i_
dist_closest = dist
for j in range(data.shape[1]) :
if math.isnan(data[i][j]) :
data[i][j] = data[closest][j]
def normalize(X):
#Thibaut
min_max_scaler = preprocessing.MinMaxScaler()
X_scaled = min_max_scaler.fit_transform(X)
return X_scaled, min_max_scaler
def covariance(v1, v2):
#Thibaut
# returns the covariance between the 2 vectors
mean_v1 = np.mean(v1)
mean_v2 = np.mean(v2)
return np.mean([(v1[i] - mean_v1) * (v2[i] - mean_v2) for i in range(len(v1))])
def correlation(v1, v2):
return covariance(v1, v2) / (np.std(v1) * np.std(v2))
def correlation_input_output(X, y):
# return the correlations between the input variables and the output variable
#Thibaut
return np.array([correlation(X[:, i], y) for i in range(X.shape[1])])
def select_features_with_highest_correlation(X, y, n_features=5, title=''):
#Thibaut
"""IN : X : input variables
y : output variable
OUT : - X_prim data array shape (N, n_features) containing the features having the highest correlation
with the output variable
- feature_highest_cor : list of nb_features element containing the indices of the columns of highest correlation
- corresponding correlations
"""
correlations = correlation_input_output(X, y)
abs_corr = np.abs(correlations)
perm_to_sort = abs_corr.argsort() # get the permutation used to sort thearray
perm_to_sort = perm_to_sort[::-1] # reverse
features_highest_cor = perm_to_sort[:n_features]
# print(features_highest_cor)
X_prim = X[:, features_highest_cor]
if title != '':
L = [np.sum(abs_corr[perm_to_sort][:i + 1]) for i in range(len(correlations) - 1)]
L /= np.sum(abs_corr)
plt.plot(L)
plt.xlabel('nb of features')
plt.ylabel('total correlation')
plt.title(title)
plt.show()
return X_prim, features_highest_cor, abs_corr[perm_to_sort][:n_features]
def select_features_with_PCA(X, n_features=5):
#Anthony
#c_X = cp.deepcopy(X)
pca = decomposition.PCA(n_components=n_features)
pca.fit(X)
return pca.transform(X), pca
def train_and_obtain_model(X, y):
#Anthony
model = LinearRegression()
model.fit(X, y)
return model
def L1(y_pred, y):
return np.mean(abs(y_pred - y))
def test_model_with_cross_validation(X, y, n_spits=10):
#Thibaut
from sklearn.model_selection import KFold
kf = KFold(n_splits=n_spits)
total_error = 0
for train_idx, test_idx in kf.split(cp.deepcopy(X)):
X_train, X_test, y_train, y_test = X[train_idx, :], X[test_idx, :], y[train_idx], y[test_idx]
model = train_and_obtain_model(X_train, y_train)
y_pred = model.predict(X_test)
error = L1(y_pred, y_test)
total_error += error
return total_error / n_spits
def plot_error_against_nb_features(X, y, training_set_name, n_max=None, method='highest corr'):
#Anthony
if n_max is None:
n_max = X.shape[1]
errors = []
n_features = [n for n in range(1, n_max)]
for n in range(1, n_max):
if method == 'highest corr':
X_n, _, _ = select_features_with_highest_correlation(X, y, n)
elif method == 'PCA':
X_n, _ = select_features_with_PCA(X, n_features=n)
else:
raise Exception("Argument 'method' is unvalid")
errors.append(test_model_with_cross_validation(X_n, y))
plt.plot(n_features, errors)
plt.xlabel('number of features')
plt.ylabel('l1 error')
plt.title(f'{training_set_name} data set, error against the number of features selected with method {method}')
plt.show()
def choose_nb_features(X, y, alpha=0.99, n_max=None, method='highest corr'):
#Anthony
if n_max is None:
n_max = X.shape[1]
errors = [10 ** 15]
for n in range(1, 4):
if method == 'highest corr':
X_n, _, _ = select_features_with_highest_correlation(X, y, n)
elif method == 'PCA':
X_n, _ = select_features_with_PCA(X, n_features=n)
errors.append(test_model_with_cross_validation(X_n, y))
n = 4
while errors[n - 1] < alpha * errors[n - 4] and n < n_max:
n += 1
if method == 'highest corr':
X_n, _, _ = select_features_with_highest_correlation(X, y, n)
elif method == 'PCA':
X_n, _ = select_features_with_PCA(X, n_features=n)
errors.append(test_model_with_cross_validation(X_n, y))
return n
def predict_new_data(model, X_new):
return model.predict(X_new)
def main(training_set_path):
X_train, y_train, X_test, y_test, attribute_names = get_raw_data(training_set_path, prop_test_data=0.2)
X_train, min_max_scaler = normalize(X_train)
X_test = min_max_scaler.fit_transform(X_test)
for method in ['highest corr', 'PCA']:
print('method', method)
plot_error_against_nb_features(X_train, y_train, training_set_name=training_set_path,method=method)
nb_features = choose_nb_features(X_train, y_train, method=method)
print(f'number of features selected with method {method} : {nb_features}')
if method == 'highest corr':
title = f'total correlation against number of features for {training_set_path}'
X_train_proj, features_highest_cor, abs_cor = select_features_with_highest_correlation(X_train, y_train,
n_features=nb_features,
title=title)
print(f'feature selected are {attribute_names[features_highest_cor]}')
print(f'corresponding absolute correlation with output variable are {abs_cor}')
X_test_proj = X_test[:, features_highest_cor]
else:
X_train_proj, pca = select_features_with_PCA(X_train, n_features=nb_features)
X_test_proj = pca.transform(X_test)
model = train_and_obtain_model(X_train_proj, y_train)
y_pred = model.predict(X_test_proj)
error = L1(y_pred, y_test)
std_dev = np.std(np.abs(y_pred-y_test))
confidence_level = 0.95
qt = norm.ppf((1-confidence_level)/2)
delta = -qt*std_dev/np.sqrt(len(y_test))
print(f'L1 error on test set ({len(y_pred)} data): ', error)
print(f'confidence intervall of L1 error with confidence level {confidence_level} : [{error-delta}, {error+delta}]')
print('*********')
for data_set in ['HousingData.csv', 'spnbmd.csv']:
print(f'processing data set {data_set}')
main(data_set)
print('-----------------------------------')
'''
#X = select_features_with_PCA(X, n_features=1)
error = test_model_with_cross_validation(X, y)
print('the average error (L1 norm) computed with cross validation is : ', error)
'''