-
Notifications
You must be signed in to change notification settings - Fork 86
/
LastFM_util_functions.py
283 lines (230 loc) · 7.63 KB
/
LastFM_util_functions.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import pickle # Save model
# import matplotlib.pyplot as plt
import re # regular expression library
from random import random, choice # for random strategy
from operator import itemgetter
import numpy as np
from scipy.sparse import csgraph
from scipy.spatial import distance
from sklearn.cluster import KMeans
from sklearn.cluster import SpectralClustering
from sklearn.decomposition import TruncatedSVD
def generateUserFeature(W):
svd = TruncatedSVD(n_components=25)
result = svd.fit(W).transform(W)
return result
def vectorize(M):
temp = []
for i in range(M.shape[0] * M.shape[1]):
temp.append(M.T.item(i))
V = np.asarray(temp)
return V
def matrixize(V, C_dimension):
temp = np.zeros(shape=(C_dimension, len(V) / C_dimension))
for i in range(len(V) / C_dimension):
temp.T[i] = V[i * C_dimension : (i + 1) * C_dimension]
W = temp
return W
def readFeatureVectorFile(FeatureVectorsFileName):
FeatureVectors = {}
with open(FeatureVectorsFileName, "r") as f:
f.readline()
for line in f:
line = line.split("\t")
vec = line[1].strip("[]").strip("\n").split(";")
FeatureVectors[int(line[0])] = np.array(vec).astype(np.float)
return FeatureVectors
# This code simply reads one line from the source files of Yahoo!
def parseLine(line):
userID, tim, pool_articles = line.split("\t")
userID, tim = int(userID), int(tim)
pool_articles = np.array(pool_articles.strip("[").strip("]").strip("\n").split(","))
return userID, tim, pool_articles
def save_to_file(fileNameWrite, recordedStats, tim):
with open(fileNameWrite, "a+") as f:
f.write("data") # the observation line starts with data;
f.write("," + str(tim))
f.write("," + ";".join([str(x) for x in recordedStats]))
f.write("\n")
def initializeGW(Gepsilon, n, relationFileName):
W = np.identity(n)
with open(relationFileName) as f:
for line in f:
line = line.split("\t")
if line[0] != "userID":
if int(line[0]) <= n and (line[1]) <= n:
W[int(line[0])][int(line[1])] += 1
G = W
L = csgraph.laplacian(G, normed=False)
I = np.identity(n)
GW = I + Gepsilon * L # W is a double stochastic matrix
print(GW)
return GW.T
# generate graph W(No clustering)
def initializeW(n, relationFileName):
W = np.identity(n)
with open(relationFileName) as f:
for line in f:
line = line.split("\t")
if line[0] != "userID":
if int(line[0]) <= n and int(line[1]) <= n:
W[int(line[0])][int(line[1])] += 1
# print W[int(line[0])][int(line[1])]
row_sums = W.sum(axis=1)
NormalizedW = W / row_sums[:, np.newaxis]
W = NormalizedW
print(W.T)
print("Wtype", type(W))
# initializeW_clustering(n,relationFileName, 5)
return W.T
def initializeW_clustering(n, relationFileName, nClusters):
W = np.identity(n + 1)
with open(relationFileName) as f:
f.readline()
for line in f:
line = line.split("\t")
if int(line[0]) <= n and int(line[1]) <= n:
W[int(line[0])][int(line[1])] += 1
# KMeans
# SpectralClustering
spc = SpectralClustering(n_clusters=nClusters, affinity="precomputed")
# spc = SpectralClustering(n_clusters=nClusters)
spc.fit(W) # What is the meaning
label = spc.labels_
with open(relationFileName + ".cluster", "w") as f:
for i in range(n):
f.write(str(label[i]) + "\n")
NeighborW = np.zeros(shape=(nClusters, nClusters))
for i in range(n):
for j in range(n):
if label[i] == label[j]:
NeighborW[label[i]][label[j]] = 0
else:
NeighborW[label[i]][label[j]] += W[i][j]
NormalizedNeighborW = normalizeByRow(NeighborW)
newW = np.identity(nClusters) + NormalizedNeighborW
print("newW", newW)
NormalizednewW = normalizeByRow(newW)
print("NormalizednewW", NormalizednewW.T)
return NormalizednewW.T, newW, label
def initializeGW_clustering(Gepsilon, relationFileName, newW):
G = newW
n = newW.shape[0]
L = csgraph.laplacian(G, normed=False)
I = np.identity(n)
GW = I + Gepsilon * L # W is a double stochastic matrix
print(GW)
return GW.T
def initializeGW_label(Gepsilon, n, relationFileName, label, diagnol):
W = np.identity(n)
with open(relationFileName) as f:
for line in f:
line = line.split("\t")
if (
line[0] != "userID"
and label[int(line[0])] != 10000
and label[int(line[1])] != 10000
): # 10000 means not top 100 user.
W[label[int(line[0])]][label[int(line[1])]] += 1
# don't need it
"""
if diagnol=='1' or diagnol=='0':
for i in range(n):
W[i][i] = int(diagnol)
"""
G = W
L = csgraph.laplacian(G, normed=False)
I = np.identity(n)
GW = I + Gepsilon * L # W is a double stochastic matrix
print(GW)
return GW.T
# generate graph W(No clustering)
def initializeW_label(n, relationFileName, label, diagnol, show_heatmap):
W = np.identity(n)
with open(relationFileName) as f:
for line in f:
line = line.split("\t")
if (
line[0] != "userID"
and label[int(line[0])] != 10000
and label[int(line[1])] != 10000
): # 10000 means not top 100 user.
W[label[int(line[0])]][label[int(line[1])]] += 1
if show_heatmap:
heatmap(W)
# normalize
if is_number(diagnol):
for i in range(n):
W[i][i] = 0
W = normalizeByRow(W)
if show_heatmap:
heatmap(W)
for i in range(n):
W[i][i] = float(diagnol)
if show_heatmap:
heatmap(W)
if diagnol == "Max":
for i in range(n):
W[i][i] = 0
W = normalizeByRow(W)
if show_heatmap:
heatmap(W)
for i in range(n):
maxi = max(W[i])
W[i][i] = maxi
print(W)
if show_heatmap:
heatmap(W)
if diagnol == "Opt":
for i in range(n):
W[i][i] = 0
if sum(W[i] != 0):
W[i][i] = np.linalg.norm(W[i]) ** 2 / sum(W[i])
else:
W[i][i] = 1
print(W)
if show_heatmap:
heatmap(W)
W = normalizeByRow(W)
if show_heatmap:
heatmap(W)
print(W.T)
return W.T
def read_cluster_label(labelfile):
label = [0]
# fin = open(labelfile,'r')
for line in labelfile:
label.append(int(line))
return np.array(label)
def heatmap(X):
plt.pcolor(X)
plt.colorbar()
plt.show()
def normalizeByRow(Matrix):
row_sums = Matrix.sum(axis=1)
for i in range(len(row_sums)):
if row_sums[i] == 0:
row_sums[i] = 0.00000000000001
print(row_sums)
NormalizednewMatrix = Matrix / row_sums[:, np.newaxis]
return NormalizednewMatrix
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def model_dump(obj, filename, linenum):
fout = open(filename + ".txt", "w")
fout.write("line\t" + str(linenum))
fout.close()
fout = open(filename + ".model", "w")
pickle.dump(obj, fout)
fout.close()
def getcons(dim):
cons = []
cons.append({"type": "eq", "fun": lambda x: np.sum(x) - 1})
for i in range(dim):
cons.append({"type": "ineq", "fun": lambda x: x[i]})
cons.append({"type": "ineq", "fun": lambda x: 1 - x[i]})
return tuple(cons)