-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
213 lines (165 loc) · 7.25 KB
/
main.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
import numpy as np
import pandas as pd
from html.parser import HTMLParser
import html
from sklearn import preprocessing
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.utils import shuffle
## TODO cosine without new users
# just desc
# dec and categ (handle categories differently ?)
# dec and categ with NLP
# trends for new users
# new user cutoff search
# trends also for old users
# count add to carts ?
dfUsersPurchased = None
dfUsersAddedCart = None
userActionsCounts = None
dfProducts = None
dfUserActions = None
test = None
predictionsDF = None
train = None
topProducts = None
TRAIN_SPLIT = 0.6
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs= True
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(htmlString):
#there is some missing and or corrupt data so handle it properly
try:
#fuck you
htmlString = html.unescape(htmlString)
s = MLStripper()
s.feed(htmlString)
except:
return ""
return s.get_data()
def preProcess():
global dfUsersPurchased
global dfUsersAddedCart
global dfUsersViewed
global userActionsCounts
global dfProducts
global dfUserActions
dfUserActions = pd.read_csv("C:\\Users\\igorf\\PycharmProjects\\vinf2\\data\\vi_dataset_events.csv")
#remove view product events
dfUserActions = dfUserActions[dfUserActions.type != 'view_product']
timestamps = dfUserActions["timestamp"]
dfUserActions = dfUserActions.drop(columns=['timestamp'])
#separate events
dfUsersPurchased = dfUserActions[dfUserActions.type == 'purchase_item']
dfUsersAddedCart = dfUserActions[dfUserActions.type == 'add_to_cart']
dfUsersViewed = dfUserActions[dfUserActions.type == 'view_product']
#get number of actions for each user
userActionsCounts = dfUserActions.groupby(["customer_id", "type"]).count()
#rename column to avoid confusion
userActionsCounts.columns = ['count']
#usntack the hierarchical index created by grouping
userActionsCounts = userActionsCounts.unstack()
#replace nans
userActionsCounts = userActionsCounts.fillna(0)
dfUserActions = dfUserActions.join(timestamps)
#TODO add back the timestamps of purchases for recent trend analysis
dfProducts = pd.read_csv("C:\\Users\\igorf\\PycharmProjects\\vinf2\\data\\vi_dataset_catalog.csv")
#drop all products that have not been purchased or added to cart
#dfProducts = dfProducts[dfProducts['product_id'].isin(dfUserActions['product_id'])]
#strip html from description
dfProducts["description"] = dfProducts.apply(lambda row: strip_tags(row['description']), axis=1)
cunts = dfProducts[dfProducts.duplicated(["description", "category_path", "brand", "gender", "price"], keep=False)].sort_values(by=["description", "category_path", "brand", "gender", "price"])
prevRow = None
for index, row in cunts.iterrows():
if prevRow is not None:
if row.iloc[2:].equals(prevRow.iloc[2:]):
dfProducts.at[index, "product_id"] = prevRow["product_id"]
dfUserActions["product_id"] = dfUserActions["product_id"].replace(row["product_id"], prevRow["product_id"])
prevRow = row
#!!BEWARE some descriptions are empty, some contain no usefull information, and couple are in slovak so we need co clear them next
def buildModel():
global dfUsersPurchased
global dfUsersAddedCart
global dfUsersViewed
global userActionsCounts
global dfProducts
global dfUserActions
global test
global train
global predictionsDF
global topProducts
# drop all products that have not been purchased
# dfProducts = dfProducts[dfProducts['product_id'].isin(dfUsersPurchased['product_id'])]
#dfUsersPurchased = shuffle(dfUsersPurchased)
dfUserActions = dfUserActions.sort_values(by=["timestamp"])
split = int(dfUserActions.shape[0] * TRAIN_SPLIT)
train, test = dfUserActions.iloc[:split], dfUserActions.iloc[split:]
trainProd = dfProducts[dfProducts['product_id'].isin(train["product_id"])]
trainPurchased = train[train.type == 'purchase_item']
trainAddCart = train[train.type == 'add_to_cart']
#get top sold products
topProducts = dfUsersPurchased.groupby(["product_id"]).count()['customer_id'].sort_values(ascending=False)
topProducts.columns = ["count"]
#generate tfid matrix
tf = TfidfVectorizer(strip_accents="unicode", analyzer="word", stop_words="english", ngram_range=(1,2), min_df=2, norm="l1")
tfidMatrix = tf.fit_transform(trainProd["description"])
tfidMatrix = tfidMatrix.toarray()
tfidMatrix = np.transpose(tfidMatrix)
#generate user profiles
#split users
#create user product matrix columns are users, rows are products
trainPurchased["value"] = 1
upTrainPurchased = pd.pivot_table(trainPurchased, values="value", index=["product_id"], columns="customer_id", fill_value=0)
trainAddCart["value"] = 0
upTrainAddCart = pd.pivot_table(trainAddCart, values="value", index=["product_id"], columns="customer_id", fill_value=0)
updf = upTrainPurchased.add(upTrainAddCart, fill_value=0)
updf = updf.fillna(0)
min_max_scaler = preprocessing.MaxAbsScaler()
x_scaled = min_max_scaler.fit_transform(updf.values)
updf = pd.DataFrame(x_scaled, index=updf.index, columns=updf.columns.values)
userProfiles = np.matmul(tfidMatrix, updf.values)
#AT THIS POINT COLUMNS ARE USERS AND ROWS ARE TERMS
#now we transpose the tfid matrix back and multiply again
tfidMatrix = np.transpose(tfidMatrix)
predictions = np.matmul(tfidMatrix, userProfiles)
predictionsDF = pd.DataFrame(predictions, index=updf.index, columns=updf.columns.values)
def evalModel():
global dfUsersPurchased
global dfUsersAddedCart
global userActionsCounts
global dfProducts
global dfUserActions
global test
global train
global predictionsDF
global topProducts
test = test[test.type != 'add_to_cart']
train = train[train.type == 'purchase_item']
customers = pd.unique(test["customer_id"])
trendHits = 0
trendTotal = 0
predHits = 0
predTotal = 0
for i in customers:
numPurchased = train[train["customer_id"] == i].shape[0]
reccomendations = None
if numPurchased == 0:
reccomendations = topProducts.head(5).index
trendTotal = trendTotal + test[test["customer_id"] == i].shape[0]
trendHits = trendHits + pd.Series(reccomendations).isin(test.loc[test["customer_id"] == i, ["product_id"]]["product_id"]).sum()
else:
reccomendations = predictionsDF[i].sort_values(ascending=False).head(5).index
predTotal = predTotal + test[test["customer_id"] == i].shape[0]
predHits = predHits + pd.Series(reccomendations).isin(test.loc[test["customer_id"] == i, ["product_id"]]["product_id"]).sum()
print("Trend hit precision: " + str(trendHits/trendTotal) + " (" + str(trendHits) + " / " + str(trendTotal) + ")")
print("Prediction hit precision: " + str(predHits/predTotal) + " (" + str(predHits) + " / " + str(predTotal) + ")")
preProcess()
buildModel()
evalModel()