-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSL.py
More file actions
137 lines (114 loc) · 4.12 KB
/
SSL.py
File metadata and controls
137 lines (114 loc) · 4.12 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
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
import numpy as np
import pandas as pd
from sklearn.semi_supervised import LabelPropagation
from sklearn.semi_supervised import LabelSpreading
from sklearn.metrics import accuracy_score
from sklearn.metrics.pairwise import cosine_similarity
from scipy import sparse
from plotFunctions import surface
from helper import createConfigName, generateVocabulary
from itertools import product
def SSL():
# PARAMETERS
RBF = 1
gammaArray = [0.5, 1, 5, 10, 20, 50, 100]
conversion = 'tfidf' # one of None, 'tfidf', 'MM', 'raw_tfidf'
nrLabeledData = 250
# Load Data
name = 'NG_guns_motorcycles'
filename = 'processedDocuments/' + name + '.pkl'
resultFilename = 'results/' + name + '_' + str(conversion) + '_'+ str(nrLabeledData) + '.txt'
results = pd.DataFrame(data = {'gamma': gammaArray})
for params in product((0,1), repeat=3):
cosSim = params[0]
avgFF = params[1]
useContextSim = params[2]
configName = createConfigName(cosSim, avgFF, useContextSim)
print configName
data = pd.read_pickle(filename)
vocabulary = generateVocabulary(data.sentences.tolist())
X = np.load('matrices/' + name + '.npy')
X = X[:-2,:-2]
contextSim = np.load('matrices/' + name + '_contextSim.npy')
nrDocs = len(data)
labels = np.ones([X.shape[0]])*-1
trueLabelIndex = range(0,nrLabeledData)
labels[trueLabelIndex] = data.loc[trueLabelIndex, 'category'].tolist()
# remove DD and FD
#X = X[:,nrDocs:]
# remove $Start$ and $End$
if useContextSim:
X[nrDocs:, nrDocs:] = contextSim
if avgFF:
FF = np.add(X[nrDocs:, nrDocs:], contextSim)/2
X[nrDocs:, nrDocs:] = FF
# Renormalize
#if renormalize:
# DF = X[nrDocs:, :nrDocs]
# rowsums = DF.sum(axis=1)
# for i in range(len(rowsums)):
# DF[i] = DF[i]/rowsums[i]
# X[nrDocs:,:nrDocs] = DF
# X[:nrDocs,nrDocs:] = np.transpose(DF)
#FF = X[nrDocs:,:]
#X[nrDocs:,:] = np.transpose(FF)
# Remove posts with no features
#DF = X[:nrDocs,:]
#indZeroFeatures = np.where(DF.sum(axis=1)==0)[0]
#for ind in indZeroFeatures:
# X = np.delete(X,ind,0)
#data.drop(data.index[indZeroFeatures], inplace=True)
#data.index = range(len(data))
#nrDocs = len(data)
# Normalize
#DF = X[:nrDocs,:]
#FF = X[nrDocs:,:]
#rowsum = DF.sum(axis=1)
#X[nrDocs:, nrDocs:] = np.transpose(X[nrDocs:, nrDocs:])
#X[-1,-1] = 1
#FF = X[nrDocs:, nrDocs:]
#FF_rowsum = FF.sum(axis=1)
if conversion=='tfidf':
DF = np.array(data.tfIdf.tolist())
X[:nrDocs, nrDocs:] = DF
#X[nrDocs:, :nrDocs] = np.transpose(DF)
#X = X[:,:]
if conversion=='raw_tfidf':
DF = np.array(data.tfIdf.tolist())
X = DF
if conversion=='MM':
DF = X[:nrDocs, nrDocs:]
FF = X[nrDocs:, nrDocs:]
X = np.dot(DF,FF)
if cosSim:
X = cosine_similarity(X,X)
print 'Label Propagation'
if RBF:
labelProp_accuracy = []
labelSpread_accuracy = []
for gamma in gammaArray:
print 'Gamma: %f' % gamma
labelPropagation = LabelPropagation('rbf', gamma=gamma, alpha=1, useInputMatrix=0, max_iter=100)
labelPropagation.fit(X, labels)
predictLabels = labelPropagation.transduction_
curr_acc = accuracy_score(data.category.tolist()[nrLabeledData:], predictLabels.tolist()[nrLabeledData:len(data)])
labelProp_accuracy.append(curr_acc)
print 'Label Prop. Test Accuracy: %f' % curr_acc
labelSpread = LabelSpreading('rbf', gamma=gamma, alpha=1)
#Test
labelSpread.fit(X,labels)
predictLabels = labelSpread.transduction_
curr_acc = accuracy_score(data.category.tolist()[nrLabeledData:], predictLabels.tolist()[nrLabeledData:len(data)])
labelSpread_accuracy.append(curr_acc)
print 'Label Spread. Test Accuracy: %f' % curr_acc
results[configName+'_LP'] = labelProp_accuracy
results[configName+'_LS'] = labelSpread_accuracy
else:
labelPropagation = LabelPropagation(alpha=1, useInputMatrix=1, max_iter=200)
print labelPropagation
labelPropagation.fit(X, labels)
predictLabels = labelPropagation.transduction_
print 'Test Accuracy: %f' % accuracy_score(data.category.tolist()[nrLabeledData+1:], predictLabels.tolist()[nrLabeledData+1:len(data)])
results.to_csv(resultFilename, sep='\t', encoding='utf-8')
if __name__ =='__main__':
SSL()