{"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":"base on: https://www.kaggle.com/abhilashawasthi/not-so-fancy-but-fast-benchmark","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-10T12:15:55.543391Z","iopub.execute_input":"2022-02-10T12:15:55.543767Z","iopub.status.idle":"2022-02-10T12:15:55.569231Z","shell.execute_reply.started":"2022-02-10T12:15:55.543684Z","shell.execute_reply":"2022-02-10T12:15:55.568651Z"},"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-10T12:15:55.570438Z","iopub.execute_input":"2022-02-10T12:15:55.571181Z","iopub.status.idle":"2022-02-10T12:17:18.587566Z","shell.execute_reply.started":"2022-02-10T12:15:55.571148Z","shell.execute_reply":"2022-02-10T12:17:18.586304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(transactions.shape)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:18.589306Z","iopub.execute_input":"2022-02-10T12:17:18.589586Z","iopub.status.idle":"2022-02-10T12:17:18.621994Z","shell.execute_reply.started":"2022-02-10T12:17:18.589551Z","shell.execute_reply":"2022-02-10T12:17:18.62103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['t_dat'] = pd.to_datetime(transactions['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:18.623377Z","iopub.execute_input":"2022-02-10T12:17:18.623743Z","iopub.status.idle":"2022-02-10T12:17:25.492757Z","shell.execute_reply.started":"2022-02-10T12:17:18.6237Z","shell.execute_reply":"2022-02-10T12:17:25.491849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_3w = transactions[transactions['t_dat'] >= pd.to_datetime('2020-08-31')].copy()\ntransactions_2w = transactions[transactions['t_dat'] >= pd.to_datetime('2020-09-07')].copy()\ntransactions_1w = transactions[transactions['t_dat'] >= pd.to_datetime('2020-09-15')].copy()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:25.495501Z","iopub.execute_input":"2022-02-10T12:17:25.496176Z","iopub.status.idle":"2022-02-10T12:17:25.999204Z","shell.execute_reply.started":"2022-02-10T12:17:25.496127Z","shell.execute_reply":"2022-02-10T12:17:25.99848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3w = {}\n\nfor i,x in enumerate(zip(transactions_3w['customer_id'], transactions_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((transactions_3w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:26.001656Z","iopub.execute_input":"2022-02-10T12:17:26.00211Z","iopub.status.idle":"2022-02-10T12:17:27.344109Z","shell.execute_reply.started":"2022-02-10T12:17:26.002062Z","shell.execute_reply":"2022-02-10T12:17:27.343247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_2w = {}\n\nfor i,x in enumerate(zip(transactions_2w['customer_id'], transactions_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((transactions_2w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:27.345464Z","iopub.execute_input":"2022-02-10T12:17:27.345956Z","iopub.status.idle":"2022-02-10T12:17:28.240218Z","shell.execute_reply.started":"2022-02-10T12:17:27.345913Z","shell.execute_reply":"2022-02-10T12:17:28.237829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_1w = {}\n\nfor i,x in enumerate(zip(transactions_1w['customer_id'], transactions_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((transactions_1w['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:28.241405Z","iopub.execute_input":"2022-02-10T12:17:28.241852Z","iopub.status.idle":"2022-02-10T12:17:28.674953Z","shell.execute_reply.started":"2022-02-10T12:17:28.241816Z","shell.execute_reply":"2022-02-10T12:17:28.674077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T12:17:28.676316Z","iopub.execute_input":"2022-02-10T12:17:28.676712Z","iopub.status.idle":"2022-02-10T12:17:28.690702Z","shell.execute_reply.started":"2022-02-10T12:17:28.676659Z","shell.execute_reply":"2022-02-10T12:17:28.690056Z"},"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((transactions_1w['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-10T12:17:28.69168Z","iopub.execute_input":"2022-02-10T12:17:28.692817Z","iopub.status.idle":"2022-02-10T12:17:31.156659Z","shell.execute_reply.started":"2022-02-10T12:17:28.692781Z","shell.execute_reply":"2022-02-10T12:17:31.15607Z"},"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-10T12:17:31.157691Z","iopub.execute_input":"2022-02-10T12:17:31.157997Z","iopub.status.idle":"2022-02-10T12:17:44.312891Z","shell.execute_reply.started":"2022-02-10T12:17:31.157969Z","shell.execute_reply":"2022-02-10T12:17:44.312055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}