{"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":"from datetime import datetime\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/')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-10T13:18:17.762240Z","iopub.execute_input":"2022-02-10T13:18:17.763056Z","iopub.status.idle":"2022-02-10T13:18:17.767847Z","shell.execute_reply.started":"2022-02-10T13:18:17.763010Z","shell.execute_reply":"2022-02-10T13:18:17.766944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = 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)\ntransactions['t_dat'] = pd.to_datetime(transactions['t_dat'])\ntransactions['month'] = transactions['t_dat'].dt.month\n\nsubmission = pd.read_csv(data_path / 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:18:17.769343Z","iopub.execute_input":"2022-02-10T13:18:17.769552Z","iopub.status.idle":"2022-02-10T13:19:42.536863Z","shell.execute_reply.started":"2022-02-10T13:18:17.769526Z","shell.execute_reply":"2022-02-10T13:19:42.535833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(transactions.shape)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:19:42.538409Z","iopub.execute_input":"2022-02-10T13:19:42.538684Z","iopub.status.idle":"2022-02-10T13:19:42.559009Z","shell.execute_reply.started":"2022-02-10T13:19:42.538641Z","shell.execute_reply":"2022-02-10T13:19:42.558020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['t_dat'].min(), transactions['t_dat'].max()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:19:42.560588Z","iopub.execute_input":"2022-02-10T13:19:42.560943Z","iopub.status.idle":"2022-02-10T13:19:42.765977Z","shell.execute_reply.started":"2022-02-10T13:19:42.560890Z","shell.execute_reply":"2022-02-10T13:19:42.765068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['month'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:19:42.767987Z","iopub.execute_input":"2022-02-10T13:19:42.768397Z","iopub.status.idle":"2022-02-10T13:19:42.937282Z","shell.execute_reply.started":"2022-02-10T13:19:42.768350Z","shell.execute_reply":"2022-02-10T13:19:42.936510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filtering on date..inspired from this notebook https://www.kaggle.com/hengzheng/time-is-our-best-friend\n# Only picking summer months as test period is 1 week after 22nd september\n# transactions = transactions.loc[transactions['t_dat']>=datetime(2020, 9, 7)]\ntransactions = transactions.loc[transactions['month'].isin([4,5,6,7,8,9,10])]\ntransactions.reset_index(drop=True, inplace=True)\nprint(transactions.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:23:59.599861Z","iopub.execute_input":"2022-02-10T13:23:59.600967Z","iopub.status.idle":"2022-02-10T13:24:02.387396Z","shell.execute_reply.started":"2022-02-10T13:23:59.600890Z","shell.execute_reply":"2022-02-10T13:24:02.386263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict = {}\n\nfor i,x in enumerate(zip(transactions['customer_id'], transactions['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict:\n        purchase_dict[cust_id] = {}\n    \n    if art_id not in purchase_dict[cust_id]:\n        purchase_dict[cust_id][art_id] = 0\n    \n    purchase_dict[cust_id][art_id] += 1\n    \nprint(len(purchase_dict))","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:24:15.985377Z","iopub.execute_input":"2022-02-10T13:24:15.986087Z","iopub.status.idle":"2022-02-10T13:24:47.537704Z","shell.execute_reply.started":"2022-02-10T13:24:15.986038Z","shell.execute_reply":"2022-02-10T13:24:47.536681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:24:47.539425Z","iopub.execute_input":"2022-02-10T13:24:47.539960Z","iopub.status.idle":"2022-02-10T13:24:47.554098Z","shell.execute_reply.started":"2022-02-10T13:24:47.539920Z","shell.execute_reply":"2022-02-10T13:24:47.552925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark = submission[['customer_id']]\nprediction_list = []\ndummy_list = list((transactions['article_id'].value_counts()).index)[:12]\ndummy_pred = ' '.join(dummy_list)\n\nfor i, cust_id in enumerate(submission['customer_id'].values.reshape((-1,))):\n    if cust_id in purchase_dict:\n        l = sorted((purchase_dict[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[:(12-len(l))])\n    else:\n        s = dummy_pred\n    prediction_list.append(s)\n\nnot_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-02-10T13:24:47.555228Z","iopub.execute_input":"2022-02-10T13:24:47.555864Z","iopub.status.idle":"2022-02-10T13:25:02.640654Z","shell.execute_reply.started":"2022-02-10T13:24:47.555785Z","shell.execute_reply":"2022-02-10T13:25:02.639842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark.to_csv('not_so_fancy_but_fast_benchmark.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T13:25:02.642756Z","iopub.execute_input":"2022-02-10T13:25:02.643300Z","iopub.status.idle":"2022-02-10T13:25:15.701167Z","shell.execute_reply.started":"2022-02-10T13:25:02.643257Z","shell.execute_reply":"2022-02-10T13:25:15.700543Z"},"trusted":true},"execution_count":null,"outputs":[]}]}