-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path5_svm_classification.py
More file actions
50 lines (41 loc) · 1.13 KB
/
5_svm_classification.py
File metadata and controls
50 lines (41 loc) · 1.13 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
'''
We use this code for classifying the sentiment after extracting the features.
Classification is done using Support Vector Machines (SVMs)
I have also included KNearestNeighborsClassifier.
'''
import numpy as np
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
class predict:
arr1 = np.loadtxt('outputfiles/processed.txt')
print arr1.shape
train_x = arr1[:400,:-1]
train_y = arr1[:400,-1]
test_x = arr1[400:,:-1]
test_y = arr1[400:,-1]
print train_x.shape
#print train_y
print test_x.shape
#print test_y
def writedown(self,stuff,ofname):
ofile = open(ofname,'w')
for j in range(len(stuff)):
lol = int(stuff[j])
ofile.write(str(lol)+'\n')
def svm_predict(self):
clf = SVC(kernel='rbf')
clf.fit(self.train_x,self.train_y)
hola = clf.predict(self.test_x)
return hola
def knn_predict(self):
neigh = KNeighborsClassifier(n_neighbors=5)
neigh.fit(self.train_x, self.train_y)
hola = neigh.predict(self.test_x)
return hola
def main():
pred = predict()
res1 = pred.svm_predict()
res2 = pred.knn_predict()
pred.writedown(res1,'results/svmresults.txt')
if __name__ == '__main__':
main()