Repository navigation
Expand file tree
/
Copy pathgetMatrixVW.py
More file actions
52 lines (40 loc) · 1.43 KB
/
Copy pathgetMatrixVW.py
File metadata and controls
52 lines (40 loc) · 1.43 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
import justTry
import string
# auxiliary function for the predictions file, if necessary
# ATTENTION: procudes 5GB+ text file
def makeVWLine(filename, row, nCols):
fin = open(filename, 'w')
for row in range(0, nUsers):
fin.write("\n".join(' |u '+str(row)+' |i '+str(i)+'\n' for i in range(0, nCols)))
if (row%5000) == 0:
print 'Wrote '+str(row)+' lines.'
fin.close()
# Generates two files:
# filename - sparse representation of ratings matris in Vowpal-Wabbit format
# filename_input.txt - predictions text file
def getMatrixVW(filename):
reviews = justTry.getUserReviews()
user_IDs = reviews.keys()
nUsers = len(user_IDs)
bus = justTry.getBusVector()
bus = bus[1]
bus_IDs = bus.keys()
nBus = len(bus_IDs)
# turn bus' value into column ids
for i, bid in enumerate(bus_IDs):
bus[bid] = i
f = open(filename, 'w')
# for each userID (row), write ratings to file
for row, uid in enumerate(user_IDs):
revs = reviews[uid] # reviews this user made (dic)
rev_IDs = reviews[uid].keys() # ids of businesses this user has reviewed
for bid in rev_IDs:
if bid in bus: # some businesses not in our list
col = bus[bid] # column associated with business
f.write(str(revs[bid])+' |u '+str(row)+' |i '+str(col)+'\n')
f.close()
# uncomment to output predictions file as well
# fname = string.split(filename,'.')
# fname = fname[0]+'_input.txt'
# makeVWLine(fname)
getMatrixVW("ratingsVW.txt")