-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNB.py
138 lines (87 loc) · 3.11 KB
/
NB.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
from __future__ import division
from sklearn.naive_bayes import GaussianNB
import csv
import numpy as np
import time
import math
from sklearn.metrics import f1_score
# The code using features and labels start from here:
features = []
with open("features.csv") as feat:
entryreader = csv.reader(feat, delimiter=',')
for row in entryreader:
features.append(row)
featNames = features[0]
features = features[1:]
print len(features), len(features[0])
labels = []
with open("labels.csv") as lbl:
entryreader = csv.reader(lbl, delimiter=',')
for row in entryreader:
labels.append(row)
labelNames = labels[0]
labels = labels[1:]
print len(labels), len(labels[0])
labels = np.array(labels).astype(int)
features = np.array(features).astype(float)
labels = np.array(labels)
features = np.array(features)
featIdxMap = dict()
for i in range(len(featNames)):
featIdxMap[featNames[i]] = i
correlation = []
with open("feature_correlation_results.csv") as corr:
entryreader = csv.reader(corr, delimiter=',')
for row in entryreader:
correlation.append(row)
titleYearIdx = -1
for i in range(len(featNames)):
if featNames[i] == 'title_year':
titleYearIdx = i
print 'year index = ', titleYearIdx
trainRows = []
testRows = []
for i in range(len(features)):
if float(features[i][titleYearIdx]) > 2010: # Different folds for test data
testRows.append(i)
else:
trainRows.append(i)
print len(trainRows) / len(features) , len(testRows) / len(features)
favoriteCols = []
for i in range(1, len(correlation)):
if correlation[i][0] in featIdxMap:
if math.fabs(float(correlation[i][1])) > 0.1: # different correlation values for accuracy
print correlation[i][0]
favoriteCols.append(featIdxMap[correlation[i][0]])
print 'favoritCols = ', len(favoriteCols)
start_time = time.time()
#clf = svm.SVC(kernel='linear')
#### temporary customization of test and train
tmp = []
for i in range(len(testRows)):
if labels[testRows[i]][0] == 1:
tmp.append(testRows[i])
posNo = len(tmp)
for i in range(len(testRows)):
if labels[testRows[i]][0] == 0 and posNo > 0:
tmp.append(testRows[i])
posNo -= 1
testRows = tmp
print 'test length = ', len(testRows)
clf = GaussianNB() # classify test data using GNB
print 'here'
#(tf[:,[91,1063]])[[0,3,4],:]
clf.fit((features[trainRows, :])[:, favoriteCols], labels[trainRows, 0])
print 'there'
# Calculating Accuracy
y_pred = clf.predict((features[testRows, :])[:, favoriteCols])
y_test = labels[testRows, 0]
print 'accuracy = %f' %(np.mean((y_test-y_pred)==0))
# Calculating f-scores
#score = clf.score((features[testRows, :])[:, favoriteCols], labels[testRows, 0])
#print 'fscore = %s' % (f1_score(y_true, y_pred, average='macro') )
print("--- %s seconds ---" % (time.time() - start_time))
y = labels[testRows, 0]
print '======================'
for i in range(len(y_pred)):
print (testRows[i] + 2), ' = ', y_pred[i], ' ', y_test[i]