{"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":"\n\nimport numpy as np\nimport pandas as pd\n\nfrom pathlib import Path\n\ndata_path = Path('/kaggle/input/h-and-m-personalized-fashion-recommendations/')\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-25T09:43:20.767867Z","iopub.execute_input":"2022-05-25T09:43:20.768901Z","iopub.status.idle":"2022-05-25T09:43:20.798752Z","shell.execute_reply.started":"2022-05-25T09:43:20.768750Z","shell.execute_reply":"2022-05-25T09:43:20.796805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntransactions = pd.read_csv(\n    data_path / 'transactions_train.csv',\n    # set dtype or pandas will drop the leading '0' and convert to int\n    dtype={'article_id': str} \n)\n\nsubmission = pd.read_csv(data_path / 'sample_submission.csv')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:44:05.123870Z","iopub.execute_input":"2022-05-25T09:44:05.124106Z","iopub.status.idle":"2022-05-25T09:45:24.069177Z","shell.execute_reply.started":"2022-05-25T09:44:05.124083Z","shell.execute_reply":"2022-05-25T09:45:24.067512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(transactions.shape)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:45:24.079697Z","iopub.execute_input":"2022-05-25T09:45:24.079996Z","iopub.status.idle":"2022-05-25T09:45:24.106447Z","shell.execute_reply.started":"2022-05-25T09:45:24.079964Z","shell.execute_reply":"2022-05-25T09:45:24.105065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntransactions['t_dat'] = pd.to_datetime(transactions['t_dat'])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:45:24.116759Z","iopub.execute_input":"2022-05-25T09:45:24.116939Z","iopub.status.idle":"2022-05-25T09:45:32.588591Z","shell.execute_reply.started":"2022-05-25T09:45:24.116917Z","shell.execute_reply":"2022-05-25T09:45:32.587774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:46:45.760903Z","iopub.execute_input":"2022-05-25T09:46:45.761447Z","iopub.status.idle":"2022-05-25T09:46:45.777539Z","shell.execute_reply.started":"2022-05-25T09:46:45.761380Z","shell.execute_reply":"2022-05-25T09:46:45.775807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_3w = transactions[transactions['t_dat'] >= pd.to_datetime('2020-08-31')].copy()\ntransactions_2w = transactions[transactions['t_dat'] >= pd.to_datetime('2020-09-07')].copy()\ntransactions_1w = transactions[transactions['t_dat'] >= pd.to_datetime('2020-09-15')].copy()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:47:00.717471Z","iopub.execute_input":"2022-05-25T09:47:00.717709Z","iopub.status.idle":"2022-05-25T09:47:01.187760Z","shell.execute_reply.started":"2022-05-25T09:47:00.717685Z","shell.execute_reply":"2022-05-25T09:47:01.186492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(transactions_1w['t_dat'].min())\nprint(transactions_1w['t_dat'].max())\nprint(transactions_2w['t_dat'].min())\nprint(transactions_2w['t_dat'].max())\nprint(transactions_3w['t_dat'].min())\nprint(transactions_3w['t_dat'].max())","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:47:41.170990Z","iopub.execute_input":"2022-05-25T09:47:41.171630Z","iopub.status.idle":"2022-05-25T09:47:41.195343Z","shell.execute_reply.started":"2022-05-25T09:47:41.171603Z","shell.execute_reply":"2022-05-25T09:47:41.194513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_3w.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:51:16.247996Z","iopub.execute_input":"2022-05-25T09:51:16.248257Z","iopub.status.idle":"2022-05-25T09:51:16.261023Z","shell.execute_reply.started":"2022-05-25T09:51:16.248231Z","shell.execute_reply":"2022-05-25T09:51:16.260256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_3w[transactions_3w['customer_id']=='0001d44dbe7f6c4b35200abdb052c77a87596fe1bdcc37e011580a479e80aa94']","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:54:50.441863Z","iopub.execute_input":"2022-05-25T09:54:50.442148Z","iopub.status.idle":"2022-05-25T09:54:50.579670Z","shell.execute_reply.started":"2022-05-25T09:54:50.442118Z","shell.execute_reply":"2022-05-25T09:54:50.578988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w = {}\n\nfor i,x in enumerate(zip(transactions_3w['customer_id'], transactions_3w['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict_3w:\n        purchase_dict_3w[cust_id] = {}\n    \n    if art_id not in purchase_dict_3w[cust_id]:\n        purchase_dict_3w[cust_id][art_id] = 0\n    \n    purchase_dict_3w[cust_id][art_id] += 1\n    \nprint(len(purchase_dict_3w))\n\ndummy_list_3w = list((transactions_3w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:49:04.804306Z","iopub.execute_input":"2022-05-25T09:49:04.805540Z","iopub.status.idle":"2022-05-25T09:49:06.508057Z","shell.execute_reply.started":"2022-05-25T09:49:04.805497Z","shell.execute_reply":"2022-05-25T09:49:06.506610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w['0001d44dbe7f6c4b35200abdb052c77a87596fe1bdcc37e011580a479e80aa94']","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:52:53.728545Z","iopub.execute_input":"2022-05-25T09:52:53.728820Z","iopub.status.idle":"2022-05-25T09:52:53.736439Z","shell.execute_reply.started":"2022-05-25T09:52:53.728792Z","shell.execute_reply":"2022-05-25T09:52:53.735683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w['0014af8e431562a90a9b67913eaf5c183fc67d591c4c37681dfb0f898ea4e0b2']","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:53:10.875957Z","iopub.execute_input":"2022-05-25T09:53:10.876239Z","iopub.status.idle":"2022-05-25T09:53:10.883062Z","shell.execute_reply.started":"2022-05-25T09:53:10.876192Z","shell.execute_reply":"2022-05-25T09:53:10.881585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w['00197ba2c13240c7743853a7980c8ae4e2f76de0f9681fb259e91b61e4420484']","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:53:30.441045Z","iopub.execute_input":"2022-05-25T09:53:30.441785Z","iopub.status.idle":"2022-05-25T09:53:30.447167Z","shell.execute_reply.started":"2022-05-25T09:53:30.441744Z","shell.execute_reply":"2022-05-25T09:53:30.446470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w['00000dbacae5abe5e23885899a1fa44253a17956c6d1c3d25f88aa139fdfc657']","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:59:36.976800Z","iopub.execute_input":"2022-05-25T09:59:36.977445Z","iopub.status.idle":"2022-05-25T09:59:36.981783Z","shell.execute_reply.started":"2022-05-25T09:59:36.977384Z","shell.execute_reply":"2022-05-25T09:59:36.981194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_2w = {}\n\nfor i,x in enumerate(zip(transactions_2w['customer_id'], transactions_2w['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict_2w:\n        purchase_dict_2w[cust_id] = {}\n    \n    if art_id not in purchase_dict_2w[cust_id]:\n        purchase_dict_2w[cust_id][art_id] = 0\n    \n    purchase_dict_2w[cust_id][art_id] += 1\n    \nprint(len(purchase_dict_2w))\n\ndummy_list_2w = list((transactions_2w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:56:09.864180Z","iopub.execute_input":"2022-05-25T09:56:09.864450Z","iopub.status.idle":"2022-05-25T09:56:10.483714Z","shell.execute_reply.started":"2022-05-25T09:56:09.864424Z","shell.execute_reply":"2022-05-25T09:56:10.482935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_1w = {}\n\nfor i,x in enumerate(zip(transactions_1w['customer_id'], transactions_1w['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict_1w:\n        purchase_dict_1w[cust_id] = {}\n    \n    if art_id not in purchase_dict_1w[cust_id]:\n        purchase_dict_1w[cust_id][art_id] = 0\n    \n    purchase_dict_1w[cust_id][art_id] += 1\n    \nprint(len(purchase_dict_1w))\n\ndummy_list_1w = list((transactions_1w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:56:18.630236Z","iopub.execute_input":"2022-05-25T09:56:18.630529Z","iopub.status.idle":"2022-05-25T09:56:19.147579Z","shell.execute_reply.started":"2022-05-25T09:56:18.630498Z","shell.execute_reply":"2022-05-25T09:56:19.146210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark = submission[['customer_id']]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:56:43.558620Z","iopub.execute_input":"2022-05-25T09:56:43.559590Z","iopub.status.idle":"2022-05-25T09:56:43.583491Z","shell.execute_reply.started":"2022-05-25T09:56:43.559527Z","shell.execute_reply":"2022-05-25T09:56:43.582504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:56:46.597091Z","iopub.execute_input":"2022-05-25T09:56:46.597333Z","iopub.status.idle":"2022-05-25T09:56:46.608117Z","shell.execute_reply.started":"2022-05-25T09:56:46.597309Z","shell.execute_reply":"2022-05-25T09:56:46.606517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_list = []\n\ndummy_list = list((transactions_1w['article_id'].value_counts()).index)[:12]\ndummy_pred = ' '.join(dummy_list)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:57:09.900552Z","iopub.execute_input":"2022-05-25T09:57:09.900906Z","iopub.status.idle":"2022-05-25T09:57:09.932922Z","shell.execute_reply.started":"2022-05-25T09:57:09.900880Z","shell.execute_reply":"2022-05-25T09:57:09.932337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy_list","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:57:19.052504Z","iopub.execute_input":"2022-05-25T09:57:19.052916Z","iopub.status.idle":"2022-05-25T09:57:19.059702Z","shell.execute_reply.started":"2022-05-25T09:57:19.052890Z","shell.execute_reply":"2022-05-25T09:57:19.058812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy_pred","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:57:12.792090Z","iopub.execute_input":"2022-05-25T09:57:12.793107Z","iopub.status.idle":"2022-05-25T09:57:12.796986Z","shell.execute_reply.started":"2022-05-25T09:57:12.793080Z","shell.execute_reply":"2022-05-25T09:57:12.796602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['customer_id'].values.reshape((-1,))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:57:56.358920Z","iopub.execute_input":"2022-05-25T09:57:56.359559Z","iopub.status.idle":"2022-05-25T09:57:56.367183Z","shell.execute_reply.started":"2022-05-25T09:57:56.359525Z","shell.execute_reply":"2022-05-25T09:57:56.366334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, cust_id in enumerate(submission['customer_id'].values.reshape((-1,))):\n    if cust_id in purchase_dict_1w:\n        l = sorted((purchase_dict_1w[cust_id]).items(), key=lambda x: x[1], reverse=True)\n        l = [y[0] for y in l]\n        if len(l)>12:\n            s = ' '.join(l[:12])\n        else:\n            s = ' '.join(l+dummy_list_1w[:(12-len(l))])\n    elif cust_id in purchase_dict_2w:\n        l = sorted((purchase_dict_2w[cust_id]).items(), key=lambda x: x[1], reverse=True)\n        l = [y[0] for y in l]\n        if len(l)>12:\n            s = ' '.join(l[:12])\n        else:\n            s = ' '.join(l+dummy_list_2w[:(12-len(l))])\n    elif cust_id in purchase_dict_3w:\n        l = sorted((purchase_dict_3w[cust_id]).items(), key=lambda x: x[1], reverse=True)\n        l = [y[0] for y in l]\n        if len(l)>12:\n            s = ' '.join(l[:12])\n        else:\n            s = ' '.join(l+dummy_list_3w[:(12-len(l))])\n    else:\n        s = dummy_pred\n    prediction_list.append(s)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:58:29.564475Z","iopub.execute_input":"2022-05-25T09:58:29.564824Z","iopub.status.idle":"2022-05-25T09:58:31.473540Z","shell.execute_reply.started":"2022-05-25T09:58:29.564799Z","shell.execute_reply":"2022-05-25T09:58:31.472665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark['prediction'] = prediction_list\nprint(not_so_fancy_but_fast_benchmark.shape)\nnot_so_fancy_but_fast_benchmark.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:58:41.239269Z","iopub.execute_input":"2022-05-25T09:58:41.239573Z","iopub.status.idle":"2022-05-25T09:58:41.363177Z","shell.execute_reply.started":"2022-05-25T09:58:41.239544Z","shell.execute_reply":"2022-05-25T09:58:41.361932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark['customer_id'].iloc[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:59:28.534989Z","iopub.execute_input":"2022-05-25T09:59:28.535242Z","iopub.status.idle":"2022-05-25T09:59:28.542065Z","shell.execute_reply.started":"2022-05-25T09:59:28.535218Z","shell.execute_reply":"2022-05-25T09:59:28.541336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark['prediction'].iloc[0]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:00:00.577909Z","iopub.execute_input":"2022-05-25T10:00:00.578300Z","iopub.status.idle":"2022-05-25T10:00:00.585848Z","shell.execute_reply.started":"2022-05-25T10:00:00.578275Z","shell.execute_reply":"2022-05-25T10:00:00.584901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nnot_so_fancy_but_fast_benchmark.to_csv('not_so_fancy_but_fast_benchmark.csv', index=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:00:58.679225Z","iopub.execute_input":"2022-05-25T10:00:58.679740Z","iopub.status.idle":"2022-05-25T10:01:04.344965Z","shell.execute_reply.started":"2022-05-25T10:00:58.679713Z","shell.execute_reply":"2022-05-25T10:01:04.344065Z"},"trusted":true},"execution_count":null,"outputs":[]}]}