-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClassifier.py
374 lines (306 loc) · 15.5 KB
/
Classifier.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
import numpy as np
import sys,os
from sklearn.naive_bayes import GaussianNB
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import Perceptron
import src.inputReader as inputReader
import src.Bayes as Bayes
import src.performanceAnalyser as performanceAnalyser
import src.Preprocessing as Preprocessing
import src.kmeans as kmeans
import src.KNN as KNN
import src.Visualization as Visualization
import src.ROC as ROC
import src.linearLogisticModels as linearLogisticModels
import src.perceptron as perceptron
dists = {-1: "Ignore",0:"Gaussian", 1:"Multinomail"}
def performPCA(inputDataClass,reduced_columns):
############################################## PCA Visualisation #############################################
# #variance v/s n_components : Fashion MNIST
# start = 10
# stop = 500
# step = 15
# Visualization.var_vs_comp(inputDataClass.Train[:,:-1], start, stop, step)
########################################################### PCA #############################################
##### Our PCA ####
pca = Preprocessing.PCA(inputDataClass.Train[:,:-1], k = reduced_columns, whiten = False) ##### Hyperparameter ####
reduced_train = pca.reduce(inputDataClass.Train[:,:-1], True)
inputDataClass.Train = np.hstack((reduced_train,inputDataClass.Train[:,-1].reshape(-1,1)))
print("train_data reduced.")
print("Train data reduced to columns = "+str(reduced_train.shape[1]))
reduced_test = pca.reduce(inputDataClass.Test[:,:-1], False)
inputDataClass.Test = np.hstack((reduced_test,inputDataClass.Test[:,-1].reshape(-1,1)))
print("test_data reduced. ")
print("Test data reduced to columns = "+str(reduced_test.shape[1]))
### SKlearn PCA #####
# pca = PCA(n_components=80,whiten=False)
# pca.fit(inputDataClass.Train[:,:-1])
# reduced_train = pca.transform(inputDataClass.Train[:,:-1])
# inputDataClass.Train = np.hstack((reduced_train,inputDataClass.Train[:,-1].reshape(-1,1)))
# reduced_test = pca.transform(inputDataClass.Test[:,:-1])
# inputDataClass.Test = np.hstack((reduced_test,inputDataClass.Test[:,-1].reshape(-1,1)))
def normalizeData(inputDataClass):
######################################## Normalising Data ####################################
normalizer = Preprocessing.Normalise()
inputDataClass.Train = np.hstack((normalizer.scale(inputDataClass.Train[:,:-1],train=True),inputDataClass.Train[:,-1].reshape(-1,1)))
inputDataClass.Test = np.hstack((normalizer.scale(inputDataClass.Test[:,:-1],train=False),inputDataClass.Test[:,-1].reshape(-1,1)))
def performVisualizations(inputDataClass):
########################################### Visualizations ###################################################
# Visualization.visualizeDataCCD(np.vstack((inputDataClass.Train,inputDataClass.Test)))
correlation_dict = performanceAnalyser.getCorrelationMatrix(inputDataClass.Train)
Visualization.visualizeCorrelation(correlation_dict)
# Visualization.visualizeDataPoints(inputDataClass.Train)
# Visualization.comp_vs_var_accuracy()
# pass
def performBayes(inputDataClass, drawPrecisionRecall = False, drawConfusion = False):
"""################################# Bayes Classifier #############################################"""
##Sklearn
# print("\nSklearn Naive Bayes")
# clf = GaussianNB()
# clf.fit(inputDataClass.Train[:,:-1], inputDataClass.Train[:,-1])
# Ypred = clf.predict(inputDataClass.Train[:,:-1])
# Ytrue = inputDataClass.Train[:,-1]
# print("Training Accuracy = "+str(performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)))
# Ypred = clf.predict(inputDataClass.Test[:,:-1])
# Ytrue = inputDataClass.Test[:,-1]
# print("Testing Accuracy = "+str(performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)))
print("\nMy Naive Bayes")
# bayesClassifier = Bayes.Bayes(isNaive = False, distribution =[0 for i in range(inputDataClass.Train.shape[1]-1)])
bayesClassifier = Bayes.Bayes(isNaive = True, distribution =[0,0,1,1,0])
bayesClassifier.train(inputDataClass.Train)
print("Training of model done.")
Ypred = bayesClassifier.fit(inputDataClass.Train)
Ytrue = inputDataClass.Train[:,-1]
print("Training Accuracy = "+str(performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)))
Ypred = bayesClassifier.fit(inputDataClass.Test)
Ytrue = inputDataClass.Test[:,-1]
print("Testing Accuracy = "+str(performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)))
print("Prediction done.")
if drawConfusion:
confusion = performanceAnalyser.getConfusionMatrix(Ytrue,Ypred)
Visualization.visualizeConfusion(confusion)
if drawPrecisionRecall:
############################ precision-recall curve #############################
threshold = np.arange(0.9,0.1,-0.1)
probas = bayesClassifier.get_probas()
for dic in probas:
sums=0.0
for item in dic:
sums+=dic[item]
for item in dic:
dic[item] = dic[item]/sums
roc = ROC.Roc(Ytrue,probas,threshold,'')
roc.Roc_gen()
precision, recall, _ = precision_recall_curve(Ytrue, probas)
plt.step(recall, precision, color='b', alpha=0.2, where='post')
plt.fill_between(recall, precision, step='post', alpha=0.2,color='b')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision Recall Curve')
return Ytrue,Ypred
def performKMeans(inputDataClass,k,mode,num_runs,visualize=False):
covar = -1
if mode == 3:
covar = performanceAnalyser.getFullCovariance(inputDataClass.Train[:,:-1])
labels, means, rms, Ypred = kmeans.kfit(inputDataClass.Train[:,:-1],k,inputDataClass.Train[:,-1],inputDataClass.Test[:,:-1],num_runs = num_runs, mode = mode,covar=covar)
print("rms = "+str(rms))
print("Kmeans done")
Ytrue = inputDataClass.Test[:,-1]
print("Testing Accuracy = "+str(performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)))
if visualize:
Visualization.visualizeKMeans(inputDataClass.Train[:,:-1],labels,k)
print("Kmeans visualized")
return Ytrue,Ypred
def performKNN(inputDataClass, nearestNeighbours,mode,label_with_distance=False):
covar=-1
if mode == 3:
covar = performanceAnalyser.getFullCovariance(inputDataClass.Train[:,:-1])
knn = KNN.KNN(nearestNeighbours,inputDataClass.Train[:,:-1],inputDataClass.Test[:,:-1],inputDataClass.Train[:,-1],label_with_distance=label_with_distance, mode=mode, covar=covar)
knn.allocate()
Ypred = knn.labels
Ytrue = inputDataClass.Test[:,-1]
print("Testing Accuracy = "+str(performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)))
return Ytrue,Ypred
def performLinearModels(inputDataClass, maxDegree, isRegularized, lambd, isRegress = False, drawConfusion = False, drawScatter = False):
train_data = inputDataClass.Train
test_data = inputDataClass.Test
Ytrue = test_data[:,-1]
# Scikit-learn Regression
reg = LinearRegression(fit_intercept=True, normalize=False, copy_X=True).fit(train_data[:,:-1], train_data[:,-1])
Ypred = reg.predict(test_data[:,:-1])
if isRegress:
rms = performanceAnalyser.calcRootMeanSquareRegression(Ypred,Ytrue)
print("Linear model rms (Scikit-learn) "+str(rms))
if not isRegress:
Ypred = (Ypred > 0.5).astype(np.int)
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Linear model Accuracy (Scikit-learn)"+str(acc))
# Our implementation
linear_model = linearLogisticModels.LinearModels(maxDegree,isRegularized,lambd)
linear_model.train(train_data)
Ypred = linear_model.test(test_data[:,:-1], isRegress)
if isRegress:
rms = performanceAnalyser.calcRootMeanSquareRegression(Ypred,Ytrue)
print("Linear model rms "+str(rms))
r2Score = performanceAnalyser.R2(Ypred,Ytrue)
print("Linear model R2 Score "+str(r2Score))
if drawScatter:
Visualization.visualizeDataRegression(train_data[:,:-1],train_data[:,-1],linear_model.W)
if not isRegress:
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Linear model Accuracy "+str(acc))
if drawConfusion:
confusion = performanceAnalyser.getConfusionMatrix(Ytrue,Ypred)
Visualization.visualizeConfusion(confusion)
return Ytrue,Ypred
def performMultiClassLinear(inputDataClass,maxDegree,learnRate, isRegularized , lambd , drawConfusion = False):
multi_class_linear_model = linearLogisticModels.MultiClassLinear(maxDegree,learnRate,isRegularized,lambd)
multi_class_linear_model.train(inputDataClass.Train)
Ytrue = inputDataClass.Test[:,-1]
Ypred = multi_class_linear_model.test(inputDataClass.Test[:,:-1])
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Multi Class Linear model Accuracy "+str(acc))
if drawConfusion:
confusion = performanceAnalyser.getConfusionMatrix(Ytrue,Ypred)
Visualization.visualizeConfusion(confusion)
return Ytrue,Ypred
def performLogisticModels(inputDataClass, maxDegree , learnRate , GDthreshold,isRegularized , lambd , drawConfusion = False, drawLikelihood = False):
train_data = inputDataClass.Train
test_data = inputDataClass.Test
Ytrue = test_data[:,-1]
# Scikit Learn
phiX = linearLogisticModels.calcPhiX(train_data[:,:-1],maxDegree)
clf = LogisticRegression().fit(phiX, train_data[:,-1])
phiXTest = linearLogisticModels.calcPhiX(test_data[:,:-1],maxDegree)
Ypred = clf.predict(phiXTest)
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Logistic model Accuracy (Scikit-learn) "+str(acc))
# Our implementation
logistic_model = linearLogisticModels.LogisticModels(maxDegree,learnRate,GDthreshold, isRegularized, lambd)
logistic_model.train(train_data)
Ypred = logistic_model.test(test_data[:,:-1])
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Logistic model Accuracy "+str(acc))
if drawConfusion:
confusion = performanceAnalyser.getConfusionMatrix(Ytrue,Ypred)
Visualization.visualizeConfusion(confusion)
if drawLikelihood:
Visualization.visualizeLikelihoodvsIteration(logistic_model.likelihood)
return Ytrue,Ypred
def performMultiClassLogistic(inputDataClass,maxDegree,learnRate, GDthreshold, isRegularized , lambd , drawConfusion = False, drawSquaredLoss = False):
train_data = inputDataClass.Train
test_data = inputDataClass.Test
Ytrue = test_data[:,-1]
phiX = linearLogisticModels.calcPhiX(train_data[:,:-1],maxDegree)
clf = LogisticRegression(solver='lbfgs',multi_class='multinomial').fit(phiX, train_data[:,-1])
phiXTest = linearLogisticModels.calcPhiX(test_data[:,:-1],maxDegree)
Ypred = clf.predict(phiXTest)
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Logistic model Accuracy (Scikit-learn) "+str(acc))
#Our implementation
multi_class_logistic_model = linearLogisticModels.MultiClassLogistic(maxDegree,learnRate, GDthreshold,isRegularized,lambd)
multi_class_logistic_model.train(train_data)
Ypred = multi_class_logistic_model.test(inputDataClass.Test[:,:-1])
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Multi Class Logistic model Accuracy "+str(acc))
if drawConfusion:
confusion = performanceAnalyser.getConfusionMatrix(Ytrue,Ypred)
Visualization.visualizeConfusion(confusion)
if drawSquaredLoss:
Visualization.visualizeLossvsIteration(multi_class_logistic_model.squaredLoss)
return Ytrue,Ypred
def performPerceptron(inputDataClass,numIter, isBinary):
train_data = inputDataClass.Train
test_data = inputDataClass.Test
Ytrue = test_data[:,-1]
clf = Perceptron().fit(train_data[:,:-1], train_data[:,-1])
Ypred = clf.predict(test_data[:, :-1])
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Perceptron model Accuracy (Scikit-learn) "+str(acc))
# Our perceptron
if isBinary:
percept = perceptron.percep_2(train_data[:,:-1],train_data[:,-1],test_data[:,:-1])
else:
percept = perceptron.multi_perceptron(train_data[:,:-1],train_data[:,-1],test_data[:,:-1])
percept.process(numIter)
Ypred = percept.ypred()
acc = performanceAnalyser.calcAccuracyTotal(Ypred,Ytrue)
print("Perceptron model Accuracy "+str(acc))
return Ytrue,Ypred
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Invalid Format. Provide input file names")
exit()
inputDataFilePath = sys.argv[1]
mode = -1 # 0 for Medical; 1 for Fashion; 2 for Railway; 3 for river data
mod_dict = {0:'Medical_data', 1:'fashion-mnist', 2:'railway_Booking', 3:'River Data'}
inputDataFile = os.path.basename(inputDataFilePath)
if inputDataFile == 'Medical_data.csv':
mode = 0
x= []
x.append(inputDataFilePath)
if len(sys.argv) != 3:
print('Enter both train and test files')
exit()
x.append(sys.argv[2])
inputDataFilePath = x
elif inputDataFile == 'fashion-mnist_train.csv':
mode = 1
x= []
x.append(inputDataFilePath)
if len(sys.argv) != 3:
print('Enter both train and test files')
exit()
x.append(sys.argv[2])
inputDataFilePath = x
elif inputDataFile == 'railwayBookingList.csv':
mode = 2
elif inputDataFile == 'river_data.csv':
mode =3
if mode==-1:
print("Unknown Dataset. Enter valid dataset.")
exit()
train_test_ratio = 0.8
inputDataClass = inputReader.InputReader(inputDataFilePath,mode,train_test_ratio = train_test_ratio)
if mode == 1:
"""################################# PCA #############################################"""
reduced_columns = 80
performPCA(inputDataClass = inputDataClass, reduced_columns = reduced_columns)
"""################################# Normalisation #############################################"""
normalizeData(inputDataClass = inputDataClass)
"""################################# Visualization #############################################"""
# performVisualizations(inputDataClass = inputDataClass)
"""################################# Bayes #############################################"""
# Ytrue,Ypred = performBayes(inputDataClass = inputDataClass, drawPrecisionRecall = False, drawConfusion = False)
"""################################# Linear Models #############################################"""
Ytrue,Ypred = performLinearModels(inputDataClass = inputDataClass, maxDegree=4, isRegularized = True, lambd = 0.001, isRegress = True, drawConfusion = False, drawScatter = False)
# Ytrue,Ypred = performMultiClassLinear(inputDataClass = inputDataClass, maxDegree = 4, learnRate = 0.01, isRegularized = True, lambd = 10, drawConfusion = False)
"""################################# Logistic Models #############################################"""
# Ytrue,Ypred = performLogisticModels(inputDataClass = inputDataClass, maxDegree = 1, learnRate = 0.001, GDthreshold = 0.01, isRegularized = True, lambd = 0.01 , drawConfusion = True, drawLikelihood = False)
# Ytrue,Ypred = performMultiClassLogistic(inputDataClass = inputDataClass, maxDegree = 3, learnRate = 0.01, GDthreshold = 0.001,isRegularized = False, lambd = 1, drawConfusion = True, drawSquaredLoss = True)
"""################################# Perceptron #############################################"""
# Ytrue,Ypred = performPerceptron(inputDataClass = inputDataClass, numIter = 1000, isBinary = False)
"""################################# KMEANS #############################################"""
# k = 3 ### Hyperparameter ###
# mode = 0 # mode = {0 : Euclidean, 1: Manhattan, 2 : Chebyshev, 3: Mahalnobis}
# num_runs= 100
# Ytrue,Ypred = performKMeans(inputDataClass,k,mode,num_runs,visualize=False)
"""################################# KNN #############################################"""
# nearestNeighbours = 15 ### Hyperparameter ###
# mode = 0 # mode = {0 : Euclidean, 1: Manhattan, 2 : Chebyshev}
# performKNN(inputDataClass, nearestNeighbours,mode,label_with_distance=False)
"""###############################PRECISION-RECALL-F1##########################################"""
# print(Ytrue)
# print(Ypred)
# precision,recall, f1score = performanceAnalyser.goodness(Ytrue,Ypred)
# print("\nPrecision")
# print(precision)
# print("Recall")
# print(recall)
# print("F1 Score")
# print(f1score)