-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.py
More file actions
257 lines (219 loc) · 6.99 KB
/
project1.py
File metadata and controls
257 lines (219 loc) · 6.99 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
import sys
import array
import copy
from sklearn import svm
from sklearn import linear_model
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
#from sklearn.neighbors.nearest_centroid import NearestCentroid
from sklearn.neighbors import NearestCentroid
def extractColumn(arg_matrix, i):
return [[row[i]] for row in arg_matrix]
def mergeColumn(a, b):
return [x + y for x, y in zip(a, b)]
def CreateDataSet(fea, dat):
newData = extractColumn(dat, fea[0])
newLab = array.array("i")
fea.remove(fea[0])
length = len(fea)
for i in range(0, length, 1):
temp = extractColumn(dat, fea[0])
newData = mergeColumn(newData, temp)
fea.remove(fea[0])
return newData
def PearsonCorrelation(x, y, fi):
sumX = 0
sumX2 = 0
ro = len(x)
co = len(x[0])
switch = 0
pc = array.array("f")
for i in range(0, co, 1):
switch += 1
sumY = 0
sumY2 = 0
sumXY = 0
for j in range(0, ro, 1):
if (switch == 1):
sumX += y[j]
sumX2 += y[j] ** 2
sumY += x[j][i]
sumY2 += x[j][i] ** 2
sumXY += y[j] * x[j][i]
r = (ro * sumXY - sumX * sumY) / ((ro * sumX2 - (sumX ** 2)) * (ro * sumY2 - (sumY ** 2))) ** (0.5)
pc.append(abs(r))
savedforPrinting = array.array("f")
myFeatures = array.array("i")
for i in range(0, fi, 1):
selectedFeatures = max(pc)
savedforPrinting.append(selectedFeatures)
featureIndex = pc.index(selectedFeatures)
pc[featureIndex] = -1
myFeatures.append(featureIndex)
return myFeatures
# Read data file
datafile = sys.argv[1]
data = []
with open(datafile, "r") as infile:
for line in infile:
temp = line.split()
l = array.array("i")
for i in temp:
l.append(int(i))
data.append(l)
# Read labels from file
labelfile = sys.argv[2]
trainlabels = array.array("i")
with open(labelfile, "r") as infile:
for line in infile:
temp = line.split()
trainlabels.append(int(temp[0]))
feat = 10
rows = len(data)
cols = len(data[0])
rowsl = len(trainlabels)
# Dimensionality Reduction
#print("Feature Selection has started\n")
neededFea = PearsonCorrelation(data, trainlabels, 2000)
#print("\nFeature Selection is completed\n\n", end="")
savedFea = copy.deepcopy(neededFea)
data1 = CreateDataSet(neededFea, data)
clf_svm = svm.SVC(gamma=0.001)
clf_log = linear_model.LogisticRegression()
clf_gnb = GaussianNB()
clf_nc = NearestCentroid()
allAccuracies = array.array("f")
allFeatures = []
accuracy_svm = 0
accuracy_score = 0
accuracy_log = 0
accuracy_gnb = 0
accuracy_nc = 0
my_accuracy = 0
iterations = 5
for i in range(iterations):
#print(i)
X_train, X_test, y_train, y_test = train_test_split(
data1, trainlabels, test_size=0.3)
newRows = len(X_train)
newCols = len(X_train[0])
newRowst = len(X_test)
newColst = len(X_test[0])
newRowsL = len(y_train)
PearFeatures = PearsonCorrelation(X_train, y_train, feat)
allFeatures.append(PearFeatures)
argument = copy.deepcopy(PearFeatures)
data_fea = CreateDataSet(argument, X_train)
clf_svm.fit(data_fea, y_train)
clf_log.fit(data_fea, y_train)
clf_gnb.fit(data_fea, y_train)
clf_nc.fit(data_fea, y_train)
TestFeatures = PearsonCorrelation(X_test, y_test, feat)
test_fea = CreateDataSet(TestFeatures, X_test)
len_test_fea = len(test_fea)
counter_svm = 0
counter_log = 0
counter_gnb = 0
counter_nc = 0
my_counter = 0
for j in range(0, len_test_fea, 1):
predLab_svm = int(clf_svm.predict([test_fea[j]]))
predLab_log = int(clf_log.predict([test_fea[j]]))
predLab_gnb = int(clf_gnb.predict([test_fea[j]]))
predLab_nc = int(clf_nc.predict([test_fea[j]]))
h = predLab_svm + predLab_log + predLab_gnb + predLab_nc
if (h >= 3):
my_predLab = 1
elif (h <= 1):
my_predLab = 0
else:
my_predLab = predLab_svm
if (my_predLab == y_test[j]):
my_counter += 1
if (predLab_svm == y_test[j]):
counter_svm += 1
if (predLab_log == y_test[j]):
counter_log += 1
if (predLab_gnb == y_test[j]):
counter_gnb += 1
if (predLab_nc == y_test[j]):
counter_nc += 1
accuracy_svm += counter_svm / len_test_fea
accuracy_log += counter_log / len_test_fea
accuracy_gnb += counter_gnb / len_test_fea
accuracy_nc += counter_nc / len_test_fea
my_accuracy += my_counter / len_test_fea
allAccuracies.append(my_counter / len_test_fea)
bestAc = max(allAccuracies)
bestInd = allAccuracies.index(bestAc)
bestFeatures = allFeatures[bestInd]
#print("\nFeatures: ", feat)
f2 = open("cs675_project1_features.txt", "w+")
originalFea = array.array("i")
for i in range(0, feat, 1):
realIndex = savedFea[bestFeatures[i]]
originalFea.append(realIndex)
print(realIndex, end = " ")
f2.write(str(realIndex) + " ")
#print(originalFea)
f2.close()
print()
# Calculate Accuracy
argument1 = copy.deepcopy(originalFea)
AccData = CreateDataSet(argument1, data)
clf_svm.fit(AccData, trainlabels)
clf_log.fit(AccData, trainlabels)
clf_gnb.fit(AccData, trainlabels)
clf_nc.fit(AccData, trainlabels)
svm_counter = 0
LeCounter = 0
k = len(AccData)
for i in range(0, k, 1):
predLab_svm = int(clf_svm.predict([AccData[i]]))
predLab_log = int(clf_log.predict([AccData[i]]))
predLab_gnb = int(clf_gnb.predict([AccData[i]]))
predLab_nc = int(clf_nc.predict([AccData[i]]))
h = predLab_svm + predLab_log + predLab_gnb + predLab_nc
if (h >= 3):
my_predLab = 1
elif (h <= 1):
my_predLab = 0
else:
my_predLab = predLab_svm
if (my_predLab == trainlabels[i]):
LeCounter += 1
if (predLab_svm == trainlabels[i]):
svm_counter += 1
FinalAcc = LeCounter / k
SVMAc = svm_counter / k
print("\nThe Accuracy: ", FinalAcc * 100)
# Read Test data
testfile = sys.argv[3]
testdata = []
with open(testfile, "r") as infile:
for line in infile:
temp = line.split()
l = array.array("i")
for i in temp:
l.append(int(i))
testdata.append(l)
argument2 = copy.deepcopy(originalFea)
testdata1 = CreateDataSet(argument2, testdata)
# create a file
f1 = open("cs675_project1_TestLabels", "w+")
for i in range(0, len(testdata1), 1):
lab1 = int(clf_svm.predict([testdata1[i]]))
lab2 = int(clf_log.predict([testdata1[i]]))
lab3 = int(clf_gnb.predict([testdata1[i]]))
lab4 = int(clf_nc.predict([testdata1[i]]))
h = lab1 + lab2 + lab3 + lab4
if (h >= 3):
f1.write(str(1) + " " + str(i) + "\n")
print(str(1) + " " + str(i) )
elif (h <= 1):
f1.write(str(0) + " " + str(i) + "\n")
print(str(0) + " " + str(i) )
else:
f1.write(str(lab1) + " " + str(i) + "\n")
print(str(lab1) + " " + str(i) )
#print("\nThe Predicted labels of the test data are saved in cs675_project1_TestLabels file")