/
formatNearestNeighborClassifier.py
353 lines (308 loc) · 12.9 KB
/
formatNearestNeighborClassifier.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
#
# The basis of this file is from the book 'Code file for the book Programmer's Guide to Data Mining' http://guidetodatamining.com
# that was written by Ron Zacharski. The sources were adapted for the research on the format analysis.
#
#
# Nearest Neighbor Classifier
#
#
# Code file for the book Programmer's Guide to Data Mining
# http://guidetodatamining.com
#
# Ron Zacharski
#
## I am trying to make the classifier more general purpose
## by reading the data from a file.
## Each line of the file contains tab separated fields.
## The first line of the file describes how those fields (columns) should
## be interpreted. The descriptors in the fields of the first line are:
##
## comment - this field should be interpreted as a comment
## class - this field describes the class of the field
## num - this field describes an integer attribute that should
## be included in the computation.
##
## more to be described as needed
##
##
## So, for example, if our file describes athletes and is of the form:
## Shavonte Zellous basketball 70 155
## The first line might be:
## comment class num num
##
## Meaning the first column (name of the player) should be considered a comment;
## the next column represents the class of the entry (the sport);
## and the next 2 represent attributes to use in the calculations.
##
## The classifer reads this file into the list called data.
## The format of each entry in that list is a tuple
##
## (class, normalized attribute-list, comment-list)
##
## so, for example
##
## [('basketball', [1.28, 1.71], ['Brittainey Raven']),
## ('basketball', [0.89, 1.47], ['Shavonte Zellous']),
## ('gymnastics', [-1.68, -0.75], ['Shawn Johnson']),
## ('gymnastics', [-2.27, -1.2], ['Ksenia Semenova']),
## ('track', [0.09, -0.06], ['Blake Russell'])]
##
from math import sqrt
from scipy import linalg, mat, dot
TRAINING_SET = 'formatsTrainingSet.txt'
FLOAT_SIZE = 2
class Classifier:
def __init__(self, filename):
self.medianAndDeviation = []
# reading the data in from the file
f = open(filename)
lines = f.readlines()
f.close()
self.format = lines[0].strip().split('\t')
self.data = []
for line in lines[1:]:
fields = line.strip().split('\t')
ignore = []
vector = []
for i in range(len(fields)):
if self.format[i] == 'num':
vector.append(float(fields[i]))
elif self.format[i] == 'comment':
ignore.append(fields[i])
elif self.format[i] == 'class':
classification = fields[i]
self.data.append((classification, vector, ignore))
self.rawData = list(self.data)
# get length of instance vector
self.vlen = len(self.data[0][1])
# now normalize the data
for i in range(self.vlen):
self.normalizeColumn(i)
##################################################
###
### CODE TO COMPUTE THE MODIFIED STANDARD SCORE
def getMedian(self, alist):
"""return median of alist"""
if alist == []:
return []
blist = sorted(alist)
length = len(alist)
if length % 2 == 1:
# length of list is odd so return middle element
return blist[int(((length + 1) / 2) - 1)]
else:
# length of list is even so compute midpoint
v1 = blist[int(length / 2)]
v2 =blist[(int(length / 2) - 1)]
return (v1 + v2) / 2.0
def getAbsoluteStandardDeviation(self, alist, median):
"""given alist and median return absolute standard deviation"""
sum = 0
for item in alist:
sum += abs(item - median)
return sum / len(alist)
def normalizeColumn(self, columnNumber):
"""given a column number, normalize that column in self.data"""
# first extract values to list
col = [v[1][columnNumber] for v in self.data]
median = self.getMedian(col)
asd = self.getAbsoluteStandardDeviation(col, median)
if asd == 0.0:
asd = 0.000001
print("Median: %f ASD = %f" % (median, asd))
self.medianAndDeviation.append((median, asd))
for v in self.data:
v[1][columnNumber] = (v[1][columnNumber] - median) / asd
print 'normalized column: ', self.data
def normalizeVector(self, v):
"""We have stored the median and asd for each column.
We now use them to normalize vector v"""
vector = list(v)
for i in range(len(vector)):
(median, asd) = self.medianAndDeviation[i]
vector[i] = (vector[i] - median) / asd
print("normalized vector: ", vector)
return vector
###
### END NORMALIZATION
##################################################
def manhattan(self, vector1, vector2):
"""Computes the Manhattan distance."""
return sum(map(lambda v1, v2: abs(v1 - v2), vector1, vector2))
def nearestNeighbor(self, itemVector):
"""return nearest neighbor to itemVector"""
return min([ (self.manhattan(itemVector, item[1]), item)
for item in self.data])
def classify(self, itemVector):
"""Return class we think item Vector is in"""
return(self.nearestNeighbor(self.normalizeVector(itemVector))[1][0])
def calculateCosineSimilarity(self, trainingVector, testVector):
"""Return cosine similarity between two vectors. We use cosine similarity because test data may be sparse"""
#sqrt(lambda x: pow(x, 2), trainingVector)
trainingVector = map(int, trainingVector[2:])
testVector = map(int, testVector[2:])
print 'trainingVector', trainingVector
print 'testVector', testVector
#a = mat(trainingVector)
#b = mat(testVector)
#print 'a', a, 'b', b
#c = dot(a,b.T)/linalg.norm(a)/linalg.norm(b)
#return c
import math
"compute cosine similarity of v1 to v2: (v1 dot v1)/{||v1||*||v2||)"
sumxx, sumxy, sumyy = 0, 0, 0
for i in range(len(trainingVector)):
x = float(trainingVector[i]); y = float(testVector[i])
sumxx += x*x
sumyy += y*y
sumxy += x*y
return sumxy/math.sqrt(sumxx*sumyy)
def unitTest():
classifier = Classifier(TRAINING_SET)
br = ('Basketball', [72, 162], ['Brittainey Raven'])
nl = ('Gymnastics', [61, 76], ['Viktoria Komova'])
cl = ("Basketball", [74, 190], ['Crystal Langhorne'])
# first check normalize function
brNorm = classifier.normalizeVector(br[1])
nlNorm = classifier.normalizeVector(nl[1])
clNorm = classifier.normalizeVector(cl[1])
assert(brNorm == classifier.data[1][1])
assert(nlNorm == classifier.data[-1][1])
print('normalizeVector fn OK')
# check distance
assert (round(classifier.manhattan(clNorm, classifier.data[1][1]), 5) == 1.16823)
assert(classifier.manhattan(brNorm, classifier.data[1][1]) == 0)
assert(classifier.manhattan(nlNorm, classifier.data[-1][1]) == 0)
print('Manhattan distance fn OK')
# Brittainey Raven's nearest neighbor should be herself
result = classifier.nearestNeighbor(brNorm)
assert(result[1][2]== br[2])
# Nastia Liukin's nearest neighbor should be herself
result = classifier.nearestNeighbor(nlNorm)
assert(result[1][2]== nl[2])
# Crystal Langhorne's nearest neighbor is Jennifer Lacy"
assert(classifier.nearestNeighbor(clNorm)[1][2][0] == "Jennifer Lacy")
print("Nearest Neighbor fn OK")
# Check if classify correctly identifies sports
assert(classifier.classify(br[1]) == 'Basketball')
assert(classifier.classify(cl[1]) == 'Basketball')
assert(classifier.classify(nl[1]) == 'Gymnastics')
print('Classify fn OK')
def test(training_filename, test_filename):
"""Test the classifier on a test set of data"""
classifier = Classifier(training_filename)
#print 'classifier: ', classifier
f = open(test_filename)
lines = f.readlines()
f.close()
numCorrect = 0.0
for line in lines:
data = line.strip().split('\t')
vector = []
classInColumn = -1
#print("%s " % (data))
for i in range(len(classifier.format)):
if classifier.format[i] == 'num':
vector.append(float(data[i]))
elif classifier.format[i] == 'class':
classInColumn = i
theClass= classifier.classify(vector)
prefix = '-'
if theClass == data[classInColumn]:
# it is correct
numCorrect += 1
prefix = '+'
print("%s %12s %s" % (prefix, theClass, line))
print("%4.2f%% correct" % (numCorrect * 100/ len(lines)))
def visualize(training_filename, par1, par2):
"""Test the classifier on a test set of data"""
#classifier = Classifier(training_filename)
f = open(training_filename)
lines = f.readlines()
f.close()
numCorrect = 0.0
for line in lines:
data = line.strip().split('\t')
# print data[1], ' par1: ', data[par1], 'par2: ', data[par2]
print '%s' % data[1] + ';' + data[par1] + ';' + data[par2]
def visualizeNormalized(training_filename, par1, par2):
"""Test the classifier on a test set of data"""
classifier = Classifier(training_filename)
f = open(training_filename)
lines = f.readlines()
f.close()
i = -1
for line in lines:
data = line.strip().split('\t')
##print data
##print data[1], ' par1: ', data[par1], 'par2: ', data[par2]
#print 'classifier.data: ', classifier.data[i][0], classifier.data[i][1]
##print '%s' % data[1] + ';' + data[par1] + ';' + data[par2]
# print 'normalized %s' % classifier.data[i][0] + ';' + classifier.data[i][par1] + ';' + classifier.data[i][par2]
print '%s' % classifier.data[i][0] + ';' \
+ str(round(classifier.data[i][1][par1 - 2], FLOAT_SIZE)).replace('.', ',') + ';' \
+ str(round(classifier.data[i][1][par2 - 2], FLOAT_SIZE)).replace('.', ',')
i += 1
def findNearestNeighbourByCosineSimilarity(training_filename, test_filename):
"""Test the classifier on a test set of data"""
classifier = Classifier(training_filename)
f = open(training_filename)
lines = f.readlines()
f.close()
f = open(test_filename)
testLines = f.readlines()
f.close()
i = -1
cosines = []
expertData = []
for line in lines:
data = line.strip().split('\t')
print data, i
if i > -1:
#cosine = classifier.calculateCosineSimilarity(data, lines.pop(i))
cosine = classifier.calculateCosineSimilarity(data, testLines[0].strip().split('\t'))
cosines.append(cosine)
expertData.append(data)
##print data[1], ' par1: ', data[par1], 'par2: ', data[par2]
#print 'classifier.data: ', classifier.data[i][0], classifier.data[i][1]
##print '%s' % data[1] + ';' + data[par1] + ';' + data[par2]
# print 'normalized %s' % classifier.data[i][0] + ';' + classifier.data[i][par1] + ';' + classifier.data[i][par2]
#print '%s' % classifier.data[i][0] + ';' \
# + str(round(classifier.data[i][1][par1 - 2], FLOAT_SIZE)).replace('.', ',') + ';' \
# + str(round(classifier.data[i][1][par2 - 2], FLOAT_SIZE)).replace('.', ',')
i += 1
idx = 0
maxCosine = -1.0
bestMatchIdx = 0
print 'cosines', cosines
print 'expertData', expertData
for cosine in cosines:
if cosine > maxCosine:
maxCosine = cosine
bestMatchIdx = idx
idx +=1
print 'best match index', bestMatchIdx, 'maxCosine', maxCosine
print 'best match: ', expertData[bestMatchIdx]
##
## Here are examples of how the classifier is used for format visualisation.
##
###test('formatsTrainingSet.txt', 'formatsTestSet.txt')#
#visualize('formatsTrainingSet.txt', 4, 6)# sw/versions
#visualizeNormalized('formatsTrainingSet.txt', 4, 6)# sw/versions
#visualizeNormalized('formatsTrainingSet.txt', 4, 5)# sw/vendors
#visualizeNormalized('formatsTrainingSet.txt', 4, 3)# sw/os
####visualizeNormalized('formatsTrainingSet.txt', 4, 2)# sw/popular
##
## Here are examples of how the classifier is used for risk visualisation.
##
#findNearestNeighbourByCosineSimilarity('riskFactorsTrainingSet.txt', 'riskFactorsTestSet.txt')
#visualizeNormalized('riskFactorsTrainingSetVisualise.txt', 4, 6)# expert 3/expert 5
visualizeNormalized('riskFactorsTrainingSetVisualise.txt', 10, 12)# expert 9/institutional expert
##
## Here are examples of how the classifier is used on different data sets
## in the book.
##test('formatsTrainingSet-Sample.txt', 'formatsTestSet-Sample.txt')#
# test('athletesTrainingSet.txt', 'athletesTestSet.txt')
# test("irisTrainingSet.data", "irisTestSet.data")
# test("mpgTrainingSet.txt", "mpgTestSet.txt")
#unitTest()