{"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":"markdown","source":"# Is the date feature so important?\n\nRecently I learned that  [the test week is the same for all customers](https://www.kaggle.com/c/h-and-m-personalized-fashion-recommendations/discussion/306380).\nAfter that, I noticed that [this recent notebook](https://www.kaggle.com/hengzheng/time-is-our-best-friend) by @hengzheng utilized this point very elegantly.\n\n\nNow I changed the beginning of data aggregation from 2020-06-01 to 2020-09-01.\nThe score improved from 0.01 to 0.018 (current public 3rd). I beleave that the seasonal effect is very important in this competition.\n","metadata":{}},{"cell_type":"code","source":"import 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-09T06:23:33.674193Z","iopub.execute_input":"2022-02-09T06:23:33.674883Z","iopub.status.idle":"2022-02-09T06:23:33.704564Z","shell.execute_reply.started":"2022-02-09T06:23:33.674761Z","shell.execute_reply":"2022-02-09T06:23:33.703717Z"},"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)\n\nsubmission = pd.read_csv(data_path / 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-09T06:24:01.656691Z","iopub.execute_input":"2022-02-09T06:24:01.656979Z","iopub.status.idle":"2022-02-09T06:25:23.23575Z","shell.execute_reply.started":"2022-02-09T06:24:01.656945Z","shell.execute_reply":"2022-02-09T06:25:23.234523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(transactions.shape)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T06:25:23.238375Z","iopub.execute_input":"2022-02-09T06:25:23.238693Z","iopub.status.idle":"2022-02-09T06:25:23.276885Z","shell.execute_reply.started":"2022-02-09T06:25:23.238649Z","shell.execute_reply":"2022-02-09T06:25:23.276059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# only use data after 2020-06-01\n\ntransactions['t_dat'] = pd.to_datetime(transactions['t_dat'])\ntransactions = transactions[transactions['t_dat'] > pd.to_datetime('2020-09-01')]\nprint(transactions.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T06:29:11.099161Z","iopub.execute_input":"2022-02-09T06:29:11.099507Z","iopub.status.idle":"2022-02-09T06:29:19.07156Z","shell.execute_reply.started":"2022-02-09T06:29:11.099472Z","shell.execute_reply":"2022-02-09T06:29:19.070698Z"},"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-09T06:29:25.130397Z","iopub.execute_input":"2022-02-09T06:29:25.130672Z","iopub.status.idle":"2022-02-09T06:29:42.078853Z","shell.execute_reply.started":"2022-02-09T06:29:25.130642Z","shell.execute_reply":"2022-02-09T06:29:42.077845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T06:29:46.910341Z","iopub.execute_input":"2022-02-09T06:29:46.910666Z","iopub.status.idle":"2022-02-09T06:29:46.921715Z","shell.execute_reply.started":"2022-02-09T06:29:46.910629Z","shell.execute_reply":"2022-02-09T06:29:46.921114Z"},"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-09T06:29:51.426582Z","iopub.execute_input":"2022-02-09T06:29:51.427535Z","iopub.status.idle":"2022-02-09T06:30:00.866017Z","shell.execute_reply.started":"2022-02-09T06:29:51.427468Z","shell.execute_reply":"2022-02-09T06:30:00.865171Z"},"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-09T06:30:16.591733Z","iopub.execute_input":"2022-02-09T06:30:16.592029Z","iopub.status.idle":"2022-02-09T06:30:29.615801Z","shell.execute_reply.started":"2022-02-09T06:30:16.591998Z","shell.execute_reply":"2022-02-09T06:30:29.614877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}