{"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":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\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-10T21:07:53.316977Z","iopub.execute_input":"2022-02-10T21:07:53.317932Z","iopub.status.idle":"2022-02-10T21:07:53.323545Z","shell.execute_reply.started":"2022-02-10T21:07:53.317877Z","shell.execute_reply":"2022-02-10T21:07:53.322722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = 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-10T21:07:53.325386Z","iopub.execute_input":"2022-02-10T21:07:53.325689Z","iopub.status.idle":"2022-02-10T21:08:58.980407Z","shell.execute_reply.started":"2022-02-10T21:07:53.325647Z","shell.execute_reply":"2022-02-10T21:08:58.979146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:08:58.981996Z","iopub.execute_input":"2022-02-10T21:08:58.982343Z","iopub.status.idle":"2022-02-10T21:08:59.000871Z","shell.execute_reply.started":"2022-02-10T21:08:58.982305Z","shell.execute_reply":"2022-02-10T21:08:58.99964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['t_dat'] = pd.to_datetime(df['t_dat'])\ndf['t_dat'].head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:08:59.003375Z","iopub.execute_input":"2022-02-10T21:08:59.003877Z","iopub.status.idle":"2022-02-10T21:09:03.850804Z","shell.execute_reply.started":"2022-02-10T21:08:59.003833Z","shell.execute_reply":"2022-02-10T21:09:03.849915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_3w = df[df['t_dat'] >= pd.to_datetime('2020-08-31')].copy()\ndf_2w = df[df['t_dat'] >= pd.to_datetime('2020-09-07')].copy()\ndf_1w = df[df['t_dat'] >= pd.to_datetime('2020-09-15')].copy()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:03.852163Z","iopub.execute_input":"2022-02-10T21:09:03.852585Z","iopub.status.idle":"2022-02-10T21:09:04.409484Z","shell.execute_reply.started":"2022-02-10T21:09:03.852553Z","shell.execute_reply":"2022-02-10T21:09:04.408634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_3w)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:04.410822Z","iopub.execute_input":"2022-02-10T21:09:04.41142Z","iopub.status.idle":"2022-02-10T21:09:04.417118Z","shell.execute_reply.started":"2022-02-10T21:09:04.411383Z","shell.execute_reply":"2022-02-10T21:09:04.416283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_2w.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:04.418502Z","iopub.execute_input":"2022-02-10T21:09:04.418727Z","iopub.status.idle":"2022-02-10T21:09:04.437613Z","shell.execute_reply.started":"2022-02-10T21:09:04.418699Z","shell.execute_reply":"2022-02-10T21:09:04.436769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_1w.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:04.439342Z","iopub.execute_input":"2022-02-10T21:09:04.439811Z","iopub.status.idle":"2022-02-10T21:09:04.45748Z","shell.execute_reply.started":"2022-02-10T21:09:04.439769Z","shell.execute_reply":"2022-02-10T21:09:04.456697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w = {}\n\nfor i,x in enumerate(zip(df_3w['customer_id'], df_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((df_3w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:04.458685Z","iopub.execute_input":"2022-02-10T21:09:04.458913Z","iopub.status.idle":"2022-02-10T21:09:05.469106Z","shell.execute_reply.started":"2022-02-10T21:09:04.458885Z","shell.execute_reply":"2022-02-10T21:09:05.46822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_2w = {}\n\nfor i,x in enumerate(zip(df_2w['customer_id'], df_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((df_2w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:05.471058Z","iopub.execute_input":"2022-02-10T21:09:05.471389Z","iopub.status.idle":"2022-02-10T21:09:06.157323Z","shell.execute_reply.started":"2022-02-10T21:09:05.471357Z","shell.execute_reply":"2022-02-10T21:09:06.156331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_1w = {}\n\nfor i,x in enumerate(zip(df_1w['customer_id'], df_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((df_1w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:06.158614Z","iopub.execute_input":"2022-02-10T21:09:06.160427Z","iopub.status.idle":"2022-02-10T21:09:06.474891Z","shell.execute_reply.started":"2022-02-10T21:09:06.160377Z","shell.execute_reply":"2022-02-10T21:09:06.473988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:09:06.476373Z","iopub.execute_input":"2022-02-10T21:09:06.476679Z","iopub.status.idle":"2022-02-10T21:09:06.487709Z","shell.execute_reply.started":"2022-02-10T21:09:06.476637Z","shell.execute_reply":"2022-02-10T21:09:06.486693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark = submission[['customer_id']]\nprediction_list = []\n\ndummy_list = list((df_2w['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_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)\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-10T21:09:06.489343Z","iopub.execute_input":"2022-02-10T21:09:06.489676Z","iopub.status.idle":"2022-02-10T21:09:08.444069Z","shell.execute_reply.started":"2022-02-10T21:09:06.489642Z","shell.execute_reply":"2022-02-10T21:09:08.443292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-10T21:18:31.029709Z","iopub.execute_input":"2022-02-10T21:18:31.030381Z","iopub.status.idle":"2022-02-10T21:18:37.763439Z","shell.execute_reply.started":"2022-02-10T21:18:31.030341Z","shell.execute_reply":"2022-02-10T21:18:37.762711Z"},"trusted":true},"execution_count":null,"outputs":[]}]}