-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvm.py
258 lines (219 loc) · 7.57 KB
/
svm.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
#from sklearn import neighbors
import books as bks
import csv
import numpy as np
import math
from sklearn import svm
from sklearn import preprocessing as pp
numFolds = 4
def circInfo(data, threshold):
authDict = {}
for d in data:
if d.author[0] != '':
if d.author in authDict:
authCounts = authDict[d.author]
else:
authCounts = [0,0]
if d.circ >= threshold:
authCounts[0] = authCounts[0]+1
else:
authCounts[1] = authCounts[1]+1
authDict[d.author] = authCounts
return authDict
def calcThreshold(trainSet, form="median"):
cvalues = np.array([ b.circ for b in trainSet ])
avg = np.average(cvalues)
median = np.median(cvalues)
return avg if form == "avg" else median
def splitData(loans, fold, wordvecs = None):
trainSet = []
validSet =[]
i = 0
for book in loans:
i = i + 1
if i%numFolds != fold:
trainSet.append(book)
else:
validSet.append(book)
threshold = calcThreshold(trainSet)
authDict = circInfo(trainSet, threshold)
trainX = []
trainY = []
validX = []
validY = []
for book in trainSet:
if book.author[0] == '':
circ = [0,0]
else:
circ = authDict[book.author]
if wordvecs is None:
trainX.append([book.callno,book.year])
else:
trainX.append([book.callno,book.year] + wordvecs(book.deweyVectors))
if book.circ > threshold:
trainY.append(1)
else:
trainY.append(0)
for book in validSet:
if book.author in authDict:
circ = authDict[book.author]
else:
circ = [0,0]
if wordvecs is None:
validX.append([book.callno,book.year])
else:
validX.append([book.callno,book.year] + wordvecs(book.deweyVectors))
if book.circ > threshold:
validY.append(1)
else:
validY.append(0)
return (trainX, trainY, validX, validY, threshold, authDict)
def splitDataTest(loans, limit, wordvecs = None):
trainSet = loans[:limit]
validSet = loans[limit+1:]
threshold = calcThreshold(trainSet)
authDict = circInfo(trainSet, threshold)
trainX = []
trainY = []
validX = []
validY = []
for book in trainSet:
if book.author[0] == '':
circ = [0,0]
else:
circ = authDict[book.author]
if wordvecs is None:
trainX.append([book.callno,book.year])
else:
trainX.append([book.callno,book.year] + wordvecs(book.deweyVectors))
if book.circ > threshold:
trainY.append(1)
else:
trainY.append(0)
for book in validSet:
if book.author in authDict:
circ = authDict[book.author]
else:
circ = [0,0]
if wordvecs is None:
validX.append([book.callno,book.year])
else:
validX.append([book.callno,book.year] + wordvecs(book.deweyVectors))
if book.circ > threshold:
validY.append(1)
else:
validY.append(0)
return (trainX, trainY, validX, validY, threshold, authDict)
def wordVecSums(vecs):
sumVec = np.zeros(vecs[0].shape[0])
for v in vecs:
sumVec = np.add(sumVec, v)
return list(sumVec)
def first4(vecs):
allVecs = (vecs * int(math.ceil((4.0/len(vecs)))))[:4] if len(vecs) < 4 else vecs[:4]
return [ x for v in allVecs for x in list(v)]
def runClassifier(classifier, trainX = [], trainY= [], validX= [], validY= []):
success = 0
failure = 0
results=[]
truePos = 0
trueNeg = 0
totPos = 0
totNeg = 0
classifier.fit(trainX, trainY)
for i in range(len(validX)):
if validY[i]==1:
totPos += 1
else:
totNeg += 1
Z=classifier.predict(validX[i])
results.append((Z,validY[i],Z==validY[i]))
if Z==validY[i]:
success = success + 1
if validY[i] == 1:
truePos += 1
else:
trueNeg += 1
else:
failure = failure + 1
return (float(success)/(success+failure), truePos/float(totPos), trueNeg/float(totNeg),results)
if __name__ == "__main__":
libraries = [ l[0] for l in csv.reader(open("library_list2.csv", "r"))]
books = bks.Books(noWordVecs = True)
mms = pp.MinMaxScaler(copy = False)
for l in libraries:
loans = (books.libraryLoans(l)).values()
limit = int(round(0.8*len(loans)))
train = loans[:limit]
test = loans[limit+1:]
threshold = None
authDict = None
ks = ['linear', 'rbf', 'sigmoid', 'poly']
cs = [0.05, 0.5, 1.0, 2.5, 5.0]
wvs = [None]
for k in ks:
for c in cs:
for wv in wvs:
wvName = wv.__name__ if wv is not None else "None"
success=[]
sens = []
spec = []
# 10 fold validation
for fold in range(4):
"""
You can pass wordVecSums to get an extra feature set of 300 appended to it that sums all the word vectors OR
You can pass first4 that gets the first 4 vectors, if there are less than 4 vectors, it duplicates the entries till there are 4,
there will be a total of 1200 new features added as a result
"""
trainX, trainY, validX, validY, threshold, authDict = splitData(train, fold, wordvecs = wv)
trainX = mms.fit_transform(trainX)
validX = mms.fit_transform(validX)
clfType = "SVM"
classifier = svm.SVC(C = c, kernel = k, tol = 0.1)
success_rate, sensitivity,specificity,results = runClassifier(
classifier,
trainX = trainX,
trainY = trainY,
validX = validX,
validY = validY)
success.append(success_rate)
sens.append(sensitivity)
spec.append(specificity)
with open(clfType+'_results/'+'results_'+l[:9] +"_"+ k + "_" + str(c) + "_" + str(fold)+ '.csv','w') as csvfile:
writer = csv.writer(csvfile,delimiter = ',', quotechar='\"', quoting=csv.QUOTE_ALL)
for r in results:
writer.writerow(r)
print str(l) + "," + str(k) + "," + str(c) + "," + wvName + "," + str(round(np.average(success), 3)) + "," + str(round(np.average(sens), 3)) + "," + str(round(np.average(specificity), 3))
"""
Generate test results
"""
libraries = [ l[0] for l in csv.reader(open("library_list.csv", "r"))]
books = bks.Books(noWordVecs = True)
mms = pp.MinMaxScaler(copy = False)
for l in libraries:
loans = (books.libraryLoans(l)).values()
limit = int(round(0.8*len(loans)))
threshold = None
authDict = None
success=[]
sens = []
spec = []
trainX, trainY, validX, validY, threshold, authDict = splitDataTest(loans, limit, wordvecs = None)
trainX = mms.fit_transform(trainX)
validX = mms.fit_transform(validX)
clfType = "SVM"
classifier = svm.SVC(C = 0.5, kernel = 'rbf', tol = 0.1)
success_rate, sensitivity,specificity,results = runClassifier(
classifier,
trainX = trainX,
trainY = trainY,
validX = validX,
validY = validY)
success.append(success_rate)
sens.append(sensitivity)
spec.append(specificity)
with open(clfType+'_results/final_'+l+"_svm_results"+'.csv','w') as csvfile:
writer = csv.writer(csvfile,delimiter = ',', quotechar='\"', quoting=csv.QUOTE_ALL)
for r in results:
writer.writerow(r)
print str(l) + ","+ str(round(np.average(success), 3)) + "," + str(round(np.average(sens), 3)) + "," + str(round(np.average(specificity), 3))