{"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\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-10T18:27:48.094665Z","iopub.execute_input":"2022-02-10T18:27:48.095021Z","iopub.status.idle":"2022-02-10T18:27:48.106575Z","shell.execute_reply.started":"2022-02-10T18:27:48.094946Z","shell.execute_reply":"2022-02-10T18:27:48.105716Z"},"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-10T18:27:48.111272Z","iopub.execute_input":"2022-02-10T18:27:48.111970Z","iopub.status.idle":"2022-02-10T18:29:04.636607Z","shell.execute_reply.started":"2022-02-10T18:27:48.111936Z","shell.execute_reply":"2022-02-10T18:29:04.635108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:04.639280Z","iopub.execute_input":"2022-02-10T18:29:04.639594Z","iopub.status.idle":"2022-02-10T18:29:04.662359Z","shell.execute_reply.started":"2022-02-10T18:29:04.639563Z","shell.execute_reply":"2022-02-10T18:29:04.661479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['t_dat'] = pd.to_datetime(df['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:04.664546Z","iopub.execute_input":"2022-02-10T18:29:04.664905Z","iopub.status.idle":"2022-02-10T18:29:11.788506Z","shell.execute_reply.started":"2022-02-10T18:29:04.664874Z","shell.execute_reply":"2022-02-10T18:29:11.787427Z"},"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-10T18:29:11.790128Z","iopub.execute_input":"2022-02-10T18:29:11.790607Z","iopub.status.idle":"2022-02-10T18:29:12.470293Z","shell.execute_reply.started":"2022-02-10T18:29:11.790548Z","shell.execute_reply":"2022-02-10T18:29:12.469301Z"},"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-10T18:29:12.471913Z","iopub.execute_input":"2022-02-10T18:29:12.472597Z","iopub.status.idle":"2022-02-10T18:29:13.882890Z","shell.execute_reply.started":"2022-02-10T18:29:12.472545Z","shell.execute_reply":"2022-02-10T18:29:13.881786Z"},"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-10T18:29:13.884332Z","iopub.execute_input":"2022-02-10T18:29:13.884620Z","iopub.status.idle":"2022-02-10T18:29:14.859275Z","shell.execute_reply.started":"2022-02-10T18:29:13.884585Z","shell.execute_reply":"2022-02-10T18:29:14.857858Z"},"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-10T18:29:14.861341Z","iopub.execute_input":"2022-02-10T18:29:14.861774Z","iopub.status.idle":"2022-02-10T18:29:15.326909Z","shell.execute_reply.started":"2022-02-10T18:29:14.861729Z","shell.execute_reply":"2022-02-10T18:29:15.325920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T18:29:15.328475Z","iopub.execute_input":"2022-02-10T18:29:15.328750Z","iopub.status.idle":"2022-02-10T18:29:15.342444Z","shell.execute_reply.started":"2022-02-10T18:29:15.328716Z","shell.execute_reply":"2022-02-10T18:29:15.341332Z"},"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-10T18:29:15.343905Z","iopub.execute_input":"2022-02-10T18:29:15.344214Z","iopub.status.idle":"2022-02-10T18:29:18.193138Z","shell.execute_reply.started":"2022-02-10T18:29:15.344181Z","shell.execute_reply":"2022-02-10T18:29:18.189494Z"},"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-10T18:29:18.195037Z","iopub.execute_input":"2022-02-10T18:29:18.195344Z","iopub.status.idle":"2022-02-10T18:29:31.550685Z","shell.execute_reply.started":"2022-02-10T18:29:18.195301Z","shell.execute_reply":"2022-02-10T18:29:31.549889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}