-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.py
284 lines (174 loc) · 6.85 KB
/
knn.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
# coding: utf-8
# Problem 1: Python & Data Exploration
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
iris = np.genfromtxt("/Users/dharshanbj/Desktop/HW1-code/data/iris.txt",delimiter=None) # load the text file
Y = iris[:,-1] # target value is the last column
X = iris[:,0:-1] # features are the other columns
# (a)To get the number of features, and to get the number of data points.
# In[3]:
print(X.shape[1]) #number of features
print(X.shape[0]) #number of data points
# (b)For each feature, plot a histogram of the data values
# In[62]:
plt.hist(iris[:,0],facecolor='green',label='Feature 1') #Feature 1
plt.hist(iris[:,1],facecolor='yellow',label='Feature 2') #Feature 2
plt.hist(iris[:,2],facecolor='red',label='Feature 3') #Feature 3
plt.hist(iris[:,3],facecolor='blue',label='Feature 4') #Feature 4
plt.xlabel('Feature value')
plt.ylabel('Count')
plt.title('Histogram of different features')
plt.legend()
# (c) Compute the mean & standard deviation of the data points for each feature
# In[18]:
print('mean of feature 1 data points ',np.mean(iris[:,0])) #mean of feature 1 data points
print('standard deviation of feature 1 data points',np.std(iris[:,0])) #standard deviation of feature 1 data points
print('mean of feature 2 data points',np.mean(iris[:,1])) #mean of feature 2 data points
print('standard deviation of feature 2 data points',np.std(iris[:,1])) #standard deviation of feature 2 data points
print('mean of feature 3 data points',np.mean(iris[:,2])) #mean of feature 3 data points
print('standard deviation of feature 3 data points',np.std(iris[:,2])) #standard deviation of feature 3 data points
print('mean of feature 4 data points',np.mean(iris[:,3])) #mean of feature 4 data points
print('standard deviation of feature 4 data points',np.std(iris[:,3])) #standard deviation of feature 4 data points
# d) For each pair of features (1,2), (1,3), and (1,4), plot a scatterplot of the feature values, colored according to their target value (class).
# In[44]:
col=[]
#print(Y)
for i in range(0,Y.size):
if Y[i]==0:
col.append('r') #red marks Y=0
elif Y[i]==1:
col.append('b') #blue marks Y=1
else:
col.append('g') #green marks Y=2
x1=iris[:,1]
x2=iris[:,2]
#print(col)
#scatter plot
plt.title('Scatter plot - Feature 1 & Feature 2')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
colors = {'r':"Y=0", 'b':"Y=1",'g':"Y=2"}
for i in range(Y.size):
plt.scatter(x1[i], x2[i],c=col[i])
#label=colors[col[i]]
#print(col)
# In[45]:
x1=iris[:,1]
x3=iris[:,3]
#scatter plot
plt.title('Scatter plot - Feature 1 & Feature 3 ')
plt.xlabel('Feature 1')
plt.ylabel('Feature 3')
colors = {'r':"Y=0", 'b':"Y=1",'g':"Y=2"}
for i in range(Y.size):
plt.scatter(x1[i], x3[i],c=col[i])
#label=colors[col[i]]
# In[46]:
x1=iris[:,1]
x4=iris[:,4]
#scatter plot
plt.title('Scatter plot - Feature 1 & Feature 4 ')
plt.xlabel('Feature 1')
plt.ylabel('Feature 4')
colors = {'r':"Y=0", 'b':"Y=1",'g':"Y=2"}
for i in range(Y.size):
plt.scatter(x1[i], x4[i],c=col[i])
#label=colors[col[i]]
# In[47]:
x2=iris[:,2]
x3=iris[:,3]
#scatter plot
plt.title('Scatter plot - Feature 2 & Feature 3 ')
plt.xlabel('Feature 2')
plt.ylabel('Feature 3')
colors = {'r':"Y=0", 'b':"Y=1",'g':"Y=2"}
for i in range(Y.size):
plt.scatter(x2[i], x3[i],c=col[i])
#label=colors[col[i]]
# In[48]:
x2=iris[:,2]
x4=iris[:,4]
#scatter plot
plt.title('Scatter plot - Feature 2 & Feature 4 ')
plt.xlabel('Feature 2')
plt.ylabel('Feature 4')
colors = {'r':"Y=0", 'b':"Y=1",'g':"Y=2"}
for i in range(Y.size):
plt.scatter(x2[i], x4[i],c=col[i])
#label=colors[col[i]]
# In[49]:
x3=iris[:,3]
x4=iris[:,4]
#scatter plot
plt.title('Scatter plot - Feature 3 & Feature 4 ')
plt.xlabel('Feature 3')
plt.ylabel('Feature 4')
colors = {'r':"Y=0", 'b':"Y=1",'g':"Y=2"}
for i in range(Y.size):
plt.scatter(x3[i], x4[i],c=col[i])
#label=colors[col[i]]
# Problem 2: kNN predictions (30 pts)
# (a) Modify the code listed above to use only the first two features of X (e.g., let X be only the first two columns of iris, instead of the first four), and visualize (plot) the classification boundary for varying values of K =[1,5,10,50] using plotClassify2D.
# In[53]:
Y = iris[:,-1]
X = iris[:,0:2]
import mltools as ml
X,Y = ml.shuffleData(X,Y);
Xtr,Xva,Ytr,Yva = ml.splitData(X,Y, 0.75);
knn = ml.knn.knnClassify()
knn.train(Xtr, Ytr, 1) # where K is an integer, e.g. 1 for nearest neighbor predict
YvaHat = knn.predict(Xva) # get estimates of y for each data point in Xva
ml.plotClassify2D(knn, Xtr, Ytr);
# In[54]:
knn = ml.knn.knnClassify()
knn.train(Xtr, Ytr, 5) # where K is an integer, e.g. 1 for nearest neighbor predict
YvaHat = knn.predict(Xva) # get estimates of y for each data point in Xva
ml.plotClassify2D(knn, Xtr, Ytr);
# In[58]:
knn = ml.knn.knnClassify()
knn.train(Xtr, Ytr, 10) # where K is an integer, e.g. 1 for nearest neighbor predict
YvaHat = knn.predict(Xva) # get estimates of y for each data point in Xva
ml.plotClassify2D(knn, Xtr, Ytr);
# In[59]:
knn = ml.knn.knnClassify()
knn.train(Xtr, Ytr, 50) # where K is an integer, e.g. 1 for nearest neighbor predict
YvaHat = knn.predict(Xva) # get estimates of y for each data point in Xva
ml.plotClassify2D(knn, Xtr, Ytr);
# (b) Again using only the first two features, compute the error rate (number of misclassifications) on both the training and validation data as a function of K = [1,2,5,10,50,100,200]. You can do this most easily with a for-loop:
# In[72]:
Y = iris[:,-1]
X = iris[:,0:2]
X,Y = ml.shuffleData(X,Y);
Xtr,Xva,Ytr,Yva = ml.splitData(X,Y, 0.75);
errTrain = []
errValidation=[]
countTrain=0
countVal=0
K=[1,2,5,10,50,100,200];
for i,k in enumerate(K):
learner = ml.knn.knnClassify() # TODO: complete code to train model
learner.train(Xtr, Ytr, k)
Yhat = learner.predict(Xtr) # TODO: complete code to predict results on training data
for j in range(len(Yhat)-1):
if Yhat[j] !=Ytr[j]:
countTrain = countTrain+1 # TODO: " " to count what fraction of predictions are wrong
errTrain.append(countTrain/75)
countTrain=0
Yhatvl = learner.predict(Xva) # TODO: complete code to predict results on training data
for j in range(len(Yva)-1):
if Yhatvl[j] !=Yva[j]:
countVal = countVal+1 # TODO: " " to count what fraction of predictions are wrong
errValidation.append(countVal/25)
countVal=0
#k=5 optimal value
print(errTrain)
print(errValidation)
#TODO: repeat prediction / error evaluation for validation data
plt.semilogx(K,errTrain,c='red',label='Training error')
plt.semilogx(K,errValidation,c='green',label='Validation error')
plt.title("K vs Error rate")
plt.xlabel("K")
plt.ylabel("Error")
plt.legend()
# For the K value 10,both the training error and the testing error is minimum.Hence, K=10 is the recommended value.