{"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-20T03:10:43.804909Z","iopub.execute_input":"2022-02-20T03:10:43.805405Z","iopub.status.idle":"2022-02-20T03:10:43.830130Z","shell.execute_reply.started":"2022-02-20T03:10:43.805301Z","shell.execute_reply":"2022-02-20T03:10:43.829516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"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-20T03:10:43.831848Z","iopub.execute_input":"2022-02-20T03:10:43.832324Z","iopub.status.idle":"2022-02-20T03:12:00.479899Z","shell.execute_reply.started":"2022-02-20T03:10:43.832281Z","shell.execute_reply":"2022-02-20T03:12:00.479033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T03:12:00.481677Z","iopub.execute_input":"2022-02-20T03:12:00.482116Z","iopub.status.idle":"2022-02-20T03:12:00.505387Z","shell.execute_reply.started":"2022-02-20T03:12:00.482075Z","shell.execute_reply":"2022-02-20T03:12:00.504582Z"},"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-20T03:12:00.507640Z","iopub.execute_input":"2022-02-20T03:12:00.507884Z","iopub.status.idle":"2022-02-20T03:12:07.299254Z","shell.execute_reply.started":"2022-02-20T03:12:00.507855Z","shell.execute_reply":"2022-02-20T03:12:07.298491Z"},"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-20T03:12:07.300553Z","iopub.execute_input":"2022-02-20T03:12:07.300845Z","iopub.status.idle":"2022-02-20T03:12:07.787577Z","shell.execute_reply.started":"2022-02-20T03:12:07.300805Z","shell.execute_reply":"2022-02-20T03:12:07.786889Z"},"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-20T03:12:07.788682Z","iopub.execute_input":"2022-02-20T03:12:07.789656Z","iopub.status.idle":"2022-02-20T03:12:09.160126Z","shell.execute_reply.started":"2022-02-20T03:12:07.789613Z","shell.execute_reply":"2022-02-20T03:12:09.159191Z"},"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-20T03:12:09.161428Z","iopub.execute_input":"2022-02-20T03:12:09.162314Z","iopub.status.idle":"2022-02-20T03:12:10.041034Z","shell.execute_reply.started":"2022-02-20T03:12:09.162262Z","shell.execute_reply":"2022-02-20T03:12:10.040121Z"},"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-20T03:12:10.042296Z","iopub.execute_input":"2022-02-20T03:12:10.042513Z","iopub.status.idle":"2022-02-20T03:12:10.449300Z","shell.execute_reply.started":"2022-02-20T03:12:10.042485Z","shell.execute_reply":"2022-02-20T03:12:10.448536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T03:12:10.450585Z","iopub.execute_input":"2022-02-20T03:12:10.450808Z","iopub.status.idle":"2022-02-20T03:12:10.460894Z","shell.execute_reply.started":"2022-02-20T03:12:10.450761Z","shell.execute_reply":"2022-02-20T03:12:10.460113Z"},"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-20T03:12:10.463042Z","iopub.execute_input":"2022-02-20T03:12:10.463348Z","iopub.status.idle":"2022-02-20T03:12:12.925239Z","shell.execute_reply.started":"2022-02-20T03:12:10.463314Z","shell.execute_reply":"2022-02-20T03:12:12.924058Z"},"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-20T03:12:12.926470Z","iopub.execute_input":"2022-02-20T03:12:12.926692Z","iopub.status.idle":"2022-02-20T03:12:25.670258Z","shell.execute_reply.started":"2022-02-20T03:12:12.926665Z","shell.execute_reply":"2022-02-20T03:12:25.669413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}