Skip to content

Commit a7772f1

Browse files
Benny SBenny S
Benny S
authored and
Benny S
committed
RecSys2020Tutorial
1 parent 2640ca2 commit a7772f1

38 files changed

+38533
-0
lines changed

RecSys2020Tutorial/00_0_Initial.ipynb

+306
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# The MIT License (MIT)\n",
10+
"\n",
11+
"# Copyright (c) 2020, NVIDIA CORPORATION.\n",
12+
"\n",
13+
"# Permission is hereby granted, free of charge, to any person obtaining a copy of\n",
14+
"# this software and associated documentation files (the \"Software\"), to deal in\n",
15+
"# the Software without restriction, including without limitation the rights to\n",
16+
"# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\n",
17+
"# the Software, and to permit persons to whom the Software is furnished to do so,\n",
18+
"# subject to the following conditions:\n",
19+
"\n",
20+
"# The above copyright notice and this permission notice shall be included in all\n",
21+
"# copies or substantial portions of the Software.\n",
22+
"\n",
23+
"# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
24+
"# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\n",
25+
"# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\n",
26+
"# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n",
27+
"# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n",
28+
"# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"# Tutorial: Feature Engineering for Recommender Systems\n",
36+
"\n",
37+
"# 0. Initial Data Loading"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"In our tutorial, we use the [eCommerce behavior data from multi category store](https://www.kaggle.com/mkechinov/ecommerce-behavior-data-from-multi-category-store) from [REES46 Marketing Platform](https://rees46.com/) as our dataset. We define our own goal and filter the dataset accordingly. This jupyter notebook provides the code to preprocess the dataset and generate the train, validation and test sets for the remainder of the tutorial.<br><br>\n",
45+
"\n",
46+
"\n",
47+
"For our tutorial, we decided that our goal is to predict if a user purchased an item:\n",
48+
"\n",
49+
"<li> Positive: User purchased an item\n",
50+
"<li> Negative: User added an item to the cart, but did not purchase it (in the same session) \n",
51+
"<br><br> \n",
52+
"We split the dataset into train, validation and test set by the timestamp:\n",
53+
"<li> Training: October 2019 - February 2020\n",
54+
"<li> Validation: March 2020\n",
55+
"<li> Test: April 2020\n",
56+
"<br><br>\n",
57+
"We remove AddToCart Events from a session, if in the same session the same item was purchased."
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"First, download all csv files included in the Google Drive folder."
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 1,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"import pandas as pd\n",
74+
"import glob"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 2,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"list_files = glob.glob('./data/*.csv')"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Next, we process a single .csv file and extract/filter the rows"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 3,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"def process_files(file):\n",
100+
" df_tmp = pd.read_csv(file)\n",
101+
" df_tmp['session_purchase'] = df_tmp['user_session'] + '_' + df_tmp['product_id'].astype(str)\n",
102+
" df_purchase = df_tmp[df_tmp['event_type']=='purchase']\n",
103+
" df_cart = df_tmp[df_tmp['event_type']=='cart']\n",
104+
" df_purchase = df_purchase[df_purchase['session_purchase'].isin(df_cart['session_purchase'])]\n",
105+
" df_cart = df_cart[~(df_cart['session_purchase'].isin(df_purchase['session_purchase']))]\n",
106+
" df_cart['target'] = 0\n",
107+
" df_purchase['target'] = 1\n",
108+
" df = pd.concat([df_cart, df_purchase])\n",
109+
" df = df.drop('category_id', axis=1)\n",
110+
" df = df.drop('session_purchase', axis=1)\n",
111+
" df[['cat_0', 'cat_1', 'cat_2', 'cat_3']] = df['category_code'].str.split(\"\\.\", n = 3, expand = True).fillna('NA')\n",
112+
" df['brand'] = df['brand'].fillna('NA')\n",
113+
" df = df.drop('category_code', axis=1)\n",
114+
" df['timestamp'] = pd.to_datetime(df['event_time'].str.replace(' UTC', ''))\n",
115+
" df['ts_hour'] = df['timestamp'].dt.hour\n",
116+
" df['ts_minute'] = df['timestamp'].dt.minute\n",
117+
" df['ts_weekday'] = df['timestamp'].dt.weekday\n",
118+
" df['ts_day'] = df['timestamp'].dt.day\n",
119+
" df['ts_month'] = df['timestamp'].dt.month\n",
120+
" df['ts_year'] = df['timestamp'].dt.year\n",
121+
" df.to_csv('./' + file.replace('../data/', ''), index=False)"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 10,
127+
"metadata": {},
128+
"outputs": [
129+
{
130+
"data": {
131+
"text/plain": [
132+
"['./2019-Dec.csv',\n",
133+
" './2020-Jan.csv',\n",
134+
" './2020-Feb.csv',\n",
135+
" './2019-Oct.csv',\n",
136+
" './2020-Mar.csv',\n",
137+
" './2020-Apr.csv',\n",
138+
" './2019-Nov.csv']"
139+
]
140+
},
141+
"execution_count": 10,
142+
"metadata": {},
143+
"output_type": "execute_result"
144+
}
145+
],
146+
"source": [
147+
"list_files"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 4,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"for file in list_files:\n",
157+
" print(file)\n",
158+
" process_files(file)"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"Finally, we load all preprocessed csv files, combine them and create our train, validation and test sets."
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 13,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": [
174+
"lp = []\n",
175+
"list_files = glob.glob('./*.csv')"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 14,
181+
"metadata": {},
182+
"outputs": [
183+
{
184+
"name": "stderr",
185+
"output_type": "stream",
186+
"text": [
187+
"/conda/envs/rapids/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3146: DtypeWarning: Columns (11) have mixed types.Specify dtype option on import or set low_memory=False.\n",
188+
" interactivity=interactivity, compiler=compiler, result=result)\n"
189+
]
190+
}
191+
],
192+
"source": [
193+
"for file in list_files:\n",
194+
" lp.append(pd.read_csv(file))"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 16,
200+
"metadata": {},
201+
"outputs": [],
202+
"source": [
203+
"df = pd.concat(lp)"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 17,
209+
"metadata": {},
210+
"outputs": [
211+
{
212+
"data": {
213+
"text/plain": [
214+
"(16695562, 19)"
215+
]
216+
},
217+
"execution_count": 17,
218+
"metadata": {},
219+
"output_type": "execute_result"
220+
}
221+
],
222+
"source": [
223+
"df.shape"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 21,
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"df_test = df[df['ts_month']==4]\n",
233+
"df_valid = df[df['ts_month']==3]\n",
234+
"df_train = df[(df['ts_month']!=3)&(df['ts_month']!=4)]"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 22,
240+
"metadata": {},
241+
"outputs": [
242+
{
243+
"data": {
244+
"text/plain": [
245+
"((11461357, 19), (2461719, 19), (2772486, 19))"
246+
]
247+
},
248+
"execution_count": 22,
249+
"metadata": {},
250+
"output_type": "execute_result"
251+
}
252+
],
253+
"source": [
254+
"df_train.shape, df_valid.shape, df_test.shape"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": 25,
260+
"metadata": {},
261+
"outputs": [],
262+
"source": [
263+
"df_train.to_parquet('./data/train.parquet', index=False)"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 28,
269+
"metadata": {},
270+
"outputs": [],
271+
"source": [
272+
"df_valid.to_parquet('./data/valid.parquet', index=False)"
273+
]
274+
},
275+
{
276+
"cell_type": "code",
277+
"execution_count": 27,
278+
"metadata": {},
279+
"outputs": [],
280+
"source": [
281+
"df_test.to_parquet('./data/test.parquet', index=False)"
282+
]
283+
}
284+
],
285+
"metadata": {
286+
"kernelspec": {
287+
"display_name": "Python 3",
288+
"language": "python",
289+
"name": "python3"
290+
},
291+
"language_info": {
292+
"codemirror_mode": {
293+
"name": "ipython",
294+
"version": 3
295+
},
296+
"file_extension": ".py",
297+
"mimetype": "text/x-python",
298+
"name": "python",
299+
"nbconvert_exporter": "python",
300+
"pygments_lexer": "ipython3",
301+
"version": "3.7.8"
302+
}
303+
},
304+
"nbformat": 4,
305+
"nbformat_minor": 4
306+
}

0 commit comments

Comments
 (0)