forked from darthbhyrava/forum_classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvm_classifier.py
More file actions
75 lines (55 loc) · 1.72 KB
/
Copy pathsvm_classifier.py
File metadata and controls
75 lines (55 loc) · 1.72 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
from __future__ import division
from scipy.spatial.distance import pdist, squareform
from scipy import exp
from scipy.linalg import eigh
from sklearn import svm , preprocessing
from doc_helper import load_data_and_labels
import numpy as np
import pickle
import pdb
train_file = "./dataset/ICHI2016-TrainData.tsv"
test_file = "./dataset/new_ICHI2016-TestData_label.tsv"
def my_svm_classifier(train, l1, test, l2):
print ("Training data size", train.shape)
print ("SVM being trained")
clf = svm.SVC()
clf.fit(preprocessing.scale(train), l1)
#clf.fit(train, l1)
print ("SVM Trained successfully")
print ("Testing data size", test.shape)
hits = 0
output = clf.predict(preprocessing.scale(test))
#output = clf.predict(test))
for i in range(len(output)):
if l2[i] == output[i]:
hits += 1
print hits, "Accuracy", float(hits)/len(l2)*100
print hits, "/", len(l2), "FINAL Accuracy", float(hits)/len(l2)*100
if __name__ == "__main__":
d = {}
d['DEMO'] = 1
d['DISE'] = 2
d['TRMT'] = 3
d['GOAL'] = 4
d['PREG'] = 5
d['FAML'] = 6
d['SOCL'] = 7
doc_embed = pickle.load(open('./data/doc_embed.pkl', 'r'))
s = load_data_and_labels()
s2 = load_data_and_labels(test_file)
l1 = []
l2 = []
train = []
test = []
for i, x in enumerate(s):
Id = "question_train_"+str(i)
train.append(doc_embed[Id])
l1.append(d[x[2]])
train = np.asarray(train)
for i, x in enumerate(s2):
Id = "question_test_"+str(i)
test.append(doc_embed[Id])
l2.append(d[x[2]])
test = np.asarray(test)
my_svm_classifier(train, l1, test, l2)
print ("SVM code ran successfully")