-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment2_class_2.py
281 lines (232 loc) · 8.77 KB
/
assignment2_class_2.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
# -*- coding: utf-8 -*-
"""
@author: Miry
"""
import xlrd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import svd
from scipy import stats
from scipy.io import loadmat
from matplotlib.pyplot import figure, plot, title, colorbar, imshow, xlabel, xticks, yticks, ylabel, show, legend
from matplotlib.pyplot import boxplot, subplot, hist, ylim
import numpy as np
from sklearn import tree
from platform import system
from os import getcwd
from toolbox_02450 import windows_graphviz_call
import matplotlib.pyplot as plt
from matplotlib.image import imread
import sklearn.linear_model as lm
from sklearn import model_selection
from sklearn.neighbors import KNeighborsClassifier, DistanceMetric
from sklearn.metrics import confusion_matrix
from numpy import cov
from toolbox_02450 import rlr_validate
from sklearn.preprocessing import StandardScaler
from assignment2_regression_data import X, Y2, attributeNames
def knn_validate(X,y,lambdas,cvf):
''' Validate regularized linear regression model using 'cvf'-fold cross validation.
Find the optimal lambda (minimizing validation error) from 'lambdas' list.
The loss function computed as mean squared error on validation set (MSE).
Function returns: MSE averaged over 'cvf' folds, optimal value of lambda,
average weight values for all lambdas, MSE train&validation errors for all lambdas.
The cross validation splits are standardized based on the mean and standard
deviation of the training set when estimating the regularization strength.
Parameters:
X training data set
y vector of values
lambdas vector of lambda values to be validated
cvf number of crossvalidation folds
Returns:
opt_val_err validation error for optimum lambda
opt_lambda value of optimal lambda
mean_w_vs_lambda weights as function of lambda (matrix)
train_err_vs_lambda train error as function of lambda (vector)
test_err_vs_lambda test error as function of lambda (vector)
'''
CV = model_selection.KFold(cvf, shuffle=True)
M = X.shape[1]
w = np.empty((M,cvf,len(lambdas)+1))
train_error = np.empty((cvf,len(lambdas)+1))
test_error = np.empty((cvf,len(lambdas)+1))
f = 0
y = y.squeeze()
for train_index, test_index in CV.split(X,y):
X_train = X[train_index]
y_train = y[train_index]
X_test = X[test_index]
y_test = y[test_index]
# Standardize the training and set set based on training set moments
mu = np.mean(X_train[:, 1:], 0)
sigma = np.std(X_train[:, 1:], 0)
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
for l in range(1,len(lambdas)+1):
mdl = KNeighborsClassifier(n_neighbors=l)
mdl.fit(X_train, y_train)
y_train_est = mdl.predict(X_train)
y_test_est = mdl.predict(X_test)
train_error[f,l] = np.sum(y_train_est != y_train) / len(y_train)
test_error[f,l] = np.sum(y_test_est != y_test) / len(y_test)
f = f+1
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))
return opt_val_err, opt_lambda, mean_w_vs_lambda, train_err_vs_lambda, test_err_vs_lambda, test_error
#########################################################################################################
#Fit logistic regression model
y= X[:,15]
x_l=Y2[:, :15]
X=X[:, :15]
N,M=X.shape
K=10
misclass_rate = np.empty((K,1))
output_knn = np.empty((K,1))
acc = np.empty((K,1))
CV = model_selection.KFold(K, shuffle=True)
# Values of lambda
#lambdas = np.power(10.,range(-5,9))
L=40
knn= np.arange(1,L+1)
#I don't spend time change the variables so lambdas here is fore numbers of nearest neighbors
lambdas = knn
k=0
scaler = StandardScaler()
for train_index, test_index in CV.split(X,y):
dy = []
yhat= []
y_true=[]
# 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
print('internal cross validation')
opt_val_err, opt_lambda, mean_w_vs_lambda, train_err_vs_lambda, test_err_vs_lambda, test_error = knn_validate(X_train, y_train, lambdas, internal_cross_validation)
output_knn[k]=opt_lambda
# 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
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = KNeighborsClassifier(n_neighbors=opt_lambda);
model=model.fit(X_train, y_train);
# Classify horses and assess probabilities
y_est_knn = model.predict(X_test)
# y_est_surg_prob = model.predict_proba(x_test)[:, 0]
# Evaluate classifier's misclassification rate over entire training data
misclass_rate[k] = np.sum(y_est_knn != y_test) / float(len(y_est_knn))
dy.append( y_est_knn )
# errors[i,l-1] = np.sum(y_est[0]!=y_test[0])
dy = np.stack(dy, axis=1)
yhat.append(dy)
y_true.append(y_test)
yhat = np.concatenate(yhat)
y_true = np.concatenate(y_true)
yhat[:] # predictions made by first classifier.
# Compute accuracy here.
acc[k]= np.sum(yhat[:] != y_true)
k+=1
#################################################################
acc_knn=acc.mean()
y_true_knn=y_true
#array([1., 0., 0., 1., 0., 0., 0., 1., 0., 1., 1., 0., 0., 1., 0., 1., 0.,
# 0., 0., 1., 1., 1., 1., 0., 1.])
############################################
print("you are awesome")
## Display classification results
#print('\nProbability of given sample needing surgery: {0:.4f}'.format(x_class))
#print('\nOverall misclassification rate: {0:.3f}'.format(misclass_rate))
#
# f = figure();
# class0_ids = np.nonzero(y_l==0)[0].tolist()
# plot(class0_ids, y_est_surg_prob[class0_ids], '.y')
# class1_ids = np.nonzero(y_l==1)[0].tolist()
# plot(class1_ids, y_est_surg_prob[class1_ids], '.r')
# xlabel('Data object (horse sample)'); ylabel('Predicted prob. of surg');
# legend(['yes', 'no'])
# ylim(-0.01,1.5)
#
# show()
#################################################################
## Maximum number of neighbors
#
#y_knn=y_l
#N = len(y_knn)
#L=40
#x_knn= Y2[:,2:4]
#CV = model_selection.LeaveOneOut()
#errors = np.zeros((N,L))
#i=0
#
#for train_index, test_index in CV.split(x_knn, y_knn):
#
# # extract training and test set for current CV fold
# X_train = x_knn[train_index,:]
# y_train = y_knn[train_index]
# X_test = x_knn[test_index,:]
# y_test = y_knn[test_index]
#
# # Fit classifier and classify the test points (consider 1 to 40 neighbors)
# for l in range(1,L+1):
# knclassifier = KNeighborsClassifier(n_neighbors=l);
# knclassifier.fit(X_train, y_train);
# y_est = knclassifier.predict(X_test);
# errors[i,l-1] = np.sum(y_est[0]!=y_test[0])
#
# i+=1
#
## Plot the classification error rate
#figure()
#plot(100*sum(errors,0)/N)
#xlabel('Number of neighbors')
#ylabel('Classification error rate (%)')
#show()
#
#print('Ran Exercise 6.3.2')
#
#
#C = len(np.array([ 'yes', 'no']))
#
#
## K-nearest neighbors
#K=30
#
## Distance metric (corresponds to 2nd norm, euclidean distance).
## You can set dist=1 to obtain manhattan distance (cityblock distance).
#dist=2
#metric = 'minkowski'
#metric_params = {} # no parameters needed for minkowski
#
## You can set the metric argument to 'cosine' to determine the cosine distance
##metric = 'cosine'
##metric_params = {} # no parameters needed for cosine
#
## To use a mahalonobis distance, we need to input the covariance matrix, too:
##metric='mahalanobis'
##metric_params={'V': cov(X_train, rowvar=False)}
#
## Fit classifier and classify the test points
#knclassifier = KNeighborsClassifier(n_neighbors=K, p=dist,
# metric=metric,
# metric_params=metric_params)
#knclassifier.fit(X_train, y_train)
#y_est = knclassifier.predict(X_test)
#
#
## Plot the classfication results
#figure()
#styles = ['ob', 'or']
#for c in range(1):
# class_mask = (y_est==c)
# plot(X_test[class_mask,0], X_test[class_mask,1], styles[c], markersize=10)
# plot(X_test[class_mask,0], X_test[class_mask,1], 'kx', markersize=8)
#title('Synthetic data classification - KNN');
#
#
#print('Ran Exercise 6.3.1')