-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetMatrix.py
39 lines (28 loc) · 1.18 KB
/
getMatrix.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
import justTry
from numpy import array
from scipy.sparse import lil_matrix
from scipy.io import mmwrite
def getMatrix(filename, n=0, nump = True):
reviews = justTry.getUserReviews(n)
user_IDs = reviews.keys()
nUsers = len(user_IDs)
bus = justTry.getBusVector(n)
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
# full array is very large, outputting as Matrix Market format later on
rmatrix = lil_matrix((nUsers,nBus))
# for each userID (row), find all ratings and place them in the appropriate location in rmatrix
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
rmatrix[row,col] = revs[bid]
mmwrite(filename, rmatrix) # 70817 x 3654
return rmatrix
#getMatrix("factorization_trials/fact_ratings_matrix.txt")