{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"## Imports\nimport cudf\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom mlxtend.preprocessing import TransactionEncoder\nfrom mlxtend.frequent_patterns import apriori, association_rules, fpgrowth","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-01T02:18:05.828220Z","iopub.execute_input":"2022-12-01T02:18:05.828609Z","iopub.status.idle":"2022-12-01T02:18:05.834422Z","shell.execute_reply.started":"2022-12-01T02:18:05.828572Z","shell.execute_reply":"2022-12-01T02:18:05.833371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Read data\ntranscation_data = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntranscation_data['customer_id'] = transcation_data['customer_id'].str[-16:].str.hex_to_int().astype('int64')\ntranscation_data['article_id'] = transcation_data.article_id.astype('int32')\ntranscation_data.t_dat = cudf.to_datetime(transcation_data.t_dat)\ntranscation_data = transcation_data[['t_dat','customer_id','article_id', 'price']]\nprint( transcation_data.shape )\ntranscation_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:18:33.456322Z","iopub.execute_input":"2022-12-01T01:18:33.456696Z","iopub.status.idle":"2022-12-01T01:19:11.474519Z","shell.execute_reply.started":"2022-12-01T01:18:33.456664Z","shell.execute_reply":"2022-12-01T01:19:11.473538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Data preprocess\ntranscation_data.loc[:,'t_dat'] = cudf.to_datetime(transcation_data['t_dat'])\ntranscation_data.loc[:, 'year'] = transcation_data['t_dat'].dt.year\ntranscation_data.loc[:, 'month'] = transcation_data['t_dat'].dt.month\ntranscation_data.loc[:, 'day'] = transcation_data['t_dat'].dt.day\ntranscation_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:19:51.444474Z","iopub.execute_input":"2022-12-01T01:19:51.445166Z","iopub.status.idle":"2022-12-01T01:19:51.491496Z","shell.execute_reply.started":"2022-12-01T01:19:51.445126Z","shell.execute_reply":"2022-12-01T01:19:51.490440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_data.to_parquet('transcation_data_hm.pqt',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:20:42.140704Z","iopub.execute_input":"2022-12-01T01:20:42.141100Z","iopub.status.idle":"2022-12-01T01:20:42.861277Z","shell.execute_reply.started":"2022-12-01T01:20:42.141070Z","shell.execute_reply":"2022-12-01T01:20:42.860177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_data = pd.read_parquet(\"./transcation_data_hm.pqt\")","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:21:01.357203Z","iopub.execute_input":"2022-12-01T01:21:01.357588Z","iopub.status.idle":"2022-12-01T01:21:03.399216Z","shell.execute_reply.started":"2022-12-01T01:21:01.357531Z","shell.execute_reply":"2022-12-01T01:21:03.397652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Aprori\n\nRef: https://towardsdatascience.com/the-frequently-bought-together-recommendation-system-b4ed076b24e5\n\nLets try and apply the aprori algorithm that tries to generate association rules by pruning itemsets based on their support values, we will use the mlextend library to impliment this.","metadata":{}},{"cell_type":"code","source":"## Generate all article ids purchased by every customer\ntranscations_data_2020 = transcation_data[(transcation_data['year'] == 2020) & (transcation_data['month'] > 6) & (transcation_data['day'] > 20)]\ntranscation_aprori = transcations_data_2020.groupby('customer_id')['article_id'].unique().reset_index()\ntranscation_aprori","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:21:06.039431Z","iopub.execute_input":"2022-12-01T01:21:06.039806Z","iopub.status.idle":"2022-12-01T01:21:16.359402Z","shell.execute_reply.started":"2022-12-01T01:21:06.039775Z","shell.execute_reply":"2022-12-01T01:21:16.358491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Transcation encoder - encode all the order into one hot encoding to feed to the apriori algorithm\nte = TransactionEncoder()\nte.fit(transcation_aprori['article_id'])\norders_one_hot_encoded = te.transform(transcation_aprori['article_id'])\n\norders_one_hot_encoded = pd.DataFrame(orders_one_hot_encoded, columns =te.columns_)\norders_one_hot_encoded.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:21:21.730467Z","iopub.execute_input":"2022-12-01T01:21:21.731165Z","iopub.status.idle":"2022-12-01T01:21:24.037563Z","shell.execute_reply.started":"2022-12-01T01:21:21.731130Z","shell.execute_reply":"2022-12-01T01:21:24.036592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lets check the itemsets generated and the support value for each itemset","metadata":{}},{"cell_type":"code","source":"results = apriori(orders_one_hot_encoded, min_support=0.003, max_len=2, use_colnames=True)\nresults.sort_values(by=['support'])","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:25:27.192478Z","iopub.execute_input":"2022-12-01T01:25:27.193185Z","iopub.status.idle":"2022-12-01T01:25:40.302178Z","shell.execute_reply.started":"2022-12-01T01:25:27.193150Z","shell.execute_reply":"2022-12-01T01:25:40.301222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"association_rules = association_rules(results, metric=\"lift\")\nassociation_rules.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:25:40.303965Z","iopub.execute_input":"2022-12-01T01:25:40.304879Z","iopub.status.idle":"2022-12-01T01:25:40.320379Z","shell.execute_reply.started":"2022-12-01T01:25:40.304842Z","shell.execute_reply":"2022-12-01T01:25:40.319505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Oops we dont see any association rules using aprori might be due to very less support values for each itemset!!","metadata":{}},{"cell_type":"markdown","source":"### Lets try and see if FPGrowth algorithm helps us out here??","metadata":{}},{"cell_type":"code","source":"transcations_data_2020 = transcation_data[(transcation_data['year'] == 2020) & (transcation_data['month'] > 6)]\ntranscation_apri = transcations_data_2020.groupby('customer_id')['article_id'].unique().reset_index()\nte = TransactionEncoder()\nte.fit(transcation_apri['article_id'])\norders_1hot = te.transform(transcation_apri['article_id'])\norders_1hot = pd.DataFrame(orders_1hot, columns =te.columns_)\nfpgrowth(orders_1hot, min_support=0.6, use_colnames=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:25:40.321774Z","iopub.execute_input":"2022-12-01T01:25:40.322109Z","iopub.status.idle":"2022-12-01T01:26:36.946984Z","shell.execute_reply.started":"2022-12-01T01:25:40.322075Z","shell.execute_reply":"2022-12-01T01:26:36.939936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Same results!!","metadata":{}},{"cell_type":"markdown","source":"### Recommendation 2: combination of items purchased by every customer","metadata":{}},{"cell_type":"code","source":"transcation_aprori","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:39:08.160417Z","iopub.execute_input":"2022-12-01T01:39:08.161084Z","iopub.status.idle":"2022-12-01T01:39:08.175857Z","shell.execute_reply.started":"2022-12-01T01:39:08.161048Z","shell.execute_reply":"2022-12-01T01:39:08.174824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let visualize some of these recommendations","metadata":{}},{"cell_type":"code","source":"customers_data = cudf.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ncustomers_data['customer_id'] = customers_data['customer_id'].str[-16:].str.hex_to_int().astype('int64')\narticles_data = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-01T02:15:20.041592Z","iopub.execute_input":"2022-12-01T02:15:20.042001Z","iopub.status.idle":"2022-12-01T02:15:20.719783Z","shell.execute_reply.started":"2022-12-01T02:15:20.041968Z","shell.execute_reply":"2022-12-01T02:15:20.718785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Items that customers buy together\")\nfor _, data in transcation_aprori.head(5).iterrows():\n    path = \"../input/h-and-m-personalized-fashion-recommendations/images\"\n    f, ax = plt.subplots(1, len(data['article_id']), figsize=(6, 6))\n    for i, article in enumerate(data['article_id']):\n        file_name = \"0\" + str(article) + \".jpg\"\n        dir_name = \"0\" + str(article)[:2]\n        image = mpimg.imread(path + \"/\" + dir_name + \"/\" + file_name)\n        ax[i].imshow(image)\n        ax[i].set_xticks([], [])\n        ax[i].set_yticks([], [])\n        ax[i].grid(False)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T02:38:40.764727Z","iopub.execute_input":"2022-12-01T02:38:40.765173Z","iopub.status.idle":"2022-12-01T02:38:44.924473Z","shell.execute_reply.started":"2022-12-01T02:38:40.765133Z","shell.execute_reply":"2022-12-01T02:38:44.923487Z"},"trusted":true},"execution_count":null,"outputs":[]}]}