-
Notifications
You must be signed in to change notification settings - Fork 0
/
Learner.py
387 lines (348 loc) · 15.6 KB
/
Learner.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
from enum import Enum
import math
import pandas as pd
import numpy as np
import ClassificationInfo
import Kmeans
class Accuracy(Enum):
TP = 1
TN = 2
FP = 3
FN = 4
class Learner:
def __init__(self, data, classificationType, targetPlace):
self.classificationType = classificationType
self.size = data.shape[0]
self.targetPlace = targetPlace
self.threshold = None
self.k = None
self.kernel = None
if(self.classificationType == "regression"):
self.setThreshold(data)
self.data = data
self.euclidean = np.zeros((self.size, self.size))
self.pointIndex = {}
self.createDistances()
self.tuningData = self.getTuneData(data)
self.data = data.drop(self.tuningData.index)
if self.classificationType == "classification":
self.folds = self.crossValidation(self.data, self.targetPlace, True)
else:
self.folds = self.crossValidationRegression(self.data.copy(), self.targetPlace, False)
self.tuneData()
self.edited = pd.DataFrame()
def createDistances(self):
count = 0
print("Creating distances...")
n = self.data.shape[0]
#create distances for euclidean distance
for i in range(n):
print("on index ", i, " of ", n)
for j in range(i + 1, n):
distance = self.euclideanDistance(self.data.iloc[i], self.data.iloc[j], (count == 0))
self.pointIndex[str(self.data.iloc[i])] = i
self.pointIndex[str(self.data.iloc[j])] = j
self.euclidean[i, j] = distance
self.euclidean[j, i] = distance
count += 1
def setThreshold(self, data):
colAverage = data[self.targetPlace].mean()
self.threshold = colAverage * 0.1
def getTuneData(self, data):
print("Splitting data for tuning...")
# split data for tuning
tune_data = data.groupby(self.targetPlace, group_keys=False).apply(lambda x: x.sample(frac=0.1))
return tune_data
def tuneData(self):
print("Tuning data...")
# Tune the data
centerK = round(math.sqrt(self.size))
start = max(0, centerK - 2 * 2)
end = min(self.size, centerK + 2 * 2)
# Generate 5 values in the range
possibleK = list(range(start, end + 1, 2))
possibleKernel = [0.1, 0.5, 2, 5, 10]
count = 0
classifications = {}
if self.classificationType == "classification":
for k in possibleK:
train = self.data
accuracy = self.tuneClassification(k, train)
classifications[accuracy] = k
count += 1
best = max(classifications.keys())
self.k = classifications[best]
print("best k = ", self.k)
else:
for k in possibleK:
for kernel in possibleKernel:
train = self.data
accuracy = self.tuneRegression(k, kernel, train)
classifications[accuracy] = [k, kernel]
count += 1
best = max(classifications.keys())
self.k = classifications[best][0]
self.kernel = classifications[best][1]
print("best k = ", self.k)
print("best kernel = ", self.kernel)
def crossValidationRegression(self, cleanDataset, classColumn, printSteps):
print("Running cross validation with stratification...")
dataChunks = []
for i in range(10):
dataChunks.append(cleanDataset.sample(frac=0.1))
cleanDataset = cleanDataset.drop(dataChunks[i].index)
return dataChunks
def crossValidation(self, cleanDataset, classColumn, printSteps):
print("Running cross validation with stratification...")
# 10-fold cross validation with stratification of classes
if printSteps == True:
print("Running cross validation with stratification...")
dataChunks = [None] * 10
classes = np.unique(cleanDataset[classColumn])
dataByClass = dict()
for uniqueVal in classes:
# Subset data based on unique class values
classSubset = cleanDataset[cleanDataset[classColumn] == uniqueVal]
if printSteps == True:
print("Creating a subset of data for class " + str(uniqueVal) + " with size of " + str(classSubset.size))
dataByClass[uniqueVal] = classSubset
numRows = math.floor(classSubset.shape[0] / 10) # of class instances per fold
for i in range(9):
classChunk = classSubset.sample(n=numRows)
if printSteps:
print("Number of values for class " + str(uniqueVal), " in fold " + str(i+1) + " is: " + str(classChunk.shape[0]))
if dataChunks[i] is None:
dataChunks[i] = classChunk
else:
dataChunks[i] = pd.concat([dataChunks[i], classChunk])
classSubset = classSubset.drop(classChunk.index)
# the last chunk might be slightly different size if dataset size is not divisible by 10
if printSteps == True:
print("Number of values for class " + str(uniqueVal), " in fold " + str(10) + " is: " + str(classSubset.shape[0]))
dataChunks[9] = pd.concat([dataChunks[9], classSubset])
if printSteps == True:
for i in range(len(dataChunks)):
print("Size of fold " + str(i+1) + " is " + str(dataChunks[i].shape[0]))
return dataChunks
def editData(self):
#run classification and regression on edited data
self.kmeanClusters = self.edited.shape[0]
copy = self.data
copyFolds = self.folds
self.data = self.edited
if self.classificationType == "classification":
self.folds = self.crossValidation(self.data, self.targetPlace, False)
output = self.classification()
else:
self.folds = self.crossValidationRegression(self.data.copy(), self.targetPlace, False)
output = self.regression()
self.data = copy
self.folds = copyFolds
return output
def tuneClassification(self, k, train):
print("k = ", k)
sum = 0
total = 0
for i in range(self.tuningData.shape[0]):
neighbors = self.findNeighbors(self.tuningData.iloc[i], train, k)
correctClass = self.tuningData.iloc[i][self.targetPlace]
assignedClasses = {}
for neighbor in neighbors:
neighborClass = train.iloc[neighbor[1]][self.targetPlace]
if neighborClass in assignedClasses:
assignedClasses[neighborClass] += 1
else:
assignedClasses[neighborClass] = 1
assignedClass = max(assignedClasses, key=assignedClasses.get)
classAccuracy = self.classificationAccuracy(correctClass, assignedClass)
if(classAccuracy == Accuracy.TP or classAccuracy == Accuracy.TN):
sum += 1
total += 1
return sum/total
def tuneRegression(self, k, kernel , train):
sum = 0
total = 0
for i in range(self.tuningData.shape[0]):
neighbors = self.findNeighbors(self.tuningData.iloc[i], train ,k)
correctValue = self.tuningData.iloc[i][self.targetPlace]
numerator = 0
denominator = 0
for neighbor in neighbors:
weight = self.kernelWeight(self.tuningData.iloc[i], train.iloc[neighbor[1]], kernel)
value = train.iloc[neighbor[1]][self.targetPlace]
numerator += (weight * value)
denominator += weight
assignedValue = numerator/denominator
if self.regressionAccuracy(correctValue, assignedValue) == Accuracy.TP or self.regressionAccuracy(correctValue, assignedValue) == Accuracy.TN:
sum += 1
total += 1
return sum / total
def classification(self, printSteps=False):
count = 0
classification = ClassificationInfo.ClassificationInfo()
# return the classification info for each dataset
for fold in self.folds:
train = self.data.drop(fold.index)
total = 0
sum = 0
for i in range(fold.shape[0]):
neighbors = self.findNeighbors(fold.iloc[i], train)
if(printSteps and count == 0):
print("Classifying point ")
print(fold.iloc[i])
print()
print("Neighbors: ")
for neighbor in neighbors:
print(train.iloc[neighbor[1]])
correctClass = fold.iloc[i][self.targetPlace]
assignedClasses = {}
for neighbor in neighbors:
neighborClass = train.iloc[neighbor[1]][self.targetPlace]
if neighborClass in assignedClasses:
assignedClasses[neighborClass] += 1
else:
assignedClasses[neighborClass] = 1
assignedClass = max(assignedClasses, key=assignedClasses.get)
if(printSteps and count == 0):
print("Assigned class: ", assignedClass)
classAccuracy = self.classificationAccuracy(correctClass, assignedClass)
if(classAccuracy == Accuracy.TP or classAccuracy == Accuracy.TN):
data_df = pd.DataFrame([fold.iloc[i]])
self.edited = pd.concat([self.edited, data_df])
sum += 1
total += 1
classification.addTrueClass([correctClass, assignedClass])
classification.addConfusion(classAccuracy)
count += 1
return classification
def kernelWeight(self, point1, point2, kernel, printSteps=False):
#return weight
squaredDist = abs(self.euclideanDistance(point1, point2)) ** 2
if printSteps:
print("calculating euclidean distance between two points and taking absolute value squared...")
print("squared distance: ", squaredDist)
print("Taking the exponential of the squared distance over 2*kernel^2...")
return math.exp(-squaredDist / (2 * kernel ** 2))
def regression(self, printSteps=False):
classification = ClassificationInfo.ClassificationInfo()
count = 0
# return the regression classification info
# return the classification info for each dataset
for fold in self.folds:
train = self.data.drop(fold.index)
total = 0
sum = 0
for i in range(fold.shape[0]):
neighbors = self.findNeighbors(fold.iloc[i], train)
if(printSteps and count == 0):
print("Regressing point ")
print(fold.iloc[i])
print()
print("Neighbors: ")
for neighbor in neighbors:
print(train.iloc[neighbor[1]])
correctValue = fold.iloc[i][self.targetPlace]
numerator = 0
denominator = 0
for neighbor in neighbors:
weight = self.kernelWeight(fold.iloc[i], train.iloc[neighbor[1]], self.kernel, (printSteps and count == 0))
value = train.iloc[neighbor[1]][self.targetPlace]
numerator += (weight * value)
denominator += weight
if denominator == 0:
assignedValue = 0
else:
assignedValue = numerator/denominator
if(printSteps and count == 0):
print("Assigned value: ", assignedValue)
classAccuracy = self.regressionAccuracy(correctValue, assignedValue)
if(classAccuracy == Accuracy.TP or classAccuracy == Accuracy.TN):
data_df = pd.DataFrame([fold.iloc[i]])
self.edited = pd.concat([self.edited, data_df])
sum += 1
total += 1
classification.addTrueClass([correctValue, assignedValue])
classification.addConfusion(classAccuracy)
print("fold accuracy: ", sum/total)
return classification
def euclideanDistance(self, point1, point2, printSteps=False):
#return distance
if printSteps:
print("Calculating Euclidean distance between two points...")
featuresSum = 0
for i in range(self.data.shape[1]):
if(printSteps):
print("adding ", (point2.iloc[i] - point1.iloc[i])**2 , "to feature sum")
featuresSum += (point2.iloc[i] - point1.iloc[i])**2
if printSteps:
print("Taking the square root of the feature sum...")
return math.sqrt(featuresSum)
def classificationAccuracy(self, trueClass, assignedClass):
classNames = list(self.data[self.targetPlace].unique())
#decide where classification falls on confusion matrix
if trueClass == assignedClass:
if trueClass == classNames[0]:
return Accuracy.TP
else:
return Accuracy.TN
else:
if assignedClass == classNames[0]:
return Accuracy.FP
else:
return Accuracy.FN
def regressionAccuracy(self, trueValue, assignedValue):
#decide where regression falls on confusion matrix
if trueValue > assignedValue:
if trueValue - assignedValue < self.threshold:
return Accuracy.TP
else:
return Accuracy.FP
else:
if assignedValue - trueValue < self.threshold:
return Accuracy.TN
else:
return Accuracy.FN
def findNeighbors(self, point, train, k=-1):
#find the k nearest neighbors
distances = []
if( k == -1):
for i in range(train.shape[0]):
try:
f = self.pointIndex[str(train.iloc[i])]
except KeyError:
f = None
try:
j = self.pointIndex[str(point)]
except KeyError:
j = None
if(f == None or j == None):
distance = self.euclideanDistance(train.iloc[i], point)
else:
distance = self.euclidean[f][j]
distances.append([distance, i])
distances.sort()
return distances[:self.k]
else:
for i in range(train.shape[0]):
f = self.pointIndex[str(train.iloc[i])]
j = self.pointIndex[str(point)]
distance = self.euclidean[f][j]
distances.append([distance, i])
distances.sort()
return distances[:k]
def kmeans(self):
#run kmeans algorithm
kmeans = Kmeans.Kmeans(self.kmeanClusters, self.data)
centroids = kmeans.getCentroids()
copy = self.data
copyFolds = self.folds
self.data = centroids
if self.classificationType == "classification":
self.folds = self.crossValidationRegression(self.data.copy(), self.targetPlace, False)
output = self.classification()
else:
self.folds = self.crossValidationRegression(self.data.copy(), self.targetPlace, False)
output = self.regression()
self.data = copy
self.folds = copyFolds
return output