-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractionsMatrixGenerator.py
More file actions
72 lines (53 loc) · 1.99 KB
/
Copy pathinteractionsMatrixGenerator.py
File metadata and controls
72 lines (53 loc) · 1.99 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from scipy import sparse
from collections import defaultdict
import glob
import csv
import sys
import io
import numpy
# later fix this to look at all browse files
# customer_id,contact_id,product_id,event_date,url
# path = "fordocker/browse_production_siteId_=35569/*.csv"
# map_customerId_productIdList= {}
# for fname in glob.glob(path):
# allUsers = {}
allProducts = set()
map_customerId_productIdList = defaultdict(list)
# if True:
# fname = "fordocker/browse_production_siteId_=35569/part-r-00016-55b1cd2d-d2c7-43dc-ac2a-da953f82d47b.csv"
path = "fordocker/browse_production_siteId_=35569/*.csv"
for fname in glob.glob(path):
print(fname)
with open(fname, mode='r') as infile:
reader = csv.reader(infile)
next(reader, None)
for row in reader:
# print(row)
customer_id = row[0]
product_id = row[2]
allProducts.add(product_id)
map_customerId_productIdList[customer_id].append(product_id)
#ok now i have lists of products per user ... now what .... get set of users and set of products. for amounts.
allUsers = list(set(map_customerId_productIdList.keys()))
allProducts = list(allProducts)
map_userId_index = {}
map_index_userId = {}
for i, userId in enumerate(allUsers):
map_userId_index[userId] = i
map_index_userId[i] = userId
map_productId_index = {}
map_index_productId = {}
for i, productId in enumerate(allProducts):
map_productId_index[productId] = i
map_index_productId[i] = productId
interactions = numpy.zeros(shape=(len(allUsers),len(allProducts)))
for userIndex, userId in enumerate(allUsers):
productsList = map_customerId_productIdList[userId]
# print(userIndex, " ", userId)
# print(productsList)
for productId in productsList:
# print(productId)
productIndex = map_productId_index[productId]
interactions[userIndex][productIndex] += 1
#now convert to coo sparse matrix
interactions_sparse_coo_matrix = sparse.csr_matrix(interactions)