{"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\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-20T09:01:32.242333Z","iopub.execute_input":"2022-02-20T09:01:32.24287Z","iopub.status.idle":"2022-02-20T09:01:32.26766Z","shell.execute_reply.started":"2022-02-20T09:01:32.242751Z","shell.execute_reply":"2022-02-20T09:01:32.267021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = Path('../input/h-and-m-personalized-fashion-recommendations/')\nN = 12","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:01:32.269516Z","iopub.execute_input":"2022-02-20T09:01:32.270011Z","iopub.status.idle":"2022-02-20T09:01:32.274395Z","shell.execute_reply.started":"2022-02-20T09:01:32.269967Z","shell.execute_reply":"2022-02-20T09:01:32.273641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(data_path / 'transactions_train.csv',\n                 usecols = ['t_dat', 'customer_id', 'article_id'],\n                 dtype={'article_id': str})\n\ndf['t_dat'] = pd.to_datetime(df['t_dat'])\nlast_ts = df['t_dat'].max()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:01:32.275871Z","iopub.execute_input":"2022-02-20T09:01:32.276317Z","iopub.status.idle":"2022-02-20T09:02:45.806873Z","shell.execute_reply.started":"2022-02-20T09:01:32.276277Z","shell.execute_reply":"2022-02-20T09:02:45.806131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales = df.drop('customer_id', axis=1).groupby('article_id').count()\ngeneral_pred = sales['t_dat'].nlargest(N).index.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:04:34.511421Z","iopub.execute_input":"2022-02-20T09:04:34.511782Z","iopub.status.idle":"2022-02-20T09:04:40.311283Z","shell.execute_reply.started":"2022-02-20T09:04:34.51174Z","shell.execute_reply":"2022-02-20T09:04:40.310539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict = {}\n\nfor i in tqdm(df.index):\n    cust_id = df.at[i, 'customer_id']\n    art_id = df.at[i, 'article_id']\n    t_dat = df.at[i, 't_dat']\n\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    x = max(1, (last_ts - t_dat).days)\n\n    a, b, c, d = 2.5e4, 1.5e5, 2e-1, 1e3\n    y = a / np.sqrt(x) + b * np.exp(-c*x) - d\n\n    purchase_dict[cust_id][art_id] += max(0, y)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:04:43.857955Z","iopub.execute_input":"2022-02-20T09:04:43.858428Z","iopub.status.idle":"2022-02-20T09:14:29.716489Z","shell.execute_reply.started":"2022-02-20T09:04:43.858395Z","shell.execute_reply":"2022-02-20T09:14:29.715339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(data_path / 'sample_submission.csv')\n\npred_list = []\nfor cust_id in tqdm(sub['customer_id']):\n    if cust_id in purchase_dict:\n        series = pd.Series(purchase_dict[cust_id])\n        series = series[series > 0]\n        l = series.nlargest(N).index.tolist()\n        if len(l) < N:\n            l = l + general_pred[:(N-len(l))]\n    else:\n        l = general_pred\n    pred_list.append(' '.join(l))\n\nsub['prediction'] = pred_list\nsub.to_csv('submission.csv', index=None)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:02:51.643856Z","iopub.status.idle":"2022-02-20T09:02:51.644489Z","shell.execute_reply.started":"2022-02-20T09:02:51.644237Z","shell.execute_reply":"2022-02-20T09:02:51.644264Z"},"trusted":true},"execution_count":null,"outputs":[]}]}