-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKNN.py
More file actions
346 lines (241 loc) · 8.86 KB
/
KNN.py
File metadata and controls
346 lines (241 loc) · 8.86 KB
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
"""
Implement k-th nearest neighbors classifier
Input: train_X: a matrix for training set features
train_Y: a vector raining set class labels
test_X: a vector for testing set features
kVal: specify the k value in k-th nearest neighbors
Return: result: a vector for predicted class labels given features in toTest
Author: Xuanpei Ouyang
"""
import numpy as np
import random
import operator
from heapq import heappop
from heapq import heappush
"""
This function takes in test sample size, total sample size,
and random generator as parameters
Return: a list of index for the test sample
"""
def select_test_sample_index(test_size, total_size, random):
test_sample_index = []
i = j = 0
tmp = test_size
while i < tmp:
prob = float(test_size)/float(total_size)
rand = random.random()
if rand <= prob:
test_sample_index.append(j)
test_size -= 1
i += 1
total_size -= 1
j += 1
return test_sample_index
"""
Given a n x 1 vector as original labels and convert original labels
into binary code labels by using one-hot encoding.
Return: mew_labels: the one-hot encoding of original labels
"""
def one_hot_encoding(labels):
num_class = len(np.unique(labels))
arr = np.zeros((len(labels), num_class), dtype=np.int)
for i in range(len(labels)):
arr[i][labels[i]] = 1
return arr
"""
Given row training and testing data set,
Return training and testing data set with z scaling
"""
def z_transform(test_dataset, train_dataset):
# calculate the mean and standard deviation of training sample
mean = np.mean(test_dataset, axis=0)
std = np.std(train_dataset, axis=0, ddof=1)
for i in range(test_dataset.shape[1]-1):
train_dataset[:, i] = (train_dataset[:, i] - mean[i]) / std[i]
test_dataset[:, i] = (test_dataset[:, i] - mean[i]) / std[i]
return train_dataset, test_dataset
"""
Given a data set
Return: n x 1 column which is the i th row of the dataset
"""
def get_column(data, i):
return [row[i] for row in data]
"""
transform the category data to numerical data
"""
def categ_to_numerical(labels):
new_categ = []
for i in range(len(labels)):
if labels[i] == 'F':
new_categ.append(0)
elif labels[i] == 'M':
new_categ.append(1)
else:
new_categ.append(2)
return new_categ
"""
Given two data points with n features
Return: the euclidian distance between these two data points
"""
def get_euclidian_distance(x1, x2):
dist = np.sqrt(sum(np.square(x1 - x2)))
return dist
"""
Use KNN to predict testing data into different category
"""
def KNN(train_X, train_Y, test_X, kVal):
print "\ntraining KNN"
predict = np.zeros((test_X.shape[0]))
for i in range(test_X.shape[0]):
curr = test_X[i, :]
pq = []
for j in range(train_X.shape[0]):
tmp_dist = get_euclidian_distance(curr, train_X[j,:])
heappush(pq, (tmp_dist, j))
indices = []
for j in range(kVal):
pair = heappop(pq)
index = pair[1]
indices.append(index)
neighbor = {}
for j in indices:
if train_Y[j] in neighbor:
count = neighbor[train_Y[j]]
neighbor[train_Y[j]] = count+1
else:
neighbor[train_Y[j]] = 1
sorted_neighbor = sorted(neighbor.items(), key=operator.itemgetter(1))
pair = sorted_neighbor[len(sorted_neighbor)-1]
pred = pair[0]
predict[i] = pred
return predict
"""
Calculate accuracy based on the result of KNN
"""
def accuracy(predict_Y, test_Y):
correct = 0
for i in range(test_Y.shape[0]):
if predict_Y[i] == test_Y[i]:
correct += 1
acc = float(correct) / float(test_Y.shape[0])
return acc
"""
This method will calculate confusion matrix
Only work for class labels that are already converted to [0, number of unqiue]
"""
def confusion_matrix(predict_Y, true_Y):
labels = np.unique(np.hstack((true_Y, predict_Y)))
dict={}
for i in range(len(labels)):
dict[labels[i]] = i
num_of_labels = len(labels)
mat = np.zeros((num_of_labels, num_of_labels), dtype=np.int)
for i in range(predict_Y.shape[0]):
true_class = int(true_Y[i])
pred_class = int(predict_Y[i])
true_index = dict[true_class]
pred_index = dict[pred_class]
tmp = mat[true_index][pred_index] + 1
mat[true_index][pred_index] = tmp
print "\nrow - expected label, col - predicted label"
print "row labels are " + str(labels)
print "col labels are " + str(labels)
print mat
"""
This method will load 3 kind of file
Seperable.csv, 3percent-miscategorization.csv,
10percent-miscatergorization.csv
And print the accuracy
"""
def run_files(file_name, k_val):
if file_name == 'Seperable.csv':
print "\nLoad Seperable.csv and train KNN with k ="+str(k_val)
elif file_name == '3percent-miscategorization.csv':
print "\nLoad 3percent-miscategorization.csv and train KNN with k ="+str(k_val)
elif file_name == '10percent-miscatergorization.csv':
print "\nLoad 10percent-miscategorization.csv and train KNN with k ="+str(k_val)
# get the dataset from the csv file
try:
hw_data = np.genfromtxt(file_name, delimiter=',')
except IOError:
print "\nFail to load"+file_name
print "Please check if the file is under the same directory."
print "\nFinish loading " + file_name
print "\npre-processing the data"
# get the number of feature and test size
hw_total_size = hw_data.shape[0]
hw_test_size = int(hw_total_size * 0.1)
random.seed(1)
testing_sample_index = select_test_sample_index(hw_test_size, hw_total_size, random)
training_sample_index = []
for i in range(hw_total_size):
if i not in testing_sample_index:
training_sample_index.append(i)
# extract the testing and training sample from the dataset
testing_sample = hw_data[testing_sample_index, :]
training_sample = hw_data[training_sample_index, :]
# apply the z-transform to the testing dataset and training dataset
# with the mean and standard deviation of training dataset
z_trans_result = z_transform(testing_sample, training_sample)
z_trans_train = z_trans_result[0]
z_trans_test = z_trans_result[1]
train_X = z_trans_train[:, 0:9]
train_Y = z_trans_train[:, 9]
test_X = z_trans_test[:, 0:9]
test_Y = z_trans_test[:, 9]
train_Y = train_Y.astype(int)
predict_Y = KNN(train_X, train_Y, test_X, k_val)
acc = accuracy(predict_Y, test_Y)
print "\naccuracy is " + str(acc)
confusion_matrix(predict_Y, test_Y)
def load_abalone(k_val):
print "\nLoad abalone csv file and train KNN with k =" + str(k_val)
# get the dataset from the csv file
abalone_data = np.genfromtxt('abalone.csv', delimiter=',', dtype=None)
abalone_data2 = np.genfromtxt('abalone.csv', delimiter=',')
num_part = np.delete(abalone_data2, 0, 1)
# get the number of feature and test size
abalone_total_size = abalone_data.shape[0]
abalone_test_size = int(abalone_total_size * 0.1)
random.seed(1)
# transform the categorical column into one-hot encoding
# numerical column
categ = get_column(abalone_data, 0)
tmp_categ = categ_to_numerical(categ)
new_categ = one_hot_encoding(tmp_categ)
new_dataset = np.hstack((new_categ, num_part))
testing_sample_index = select_test_sample_index(abalone_test_size, abalone_total_size, random)
training_sample_index = []
for i in range(abalone_total_size):
if i not in testing_sample_index:
training_sample_index.append(i)
# extract the testing and training sample from the dataset
testing_sample = new_dataset[testing_sample_index, :]
training_sample = new_dataset[training_sample_index, :]
# apply the z-transform to the testing dataset and training dataset
# with the mean and standard deviation of training dataset
z_trans_result = z_transform(testing_sample, training_sample)
z_trans_train = z_trans_result[0]
z_trans_test = z_trans_result[1]
train_X = z_trans_train[:, 0:10]
train_Y = z_trans_train[:, 10]
test_X = z_trans_test[:, 0:10]
test_Y = z_trans_test[:, 10]
train_Y = train_Y.astype(int)
predict_Y = KNN(train_X, train_Y, test_X, k_val)
acc = accuracy(predict_Y, test_Y)
print "\naccuracy is " + str(acc)
confusion_matrix(predict_Y, test_Y)
"""
main function loads abalone.csv under the same folder
"""
if __name__ == "__main__":
list_k = [1, 3, 5, 7, 9]
for k in list_k:
load_abalone(k)
for k in list_k:
run_files('Seperable.csv', k)
for k in list_k:
run_files('3percent-miscategorization.csv', k)
for k in list_k:
run_files('10percent-miscatergorization.csv', k)