-
Notifications
You must be signed in to change notification settings - Fork 0
/
getData.py
220 lines (161 loc) · 5.33 KB
/
getData.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
#import rpy2.robjects as robjects
import numpy as np
from aux import dictionate
import mycsv
#2410 docs
#4356 links
#2960 vocab
#136394 number of tokens
#-----------------------------------------------------------------------------------------
#------------------------------------ Cora -------------------------------------
#-----------------------------------------------------------------------------------------
# data is the matrix with columns doc,word,count
# dd is a list of numpy int arrays
def getData(path):
def rdatopy(path,filename):
# load your file
robjects.r['load'](path+filename+'.rda')
# retrieve the matrix that was loaded from the file
return robjects.r[filename]
#**1. Citation matrix
cites=rdatopy(path,'cora.cites')
dd=[]
for row in cites:
dd.append(np.array(row,dtype=np.int))
#**2. Documents
docs=rdatopy(path,'cora.documents')
data=[]
for docid,doc in enumerate(docs):
words,counts=np.array(doc,dtype=np.int)
for w,c in zip(words,counts):
data.append([docid,w,c])
return np.array(data,dtype=np.int),dd
#-----------------------------------------------------------------------------------------
#------------------------------------ ALL -------------------------------------
#-----------------------------------------------------------------------------------------
#Doc,word,others
def splitTrainTest(data,per):
data=np.array(data,dtype=np.int)
data=data[data[:,0].argsort()]
# Index into lists for each Doc
indexeddata={}
for row in data:
try:
indexeddata[row[0]].append(row[1:])
except KeyError:
indexeddata[row[0]]=[row[1:]]
# Split each Doc into train and test
traindata=[]
testdata=[]
for k,v in indexeddata.items():
trainlen=len(v)*per
if trainlen<5:
print "Warning: Document "+str(k)+" has only "+str(len(v))+" words in it"
for idx,elem in enumerate(v):
if idx<trainlen:
traindata.append([k]+list(elem)+[0])
else:
testdata.append([k]+list(elem))
return np.array(traindata,dtype=np.int),np.array(testdata,dtype=np.int)
# Doc,word,count
def splitTrainTestRepeated(data,per):
data=np.array(data,dtype=np.int)
data=data[data[:,0].argsort()]
# Index into lists for each Doc
indexeddata={}
for row in data:
try:
indexeddata[row[0]].append(row[1:])
except KeyError:
indexeddata[row[0]]=[row[1:]]
# Split each Doc into train and test
traindata=[]
testdata=[]
for k,v in indexeddata.items():
trainlen=len(v)*per
if trainlen<5:
print "Warning: Document "+str(k)+" has only "+str(len(v))+" words in it"
for idx,elem in enumerate(v):
for i in range(elem[1]):
#for i in range(1):
if idx<trainlen:
traindata.append([k,elem[0],0])
else:
testdata.append([k,elem[0]])
return np.array(traindata,dtype=np.int),np.array(testdata,dtype=np.int)
#-----------------------------------------------------------------------------------------
# ------------------------------------ DIGGS -------------------------------------
#-----------------------------------------------------------------------------------------
"""import nltk
from nltk.stem.porter import *
allwords=set()
alldocs=[]
def preprocessdiggs(path):
with open(path+"docs.txt") as f:
for doc in f.readlines():
doc=doc.lower()
words=nltk.word_tokenize(doc)
stemmer = PorterStemmer()
finaldoc=map(stemmer.stem,words)
for word in finaldoc:
allwords.add(word)
alldocs.append(finaldoc)
_,_,val2idxs,_=dictionate(np.array(list(allwords))[:,np.newaxis])
val2idxs=val2idxs[0]
with open(path+"diggs.dat") as f:
diggs=f.readlines()
finaldata=[]
for docid,(doc,count) in enumerate(zip(alldocs,diggs)):
for word in doc:
finaldata.append([docid,val2idxs[word],count])
return np.array(finaldata,dtype=np.int)
"""
#-----------------------------------------------------------------------------------------
# ---------------------------------- MOVIELENS -------------------------------------
#-----------------------------------------------------------------------------------------
def preprocessMovielens(path):
#** Split data into training and testing set
data=np.load(path+"/dictionateddata.npy")
return splitTrainTest2(data,0.7)
#-----------------------------------------------------------------------------------------
# ------------------------------------ UCI -------------------------------------
#-----------------------------------------------------------------------------------------
#** Takes a doc,word,count table and transforms it into repeated doc,word,topic
#def extend
def preprocessUCI(path):
#** Split data into training and testing set
data=mycsv.getCol(path,[0,1,2,2],delimiter=" ")
data=np.array(data,np.int)
return data-1
if __name__=="__main__":
#** CORA
if 0:
path="Datasets/cora/"
data,dd=getData(path)
#print data
#print dd
#print len(data)
data=data[data[:,0].argsort()]
train,test=splitTrainTest2(data,0.7)
print train
print test
#** Diggs
if 0:
path="Datasets/diggs/"
data=preprocessdiggs(path)
print data[:100]
print np.max(data[:][:,1])
#** UCI
if 0:
path="Datasets/uci/docword.nytimes1m.txt"
data=preprocessUCI(path)
print data[:10]
#** DBLP
if 1:
path="Datasets/dblp/"
data=np.load(path+"data.npy")
train,test=splitTrainTest(data,0.7)
print train[:10]
print test[:10]
np.save("train",train)
np.save("test",test)