/
sparse_matrix.py
276 lines (224 loc) · 6.96 KB
/
sparse_matrix.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
#=*- coding:utf-8 -*-
from numpy import array,matrix,linalg,fliplr,zeros,dot
import random,cPickle
import time
#read atuhors
def authors():
counter = -1
authors_dict = dict()
datas = open('Author.csv','r')
for line in datas.readlines():
line = line.strip().split(',')
try:
aid = int(line[0])
counter += 1
authors_dict.setdefault( line[0],[line[1].lower(),counter] )
except:
pass
datas.close()
return authors_dict#dictionary
def authors_list():
authors_dict = []
datas = open('Author.csv','r')
for line in datas.readlines():
line = line.strip().split(',')
try:
aid = int(line[0])
authors_dict.append(line[0])
except:
pass
datas.close()
return authors_dict#list
def papers():
datas = open('Paper.csv','r')
papers_dict = []
BUFFER = 1024
content = datas.readlines(BUFFER)
while len(content) > 0:
for line in content:
line = line.strip().split(',')
try:
pid = int(line[0])
if type(pid) is int:
papers_dict.append(line[0])
except:
pass
content = datas.readlines(BUFFER)
datas.close()
return papers_dict#a list containing the paper id
def _init_paper_author(author_paper,author_csv,paper_id):
paper_author = open('PaperAuthor.csv','r')
BUFFER = 1024
buffer_content = paper_author.readlines(BUFFER)
#initial the author_paper matrix
while len(buffer_content) > 0:
for record in buffer_content:
record = record.strip().split(',')
try:
int(record[0])#judge whether it's a valiable paperId
pid = record[0]
aid = record[1]
pindex = paper_id.index(record[[0]])#hash the paperId to a sequential order
aindex = author_csv[aid][1]#hash
author_paper[aindex][pindex] += 1
except:
pass
buffer_content = paper_author.readlines(BUFFER)
paper_author.close()
print 'finish initilizing paper_author matrix'
return author_paper
def _init_author_matrix(author_matrix,author_paper,author_csv):
num_paper = author_paper.shape[1]
for j in range(0,len(author_csv) - 1):
for i in range(j + 1,len(author_csv)):
sum_paper = dot(author_paper[j],author_paper[i])
author_matrix[i][j] = sum_paper
author_matrix[j][i] = sum_paper
print 'finish initilizing author_matrix'
return author_matrix
if __name__ == '__main__':
author_id = authors_list()#return the authors dictionary
total_authors = len(author_id)
paper_id = papers()
total_papers = len(paper_id)
cPickle.dump(author_id,open('./plk/map_author_id.plk','w'))
cPickle.dump(paper_id,open('./plk/map_paper_id.plk','w'))
print 'save the author and paper id map'
BUFFER = 1024 #buffer for reading the large files
#import the paper-author relation
paper_author = dict()
pa_datas = open('PaperAuthor.csv','r')
buffer_content = pa_datas.readlines(BUFFER)
#initial the paper_author dict
counter = 0
author_dict = dict()
for i,v in enumerate(author_id ):
author_dict[v] = i
while len(buffer_content) > 0:
for record in buffer_content:
record = record.strip().split(',')
try:
int(record[0])#judge whether it's a valiable paperId
pid = record[0]
aid = record[1]
if pid not in paper_author:
paper_author[pid] = []
#there are some authors in the paperAuthors.csv but not in the author.csv
if aid in author_dict:
counter += 1
if counter % 100000 == 1:
print counter
author_index = author_dict[aid]
if author_index not in paper_author[pid]:
paper_author[pid].append(author_index)
except:
pass
buffer_content = pa_datas.readlines(BUFFER)
pa_datas.close()
print 'saving the paper_author dictionary variable'
new_pada = open('./plk/paper_author.plk','wb')
for paper in paper_author:
seg = ''
seg += paper
for author in paper_author[paper]:
seg += ' '
seg += author_id[author]
seg += '\n'
new_pada.write(seg)
new_pada.close()
print 'finish'
#initilize the author matrix
sparse_matrix = dict()
paper_counter = 0 #used to point out how many papers have been scaned
sparse_nodes = 0
print 'program run for %d , there are %d papers' %(time.clock(),len(paper_author) )
timeSpan = 0.
interval = 0.
for paper in paper_author:
paper_counter += 1
if paper_counter % 1000000 == 1:
interval = time.clock() - timeSpan
timeSpan += interval
print 'searching paper %d authors,spend %d m'%(paper_counter,interval / 60.)
print 'sparse matrix has ',sparse_nodes
authors = paper_author[paper]
#print 'paper %s has authors %d'%(paper,len(authors) )
for i in range(0,len(authors) - 1):
#there are some authors in the paperAuthors.csv but not in the author.csv
index_i = authors[i]
for j in range(i+1,len(authors) ):
#return the continue index
index_j = authors[j]
#build a upper triangle matrix
if index_i < index_j:
if index_i not in sparse_matrix:
sparse_matrix[index_i] = {index_j:1}
sparse_nodes += 1
elif index_j not in sparse_matrix[index_i]:
sparse_matrix[index_i][index_j] = 1
sparse_nodes += 1
else:
sparse_matrix[index_i][index_j] += 1
elif index_i > index_j:
if index_j not in sparse_matrix:
sparse_matrix[index_j] = {index_j:1}
sparse_nodes += 1
elif index_i not in sparse_matrix[index_j]:
sparse_matrix[index_j][index_i] = 1
sparse_nodes += 1
else:
sparse_matrix[index_j][index_i] += 1
days = timeSpan / (3600. * 24)
print 'spend %d days finish initialing the sparse matrix' %days
del paper_author
#save the triangle matrix
print 'save the triangle matrix'
amatrix = open('author_matrix.data','w')
amatrix.write('%sparse matrix market format file')
seg = str(len(author_id)) + ' '+ str(len(author_id)) + ' ' + str(sparse_nodes ) + '\n'
amatrix.write(seg)
for author in sparse_matrix:
co_authors = sparse_matrix[author]
for co_author in co_authors:
seg = str(author)
seg += ' '
seg += str(co_author)
seg += ' '
seg += str(co_authors[co_author])
seg += '\n'
amatrix.write(seg)
seg = str(co_author)
seg += ' '
seg += str(author)
seg += ' '
seg += str(co_authors[co_author])
seg += '\n'
amatrix.write(seg)
print 'enough for author_matrix'
def matrix_mf():
author_csv = authors()#return the authors dictionary
total_authors = len(author_csv)
paper_id = papers()
total_papers = len(paper_id)
author_matrix = zeros((total_authors,total_authors))
author_paper = zeros((total_authors,total_papers))
author_paper = _init_paper_author(author_paper,author_csv,paper_id)
author_matrix = _init_author_matrix(author_matrix,author_paper,author_csv)
evals,evec = linalg.eig(author_matrix)#return the eigenvalues and eigenvalues vectors
print 'catching the eigenvalues and eigeenvectors'
b = sort(evals)
b.sort(cmp = lambda x,y:cmp(y,x))
power = sum(b)
sub_power = 0
index = 0
for i in range(0,len(b)):
sub_power += b[i]
index += 1
if sub_power / power > 0.8:
break
idx = evals.argsort()
evec = evec[:,idx]
evec = fliplr(evec)[:,0:index]
print 'mapping the author_matrix to a new vector space'
new_user_matrix = dot(evec.T,author_matrix.T)
new_user_matrix.dump('user_feature_matrix.plk')