-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2_code2.py
793 lines (610 loc) · 26.7 KB
/
project2_code2.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 10 20:33:54 2017
@author: rajaz
"""
import os
import numpy as np
import xlrd
from sklearn import cross_validation, tree
import sklearn.linear_model as lm
from matplotlib.pyplot import figure, plot, subplot, title, xlabel, ylabel, show, clim, boxplot, legend, ylim, hold, imshow, xticks,yticks
from scipy import stats
from toolbox_02450 import feature_selector_lr, bmplot
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
os.chdir("C:\\Users\\rajaz\\OneDrive\\Documents\\Machine\\project_data\\")
doc = xlrd.open_workbook(".\\glass_data.xls").sheet_by_index(0)
attributeNames = doc.row_values(0, 1, 10)
# Extract class names to python list,
# then encode with integers (dict)
classLabels = doc.col_values(10, 1, 214)
classNames = sorted(set(classLabels))
classDict = dict(zip(classNames, range(6)))
y = np.array([classDict[value] for value in classLabels])
# Preallocate memory, then extract excel data to matrix X
X = np.mat(np.empty((213, 9)))
for i, col_id in enumerate(range(1, 10)):
X[:, i] = np.mat(doc.col_values(col_id, 1, 214)).T
# Compute values of N (number of observations), M (number of attributes) and C (classes).
N = len(y)
M = len(attributeNames)
C = len(classNames)
# Tree complexity parameter - constraint on maximum depth
tc = np.arange(2, 21, 1)
# Simple holdout-set crossvalidation
test_proportion = 0.5
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,y,test_size=test_proportion)
# Initialize variables
Error_train = np.empty((len(tc),1))
Error_test = np.empty((len(tc),1))
for i, t in enumerate(tc):
# Fit decision tree classifier, Gini split criterion, different pruning levels
dtc = tree.DecisionTreeClassifier(criterion='gini', max_depth=t)
dtc = dtc.fit(X_train,y_train)
# Evaluate classifier's misclassification rate over train/test data
y_est_test = dtc.predict(X_test)
y_est_train = dtc.predict(X_train)
misclass_rate_test = sum(np.abs(y_est_test - y_test)) / float(len(y_est_test))
misclass_rate_train = sum(np.abs(y_est_train - y_train)) / float(len(y_est_train))
Error_test[i], Error_train[i] = misclass_rate_test, misclass_rate_train
f = figure(); f.hold(True)
plot(tc, Error_train)
plot(tc, Error_test)
xlabel('Model complexity (max tree depth)')
ylabel('Error (misclassification rate)')
legend(['Error_train','Error_test'])
show()
# exercise 6.1.2
from matplotlib.pyplot import figure, plot, xlabel, ylabel, legend, show, boxplot
from scipy.io import loadmat
from sklearn import cross_validation, tree
import numpy as np
# Tree complexity parameter - constraint on maximum depth
tc = np.arange(2, 21, 1)
# K-fold crossvalidation
K = 10
CV = cross_validation.KFold(N,K,shuffle=True)
# Initialize variable
Error_train = np.empty((len(tc),K))
Error_test = np.empty((len(tc),K))
k=0
for train_index, test_index in CV:
print('Computing CV fold: {0}/{1}..'.format(k+1,K))
# extract training and test set for current CV fold
X_train, y_train = X[train_index,:], y[train_index]
X_test, y_test = X[test_index,:], y[test_index]
for i, t in enumerate(tc):
# Fit decision tree classifier, Gini split criterion, different pruning levels
dtc = tree.DecisionTreeClassifier(criterion='gini', max_depth=t)
dtc = dtc.fit(X_train,y_train.ravel())
y_est_test = dtc.predict(X_test)
y_est_train = dtc.predict(X_train)
# Evaluate misclassification rate over train/test data (in this CV fold)
misclass_rate_test = sum(np.abs(y_est_test - y_test)) / float(len(y_est_test))
misclass_rate_train = sum(np.abs(y_est_train - y_train)) / float(len(y_est_train))
Error_test[i,k], Error_train[i,k] = misclass_rate_test, misclass_rate_train
k+=1
f = figure(); f.hold(True)
boxplot(Error_test.T)
xlabel('Model complexity (max tree depth)')
ylabel('Test error across CV folds, K={0})'.format(K))
f = figure(); f.hold(True)
plot(tc, Error_train.mean(1))
plot(tc, Error_test.mean(1))
xlabel('Model complexity (max tree depth)')
ylabel('Error (misclassification rate, CV K={0})'.format(K))
legend(['Error_train','Error_test'])
show()
# exercise 6.2.1
from matplotlib.pyplot import figure, plot, subplot, title, xlabel, ylabel, show, clim
from scipy.io import loadmat
import sklearn.linear_model as lm
from sklearn import cross_validation
from toolbox_02450 import feature_selector_lr, bmplot
import numpy as np
## Crossvalidation
# Create crossvalidation partition for evaluation
K = 5
CV = cross_validation.KFold(N,K,shuffle=True)
# Initialize variables
Features = np.zeros((M,K))
Error_train = np.empty((K,1))
Error_test = np.empty((K,1))
Error_train_fs = np.empty((K,1))
Error_test_fs = np.empty((K,1))
Error_train_nofeatures = np.empty((K,1))
Error_test_nofeatures = np.empty((K,1))
k=0
for train_index, test_index in CV:
# 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
# Compute squared error without using the input data at all
Error_train_nofeatures[k] = np.square(y_train-y_train.mean()).sum()/y_train.shape[0]
Error_test_nofeatures[k] = np.square(y_test-y_test.mean()).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]
# Compute squared error with feature subset selection
#textout = 'verbose';
textout = '';
selected_features, features_record, loss_record = feature_selector_lr(X_train, y_train, internal_cross_validation,display=textout)
Features[selected_features,k]=1
# .. alternatively you could use module sklearn.feature_selection
if len(selected_features) is 0:
print('No features were selected, i.e. the data (X) in the fold cannot describe the outcomes (y).' )
else:
m = lm.LinearRegression(fit_intercept=True).fit(X_train[:,selected_features], y_train)
Error_train_fs[k] = np.square(y_train-m.predict(X_train[:,selected_features])).sum()/y_train.shape[0]
Error_test_fs[k] = np.square(y_test-m.predict(X_test[:,selected_features])).sum()/y_test.shape[0]
figure(k)
subplot(1,2,1)
plot(range(1,len(loss_record)), loss_record[1:])
xlabel('Iteration')
ylabel('Squared error (crossvalidation)')
subplot(1,3,3)
bmplot(attributeNames, range(1,features_record.shape[1]), -features_record[:,1:])
clim(-1.5,0)
xlabel('Iteration')
print('Cross validation fold {0}/{1}'.format(k+1,K))
print('Train indices: {0}'.format(train_index))
print('Test indices: {0}'.format(test_index))
print('Features no: {0}\n'.format(selected_features.size))
k+=1
# Display results
print('\n')
print('Linear regression without feature selection:\n')
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}'.format((Error_test_nofeatures.sum()-Error_test.sum())/Error_test_nofeatures.sum()))
print('Linear regression with feature selection:\n')
print('- Training error: {0}'.format(Error_train_fs.mean()))
print('- Test error: {0}'.format(Error_test_fs.mean()))
print('- R^2 train: {0}'.format((Error_train_nofeatures.sum()-Error_train_fs.sum())/Error_train_nofeatures.sum()))
print('- R^2 test: {0}'.format((Error_test_nofeatures.sum()-Error_test_fs.sum())/Error_test_nofeatures.sum()))
figure(k)
subplot(1,3,2)
bmplot(attributeNames, range(1,Features.shape[1]+1), -Features)
clim(-1.5,0)
xlabel('Crossvalidation fold')
ylabel('Attribute')
# Inspect selected feature coefficients effect on the entire dataset and
# plot the fitted model residual error as function of each attribute to
# inspect for systematic structure in the residual
f=2 # cross-validation fold to inspect
ff=Features[:,f-1].nonzero()[0]
if len(ff) is 0:
print('\nNo features were selected, i.e. the data (X) in the fold cannot describe the outcomes (y).' )
else:
m = lm.LinearRegression(fit_intercept=True).fit(X[:,ff], y)
y_est= m.predict(X[:,ff])
residual=y-y_est
figure(k+1)
title('Residual error vs. Attributes for features selected in cross-validation fold {0}'.format(f))
for i in range(0,len(ff)):
subplot(2,np.ceil(len(ff)/2.0),i+1)
plot(X[:,ff[i]],residual,'.')
xlabel(attributeNames[ff[i]])
ylabel('residual error')
show()
# exercise 6.3.1
from matplotlib.pyplot import figure, boxplot, xlabel, ylabel, show
import numpy as np
from scipy.io import loadmat
import sklearn.linear_model as lm
from sklearn import cross_validation, tree
from scipy import stats
## Crossvalidation
# Create crossvalidation partition for evaluation
K = 10
CV = cross_validation.KFold(N,K,shuffle=True)
#CV = cross_validation.StratifiedKFold(y.A.ravel(),k=K)
# Initialize variables
Error_logreg = np.empty((K,1))
Error_dectree = np.empty((K,1))
n_tested=0
k=0
for train_index, test_index in CV:
print('CV-fold {0} of {1}'.format(k+1,K))
# 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]
# Fit and evaluate Logistic Regression classifier
model = lm.logistic.LogisticRegression(C=N)
model = model.fit(X_train, y_train)
y_logreg = model.predict(X_test)
Error_logreg[k] = 100*(y_logreg!=y_test).sum().astype(float)/len(y_test)
# Fit and evaluate Decision Tree classifier
model2 = tree.DecisionTreeClassifier()
model2 = model2.fit(X_train, y_train)
y_dectree = model2.predict(X_test)
Error_dectree[k] = 100*(y_dectree!=y_test).sum().astype(float)/len(y_test)
k+=1
# Test if classifiers are significantly different using methods in section 9.3.3
# by computing credibility interval. Notice this can also be accomplished by computing the p-value using
# [tstatistic, pvalue] = stats.ttest_ind(Error_logreg,Error_dectree)
# and test if the p-value is less than alpha=0.05.
z = (Error_logreg-Error_dectree)
zb = z.mean()
nu = K-1
sig = (z-zb).std() / (K-1)
alpha = 0.05
zL = zb + sig * stats.t.ppf(alpha/2, nu);
zH = zb + sig * stats.t.ppf(1-alpha/2, nu);
if zL <= 0 and zH >= 0 :
print('Classifiers are not significantly different')
else:
print('Classifiers are significantly different.')
# Boxplot to compare classifier error distributions
figure()
boxplot(np.bmat('Error_logreg, Error_dectree'))
xlabel('Logistic Regression vs. Decision Tree')
ylabel('Cross-validation error [%]')
show()
# Plot the training data points (color-coded) and test data points.
figure(1);
hold(True);
styles = ['.b', '.r', '.g', '.y','o','p']
for c in range(C):
class_mask = (y_train==c)
plot(X_train[class_mask,0], X_train[class_mask,1], styles[c])
# K-nearest neighbors
K=5
# Distance metric (corresponds to 2nd norm, euclidean distance).
# You can set dist=1 to obtain manhattan distance (cityblock distance).
dist=2
# Fit classifier and classify the test points
knclassifier = KNeighborsClassifier(n_neighbors=K, p=dist);
knclassifier.fit(X_train, y_train);
y_est = knclassifier.predict(X_test);
# Plot the classfication results
styles = ['ob', 'or', 'og', 'oy', 'oc', 'ow']
for c in range(C):
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');
# Compute and plot confusion matrix
cm = confusion_matrix(y_test, y_est);
accuracy = 100*cm.diagonal().sum()/cm.sum(); error_rate = 100-accuracy;
figure(2);
imshow(cm, cmap='binary', interpolation='None');
colorbar()
xticks(range(C)); yticks(range(C));
xlabel('Predicted class'); ylabel('Actual class');
title('Confusion matrix (Accuracy: {0}%, Error Rate: {1}%)'.format(accuracy, error_rate));
show()
from sklearn import cross_validation
import numpy as np
# Maximum number of neighbors
L=40
CV = cross_validation.LeaveOneOut(N)
errors = np.zeros((N,L))
i=0
for train_index, test_index in CV:
print('Crossvalidation fold: {0}/{1}'.format(i+1,N))
# 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]
# 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()
from sklearn.naive_bayes import MultinomialNB
y = y.squeeze()
# Naive Bayes classifier parameters
alpha = 1.0 # additive parameter (e.g. Laplace correction)
est_prior = True # uniform prior (change to True to estimate prior from data)
# K-fold crossvalidation
K = 10
CV = cross_validation.KFold(N,K,shuffle=True)
errors = np.zeros(K)
k=0
for train_index, test_index in CV:
print('Crossvalidation fold: {0}/{1}'.format(k+1,K))
# 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]
nb_classifier = MultinomialNB(alpha=alpha, fit_prior=est_prior)
nb_classifier.fit(X_train, y_train)
y_est_prob = nb_classifier.predict_proba(X_test)
y_est = np.argmax(y_est_prob,1)
errors[k] = np.sum(y_est!=y_test,dtype=float)/y_test.shape[0]
k+=1
# Plot the classification error rate
print('Error rate: {0}%'.format(100*np.mean(errors)))
import os
from matplotlib.pylab import (figure, semilogx, loglog, xlabel, ylabel, legend, title, subplot, show, hold, plot, contour, contourf, cm, colorbar, legend)
import numpy as np
from scipy.io import loadmat
import sklearn.linear_model as lm
from sklearn import model_selection
from toolbox_02450 import rlr_validate
# Add offset attribute
X = np.concatenate((np.ones((X.shape[0],1)),X),1)
attributeNames = [u'Offset']+attributeNames
M = M+1
y = y.reshape(213,1)
## Crossvalidation
# Create crossvalidation partition for evaluation
K = 5
CV = model_selection.KFold(K)
# 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))
w_rlr = np.matrix(np.empty((M,K)))
w_noreg = np.matrix(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, mean_w_vs_lambda, train_err_vs_lambda, test_err_vs_lambda = rlr_validate(X_train, y_train, lambdas, internal_cross_validation)
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()/y_train.shape[0]
Error_test_nofeatures[k] = np.square(y_test-y_test.mean()).sum()/y_test.shape[0]
# Estimate weights for the optimal value of lambda, on entire training set
w_rlr[:,k] = np.linalg.lstsq(XtX+opt_lambda*np.eye(M),Xty)[0]
# Compute mean squared error with regularization with optimal lambda
Error_train_rlr[k] = np.square(y_train-X_train @ w_rlr[:,k]).sum()/y_train.shape[0]
Error_test_rlr[k] = np.square(y_test-X_test @ w_rlr[:,k]).sum()/y_test.shape[0]
# Estimate weights for unregularized linear regression, on entire training set
w_noreg[:,k] = np.linalg.lstsq(XtX,Xty)[0]
# Compute mean squared error without regularization
Error_train[k] = np.square(y_train-X_train @ w_noreg[:,k]).sum()/y_train.shape[0]
Error_test[k] = np.square(y_test-X_test @ w_noreg[:,k]).sum()/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]
figure(k, figsize=(12,8))
subplot(1,2,1)
semilogx(lambdas,mean_w_vs_lambda.T,'.-')
xlabel('Regularization factor')
ylabel('Mean Coefficient Values')
subplot(1,2,2)
title('Optimal lambda = {0}'.format(opt_lambda))
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'])
print('Cross validation fold {0}/{1}:'.format(k+1,K))
print('Train indices: {0}'.format(train_index))
print('Test indices: {0}\n'.format(test_index))
k+=1
# Display results
print('\n')
print('Linear regression without feature selection:\n')
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()))
show()
y = y.reshape(213,)
import neurolab as nl
# Parameters for neural network classifier
n_hidden_units = 1 # number of hidden units
n_train = 2 # number of networks trained in each k-fold
# These parameters are usually adjusted to: (1) data specifics, (2) computational constraints
learning_goal = 2.0 # stop criterion 1 (train mse to be reached)
max_epochs = 200 # stop criterion 2 (max epochs in training)
# K-fold CrossValidation (4 folds here to speed up this example)
K = 4
CV = model_selection.KFold(K,shuffle=True)
# Variable for classification error
errors = np.zeros(K)
error_hist = np.zeros((max_epochs,K))
bestnet = list()
k=0
for train_index, test_index in CV.split(X,y):
print('\nCrossvalidation fold: {0}/{1}'.format(k+1,K))
# 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,:]
best_train_error = 1e100
for i in range(n_train):
# Create randomly initialized network with 2 layers
ann = nl.net.newff([[0, 1], [0, 1]], [n_hidden_units, 1], [nl.trans.TanSig(),nl.trans.PureLin()])
# train network
train_error = ann.train(X_train, y_train, goal=learning_goal, epochs=max_epochs, show=round(max_epochs/8))
if train_error[-1]<best_train_error:
bestnet.append(ann)
best_train_error = train_error[-1]
error_hist[range(len(train_error)),k] = train_error
y_est = bestnet[k].sim(X_test)
y_est = (y_est>.5).astype(int)
errors[k] = (y_est!=y_test).sum().astype(float)/y_test.shape[0]
k+=1
# Print the average classification error rate
print('Error rate: {0}%'.format(100*np.mean(errors)))
# Display the decision boundary for the several crossvalidation folds.
# (create grid of points, compute network output for each point, color-code and plot).
grid_range = [-1, 2, -1, 2]; delta = 0.05; levels = 100
a = np.arange(grid_range[0],grid_range[1],delta)
b = np.arange(grid_range[2],grid_range[3],delta)
A, B = np.meshgrid(a, b)
values = np.zeros(A.shape)
figure(1,figsize=(18,9)); hold(True)
for k in range(4):
subplot(2,2,k+1)
cmask = (y==0).squeeze(); plot(X[cmask,0], X[cmask,1],'.r')
cmask = (y==1).squeeze(); plot(X[cmask,0], X[cmask,1],'.b')
title('Model prediction and decision boundary (kfold={0})'.format(k+1))
xlabel('Feature 1'); ylabel('Feature 2');
for i in range(len(a)):
for j in range(len(b)):
values[i,j] = bestnet[k].sim( np.mat([a[i],b[j]]) )[0,0]
contour(A, B, values, levels=[.5], colors=['k'], linestyles='dashed')
contourf(A, B, values, levels=np.linspace(values.min(),values.max(),levels), cmap=cm.RdBu)
if k==0: colorbar(); legend(['Class A (y=0)', 'Class B (y=1)'])
# Display exemplary networks learning curve (best network of each fold)
figure(2); hold(True)
bn_id = np.argmax(error_hist[-1,:])
error_hist[error_hist==0] = learning_goal
for bn_id in range(K):
plot(error_hist[:,bn_id]); xlabel('epoch'); ylabel('train error (mse)'); title('Learning curve (best for each CV fold)')
plot(range(max_epochs), [learning_goal]*max_epochs, '-.')
show()
from matplotlib.pyplot import (figure,plot, subplot, bar, title, show)
import numpy as np
from scipy.io import loadmat
import neurolab as nl
from sklearn import model_selection
from scipy import stats
# Normalize data
X = stats.zscore(X);
# Parameters for neural network classifier
n_hidden_units = 2 # number of hidden units
n_train = 2 # number of networks trained in each k-fold
learning_goal = 10 # stop criterion 1 (train mse to be reached)
max_epochs = 64 # stop criterion 2 (max epochs in training)
show_error_freq = 3 # frequency of training status updates
# K-fold crossvalidation
K = 3 # only five folds to speed up this example
CV = model_selection.KFold(K,shuffle=True)
# Variable for classification error
errors = np.zeros(K)
error_hist = np.zeros((max_epochs,K))
bestnet = list()
k=0
for train_index, test_index in CV.split(X,y):
print('\nCrossvalidation fold: {0}/{1}'.format(k+1,K))
# 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,:]
best_train_error = 1e100
for i in range(n_train):
print('Training network {0}/{1}...'.format(i+1,n_train))
# Create randomly initialized network with 2 layers
ann = nl.net.newff([[-3, 3]]*M, [n_hidden_units, 1], [nl.trans.TanSig(),nl.trans.PureLin()])
if i==0:
bestnet.append(ann)
# train network
train_error = ann.train(X_train, y_train, goal=learning_goal, epochs=max_epochs, show=show_error_freq)
if train_error[-1]<best_train_error:
bestnet[k]=ann
best_train_error = train_error[-1]
error_hist[range(len(train_error)),k] = train_error
print('Best train error: {0}...'.format(best_train_error))
y_est = bestnet[k].sim(X_test)
y_est = (y_est>.5).astype(int)
errors[k] = (y_est!=y_test).sum().astype(float)/y_test.shape[0]
k+=1
# Print the average classification error rate
print('Error rate: {0}%'.format(100*np.mean(errors)))
figure(figsize=(6,7));
subplot(2,1,1); bar(range(0,K),errors); title('CV errors');
subplot(2,1,2); plot(error_hist); title('Training error as function of BP iterations');
figure(figsize=(6,7));
subplot(2,1,1); plot(y_est); plot(y_test); title('Last CV-fold: est_y vs. test_y');
subplot(2,1,2); plot((y_est-y_test)); title('Last CV-fold: prediction error (est_y-test_y)');
show()
from matplotlib.pyplot import figure, show
from scipy.io import loadmat
from toolbox_02450 import dbplotf
import numpy as np
import sklearn.neural_network as nn
import sklearn.linear_model as lm
#from pybrain.datasets import ClassificationDataSet
#from pybrain.tools.shortcuts import buildNetwork
#from pybrain.supervised.trainers import BackpropTrainer
#from pybrain.structure.modules import SoftmaxLayer
#%% Model fitting and prediction
## ANN Classifier, i.e. MLP with one hidden layer
clf = nn.MLPClassifier(solver='lbfgs',alpha=1e-4,
hidden_layer_sizes=(NHiddenUnits,), random_state=1)
clf.fit(X_train,y_train)
print('Number of miss-classifications for ANN:\n\t {0} out of {1}'.format(np.sum(clf.predict(X_test)!=y_test),len(y_test)))
# Multinomial logistic regression
logreg = lm.LogisticRegression(solver='lbfgs', multi_class='multinomial', tol=1e-4, random_state=1)
logreg.fit(X_train,y_train)
# Number of miss-classifications
print('Number of miss-classifications for Multinormal regression:\n\t {0} out of {1}'.format(np.sum(logreg.predict(X_test)!=y_test),len(y_test)))
#%% Decision boundaries for the ANN model
figure(1)
def neval(xval):
return np.argmax(clf.predict_proba(xval),1)
dbplotf(X_test,y_test,neval,'auto')
show()
#%% Decision boundaries for the multinomial regression model
figure(1)
def nevallog(xval):
return np.argmax(logreg.predict_proba(xval),1)
dbplotf(X_test,y_test,nevallog,'auto')
show()
# exercise 8.3.2 Fit Multinomial logistic regression
from matplotlib.pyplot import figure, show
from scipy.io import loadmat
from toolbox_02450 import dbplotf
import numpy as np
import sklearn.neural_network as nn
import sklearn.linear_model as lm
#from pybrain.datasets import ClassificationDataSet
#from pybrain.tools.shortcuts import buildNetwork
#from pybrain.supervised.trainers import BackpropTrainer
#from pybrain.structure.modules import SoftmaxLayer
#%% Model fitting and prediction
## ANN Classifier, i.e. MLP with one hidden layer
clf = nn.MLPClassifier(solver='lbfgs',alpha=1e-4,
hidden_layer_sizes=(NHiddenUnits,), random_state=1)
clf.fit(X_train,y_train)
print('Number of miss-classifications for ANN:\n\t {0} out of {1}'.format(np.sum(clf.predict(X_test)!=y_test),len(y_test)))
# Multinomial logistic regression
logreg = lm.LogisticRegression(solver='lbfgs', multi_class='multinomial', tol=1e-4, random_state=1)
logreg.fit(X_train,y_train)
# Number of miss-classifications
print('Number of miss-classifications for Multinormal regression:\n\t {0} out of {1}'.format(np.sum(logreg.predict(X_test)!=y_test),len(y_test)))
#%% Decision boundaries for the ANN model
figure(1)
def neval(xval):
return np.argmax(clf.predict_proba(xval),1)
dbplotf(X_test,y_test,neval,'auto')
show()
#%% Decision boundaries for the multinomial regression model
figure(1)
def nevallog(xval):
return np.argmax(logreg.predict_proba(xval),1)
dbplotf(X_test,y_test,nevallog,'auto')
show()