forked from HKUST-KnowComp/semihin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
196 lines (168 loc) · 6.95 KB
/
classifier.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
import numpy as np
import pickle as pk
from labelpropagation import LabelPropagation
class SSLClassifier:
def __init__(self, graph, label, classNames, LPParam, repeatTimes=10, trainNumbers=1,classCount={}):
# init
self.label = label
self.graph = graph
self.classNames = classNames
self.LPParam = LPParam
self.repeatTimes = repeatTimes
self.classCount = classCount
self.totalNum = 0.0
for k,v in self.classCount.items():
self.totalNum += v
# number of training data for each class
self.trainNumbers = trainNumbers
# default
self.trainLabel = {}
self.testLabel = {}
self.pred = {}
self.results = []
# definition
self.n = self.graph.shape[0]
self.c = len(classNames)
self.y = np.zeros((self.n,self.c))
def randomTrainTestSplit(self):
self.trainLabel = {}
self.testLabel = {}
train_labels = set()
for c in self.classNames:
c_train = [k for k,v in self.label.iteritems() if v == c]
c_label = np.random.permutation(len(c_train))[0:self.trainNumbers]
for l in c_label:
train_labels.add(c_train[l])
self.y = np.zeros((self.n,self.c))
for (k,v) in self.label.items():
if k not in train_labels:
self.testLabel[k] = v
else:
self.trainLabel[k] = v
cid = self.classNames.index(v)
self.y[k,cid] = self.totalNum / self.classCount[v]
def makey(self):
self.y = np.zeros((self.n,self.c))
for (k,v) in self.trainLabel.items():
cid = self.classNames.index(v)
self.y[k, cid] = 1 #/ self.classCount[v]
def runExperiment(self):
# run LP algorithm
lp = LabelPropagation(self.graph,self.y,self.LPParam)
lp.walk()
# convert pred probability to pred labels
pred = {}
trainProb = np.zeros((len(self.trainLabel),self.c))
testProb = np.zeros((len(self.testLabel),self.c))
for i,t in enumerate(self.testLabel):
pred[t] = self.classNames[np.argmax(lp.PredictedProbs[t, :])]
testProb[i, :] = lp.PredictedProbs[t, :]
for i,t in enumerate(self.trainLabel):
trainProb[i, :] = lp.PredictedProbs[t, :]
# calculate error rate
error_rate = self.precision(pred, self.testLabel)
return error_rate,trainProb,testProb,pred
'''
def precision(self, pred, test):
correct = 0.0
wrong = 0.0
for (d, l) in test.items():
if pred[d] == l:
correct += 1
else:
wrong += 1
return correct / (correct + wrong)
'''
def precision(self, pred, test):
correct = 0.0
wrong = 0.0
for (d, l) in test.items():
if pred[d] == l:
correct += 1.0 / self.classCount[l]
else:
wrong += 1.0 / self.classCount[l]
return correct / (correct + wrong)
def repeatedExperiment(self,savePathPrefix=None):
self.results = []
for r in range(self.repeatTimes):
self.randomTrainTestSplit()
if savePathPrefix:
self.saveFixedExperiment(savePathPrefix + str(r).zfill(3))
e, trainPred, testPred, pred = self.runExperiment()
self.results.append(e)
def saveFixedExperiment(self,pathPrefix):
with open(pathPrefix + '_train','w') as f:
pk.dump(self.trainLabel,f)
with open(pathPrefix + '_test', 'w') as f:
pk.dump(self.testLabel,f)
def repeatedFixedExperiment(self,pathPrefix):
self.results = []
for r in range(self.repeatTimes):
self.loadFixedExperiment(pathPrefix+str(r).zfill(3))
self.makey()
e, trainPred, testPred, pred = self.runExperiment()
self.results.append(e)
def loadFixedExperiment(self,pathPrefix):
with open(pathPrefix + '_train') as f:
self.trainLabel = pk.load(f)
with open(pathPrefix + '_test') as f:
self.testLabel = pk.load(f)
# These two methods fits when feature extraction change the Id (vectorize, cosine-graph)
def repeatedFixedExperimentwithNewIds(self,pathPrefix,newIds,saveProb=False,savePathPrefix='',savePred=False):
self.results = []
for r in range(self.repeatTimes):
self.loadFixedExperimentwithNewIds(pathPrefix+str(r).zfill(3),newIds)
self.makey()
e,trainProb,testProb,pred = self.runExperiment()
if saveProb:
with open(savePathPrefix + '_' + str(r).zfill(3) + '_train', 'w') as f:
pk.dump(trainProb,f)
with open(savePathPrefix + '_' + str(r).zfill(3) + '_test', 'w') as f:
pk.dump(testProb,f)
if savePred:
with open(savePathPrefix + '_' + str(r).zfill(3) + '_pred', 'w') as f:
pk.dump(pred, f)
self.results.append(e)
def repeatedFixedExpeimentwithInput(self,pathPrefix,newIds,saveProb=True,savePathPrefix='',savePred=False,inputPredPath=None):
beta = 0.9
self.results = []
for r in range(self.repeatTimes):
self.loadFixedExperimentwithNewIds(pathPrefix+str(r).zfill(3),newIds)
with open(inputPredPath + '_' + str(r).zfill(3)) as f:
inputPred = pk.load(f)
# generate y
self.MakeyAndPred(inputPred, beta)
e,trainProb,testProb,pred = self.runExperiment()
if saveProb:
with open(savePathPrefix + '_' + str(r).zfill(3) + '_train', 'w') as f:
pk.dump(trainProb,f)
with open(savePathPrefix + '_' + str(r).zfill(3) + '_test', 'w') as f:
pk.dump(testProb,f)
if savePred:
with open(savePathPrefix + '_' + str(r).zfill(3) + '_pred', 'w') as f:
pk.dump(pred, f)
self.results.append(e)
def loadPred(self,loadPath):
with open(loadPath) as f:
pred = pk.load(f)
return pred
def MakeyAndPred(self,pred, beta):
self.y = np.zeros((self.n,self.c))
for (k,v) in self.trainLabel.items():
cid = self.classNames.index(v)
self.y[k, cid] = 1 #/ self.classCount[v]
m = len(self.trainLabel)+len(self.testLabel)
self.y[0:m,:] = beta * self.y[0:m,:] + (1 - beta) * pred
def loadFixedExperimentwithNewIds(self,pathPrefix,newIds):
self.trainLabel = {}
self.testLabel = {}
with open(pathPrefix + '_train') as f:
self.trainLabel = pk.load(f)
with open(pathPrefix + '_test') as f:
self.testLabel = pk.load(f)
def stats(self):
mean = np.mean(self.results)
std = np.std(self.results)
print str(mean) + '\t' + str(std)
def get_mean(self):
return np.mean(self.results)