-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_ML_HW03_19512087.py
177 lines (149 loc) · 7.31 KB
/
advanced_ML_HW03_19512087.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
# -*- coding: utf-8 -*-
# DO NOT CHANGE
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import pairwise_distances
import time
import matplotlib.pyplot as plt
def wkNN(Xtr,ytr,Xts,k,random_state=None):
# Implement PNN
# Xtr: training input data set
# ytr: ouput labels of training set
# Xts: test data set
# random_state: random seed
# return 1-D array containing output labels of test set
dst = pairwise_distances(Xts,Xtr,metric='euclidean')
y_pred=[]
for i, Xts_ds in enumerate(dst):
knn = Xts_ds[np.argsort(dst[i])[:k]]
knn_cls = ytr[np.argsort(dst[i])[:k]]
votes = np.zeros(len(np.unique(ytr)))
w_j = [(knn.max() - knn[j])/(knn.max() - knn.min()) if knn.max() != knn.min() else 1 for j in range(len(knn))]
for l in range(len(w_j)):
votes[int(knn_cls[l])] += w_j[l]
y_pred.append(np.argsort(votes)[-1])
return y_pred
def PNN(Xtr,ytr,Xts,k,random_state=None):
# Implement PNN
# Xtr: training input data set
# ytr: ouput labels of training set
# Xts: test data set
# random_state: random seed
# return 1-D array containing output labels of test set
dst = pairwise_distances(Xts,Xtr,metric='euclidean')
y_pred=[]
for i in range(len(dst)):
tmp_dict = {}
for j in np.unique(ytr):
tmp_cls = dst[i, np.where(ytr == j)[0]]
tmp_dict[j] = tmp_cls[np.argsort(tmp_cls)][:k]
for l in range(len(tmp_dict)):
for n in range(k):
tmp_dict[l][n] /= n+1
tmp_dict[l] = sum(tmp_dict[l])
y_pred.append(min(tmp_dict, key=tmp_dict.get))
return y_pred
def accuracy_score(yts, y_pred):
correct = 0
for i in range(len(y_pred)):
if y_pred[i] == yts[i]:
correct += 1
accuracy = correct/len(y_pred)
return format(accuracy, ".4f")
#TODO: Cacluate accuracy with varying k for wkNN and PNN
#TODO: Calculate computation time
#TODO: Draw scatter plot
X1,y1=datasets.make_classification(n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_classes=3, n_clusters_per_class=1, random_state=13)
Xtr1,Xts1, ytr1, yts1=train_test_split(X1,y1,test_size=0.2,random_state=22)
X2,y2=datasets.make_classification(n_samples=1000, n_features=6, n_informative=2, n_redundant=3, n_classes=2, n_clusters_per_class=2, flip_y=0.2,random_state=75)
Xtr2, Xts2, ytr2, yts2=train_test_split(X2,y2,test_size=0.2, random_state=78)
start_time = time.time()
wkNN_y_pred_3_1 = wkNN(Xtr1,ytr1,Xts1,3,random_state=None)
wkNN_y_pred_5_1 = wkNN(Xtr1,ytr1,Xts1,5,random_state=None)
wkNN_y_pred_7_1 = wkNN(Xtr1,ytr1,Xts1,7,random_state=None)
wkNN_y_pred_9_1 = wkNN(Xtr1,ytr1,Xts1,9,random_state=None)
wkNN_y_pred_11_1 = wkNN(Xtr1,ytr1,Xts1,11,random_state=None)
PNN_y_pred_3_1 = PNN(Xtr1,ytr1,Xts1,3,random_state=None)
PNN_y_pred_5_1 = PNN(Xtr1,ytr1,Xts1,5,random_state=None)
PNN_y_pred_7_1 = PNN(Xtr1,ytr1,Xts1,7,random_state=None)
PNN_y_pred_9_1 = PNN(Xtr1,ytr1,Xts1,9,random_state=None)
PNN_y_pred_11_1 = PNN(Xtr1,ytr1,Xts1,11,random_state=None)
dataset1_time = format(time.time() - start_time, ".4f")
start_time = time.time()
wkNN_y_pred_3_2 = wkNN(Xtr2,ytr2,Xts2,3,random_state=None)
wkNN_y_pred_5_2 = wkNN(Xtr2,ytr2,Xts2,5,random_state=None)
wkNN_y_pred_7_2 = wkNN(Xtr2,ytr2,Xts2,7,random_state=None)
wkNN_y_pred_9_2 = wkNN(Xtr2,ytr2,Xts2,9,random_state=None)
wkNN_y_pred_11_2 = wkNN(Xtr2,ytr2,Xts2,11,random_state=None)
PNN_y_pred_3_2 = PNN(Xtr2,ytr2,Xts2,3,random_state=None)
PNN_y_pred_5_2 = PNN(Xtr2,ytr2,Xts2,5,random_state=None)
PNN_y_pred_7_2 = PNN(Xtr2,ytr2,Xts2,7,random_state=None)
PNN_y_pred_9_2 = PNN(Xtr2,ytr2,Xts2,9,random_state=None)
PNN_y_pred_11_2 = PNN(Xtr2,ytr2,Xts2,11,random_state=None)
dataset2_time = format(time.time() - start_time, ".4f")
print("Elapsed time: ", dataset1_time)
print("--------------------------------")
print("{:>6}".format("k"), "{:>6}".format("wkNN"), "{:>6}".format("PNN"))
print("{:>6}".format("3"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_3_1)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_3_1)))
print("{:>6}".format("5"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_5_1)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_5_1)))
print("{:>6}".format("7"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_7_1)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_7_1)))
print("{:>6}".format("9"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_9_1)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_9_1)))
print("{:>6}".format("11"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_11_1)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_11_1)))
print("\n")
print("Elapsed time: ", dataset2_time)
print("--------------------------------")
print("{:>6}".format("k"), "{:>6}".format("wkNN"), "{:>6}".format("PNN"))
print("{:>6}".format("3"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_3_2)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_3_2)))
print("{:>6}".format("5"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_5_2)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_5_2)))
print("{:>6}".format("7"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_7_2)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_7_2)))
print("{:>6}".format("9"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_9_2)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_9_2)))
print("{:>6}".format("11"), "{:>6}".format(accuracy_score(yts1, wkNN_y_pred_11_2)), "{:>6}".format(accuracy_score(yts1, PNN_y_pred_11_2)))
# Draw the figures
# Data1: k = 7
scatt = []
for j in range(200):
if wkNN_y_pred_7_1[j]==yts1[j] :
scatt.append(1)
else :
scatt.append(0)
scatt = np.array(scatt)
missed1 = Xts1[np.where(scatt == 0), :]
scatt=[]
for j in range(200):
if PNN_y_pred_7_1[j] == yts1[j] :
scatt.append(1)
else :
scatt.append(0)
scatt = np.array(scatt)
missed2 = Xts1[np.where(scatt == 0), :]
plt.figure(figsize=(10, 8))
plt.scatter(Xtr1[:, 0], Xtr1[:, 1], c=ytr1, label='Train')
plt.scatter(Xts1[:, 0], Xts1[:, 1], c=yts1, marker='x', label='Test')
plt.scatter(missed1[0][:, 0], missed1[0][:, 1], marker='s', edgecolors='r', facecolors='none', label='Missclassified by wkNN')
plt.scatter(missed2[0][:, 0], missed2[0][:, 1], marker='d', edgecolors='b', facecolors='none', label='Missclassified by PNN')
plt.legend(loc='under right')
# Draw the figures
# Data2: k = 7
scatt = []
for j in range(200):
if wkNN_y_pred_7_2[j]==yts2[j] :
scatt.append(1)
else :
scatt.append(0)
scatt = np.array(scatt)
missed3 = Xts2[np.where(scatt == 0), :]
scatt=[]
for j in range(200):
if PNN_y_pred_7_2[j] == yts2[j] :
scatt.append(1)
else :
scatt.append(0)
scatt = np.array(scatt)
missed4 = Xts2[np.where(scatt == 0), :]
plt.figure(figsize=(10, 8))
plt.scatter(Xtr2[:, 0], Xtr2[:, 1], c=ytr2, label='Train')
plt.scatter(Xts2[:, 0], Xts2[:, 1], c=yts2, marker='x', label='Test')
plt.scatter(missed3[0][:, 0], missed3[0][:, 1], marker='s', edgecolors='r', facecolors='none', label='Missclassified by wkNN')
plt.scatter(missed4[0][:, 0], missed4[0][:, 1], marker='d', edgecolors='b', facecolors='none', label='Missclassified by PNN')
plt.legend(loc='under right')