{"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 os\nimport numpy as np\nimport pandas as pd\nimport gc\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:29.416948Z","iopub.status.idle":"2022-04-16T04:47:29.417769Z","shell.execute_reply.started":"2022-04-16T04:47:29.417477Z","shell.execute_reply":"2022-04-16T04:47:29.417509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/h-and-m-personalized-fashion-recommendations","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:29.419590Z","iopub.status.idle":"2022-04-16T04:47:29.420575Z","shell.execute_reply.started":"2022-04-16T04:47:29.420176Z","shell.execute_reply":"2022-04-16T04:47:29.420207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Articles**","metadata":{}},{"cell_type":"code","source":"articles= pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:20.160117Z","iopub.execute_input":"2022-04-16T04:46:20.160888Z","iopub.status.idle":"2022-04-16T04:46:20.786297Z","shell.execute_reply.started":"2022-04-16T04:46:20.160833Z","shell.execute_reply":"2022-04-16T04:46:20.785295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:20.787868Z","iopub.execute_input":"2022-04-16T04:46:20.788235Z","iopub.status.idle":"2022-04-16T04:46:20.813650Z","shell.execute_reply.started":"2022-04-16T04:46:20.788191Z","shell.execute_reply":"2022-04-16T04:46:20.812666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:20.816291Z","iopub.execute_input":"2022-04-16T04:46:20.816630Z","iopub.status.idle":"2022-04-16T04:46:20.824776Z","shell.execute_reply.started":"2022-04-16T04:46:20.816583Z","shell.execute_reply":"2022-04-16T04:46:20.823845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.info","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:20.826503Z","iopub.execute_input":"2022-04-16T04:46:20.826810Z","iopub.status.idle":"2022-04-16T04:46:20.882367Z","shell.execute_reply.started":"2022-04-16T04:46:20.826768Z","shell.execute_reply":"2022-04-16T04:46:20.881410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:20.884009Z","iopub.execute_input":"2022-04-16T04:46:20.884265Z","iopub.status.idle":"2022-04-16T04:46:20.957576Z","shell.execute_reply.started":"2022-04-16T04:46:20.884235Z","shell.execute_reply":"2022-04-16T04:46:20.956590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = articles.corr()\ncorr.style.background_gradient (cmap = \"copper\")","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:20.958854Z","iopub.execute_input":"2022-04-16T04:46:20.959378Z","iopub.status.idle":"2022-04-16T04:46:21.037484Z","shell.execute_reply.started":"2022-04-16T04:46:20.959343Z","shell.execute_reply":"2022-04-16T04:46:21.036855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Heatmap\ncorrmat = articles.corr()\nf, ax = plt.subplots(figsize=(15,15))\nsns.heatmap(corrmat, square=True, annot = True, annot_kws={'size':10})","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:21.038856Z","iopub.execute_input":"2022-04-16T04:46:21.039128Z","iopub.status.idle":"2022-04-16T04:46:22.200209Z","shell.execute_reply.started":"2022-04-16T04:46:21.039097Z","shell.execute_reply":"2022-04-16T04:46:22.199160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.plot()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:22.201957Z","iopub.execute_input":"2022-04-16T04:46:22.202296Z","iopub.status.idle":"2022-04-16T04:46:27.552495Z","shell.execute_reply.started":"2022-04-16T04:46:22.202252Z","shell.execute_reply":"2022-04-16T04:46:27.551572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**customers**","metadata":{}},{"cell_type":"code","source":"customers= pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:27.555159Z","iopub.execute_input":"2022-04-16T04:46:27.555385Z","iopub.status.idle":"2022-04-16T04:46:30.940693Z","shell.execute_reply.started":"2022-04-16T04:46:27.555358Z","shell.execute_reply":"2022-04-16T04:46:30.939780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:30.942779Z","iopub.execute_input":"2022-04-16T04:46:30.943144Z","iopub.status.idle":"2022-04-16T04:46:30.957696Z","shell.execute_reply.started":"2022-04-16T04:46:30.943098Z","shell.execute_reply":"2022-04-16T04:46:30.956691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:30.959559Z","iopub.execute_input":"2022-04-16T04:46:30.960047Z","iopub.status.idle":"2022-04-16T04:46:30.968793Z","shell.execute_reply.started":"2022-04-16T04:46:30.960009Z","shell.execute_reply":"2022-04-16T04:46:30.967990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.info","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:30.970197Z","iopub.execute_input":"2022-04-16T04:46:30.970718Z","iopub.status.idle":"2022-04-16T04:46:30.987667Z","shell.execute_reply.started":"2022-04-16T04:46:30.970684Z","shell.execute_reply":"2022-04-16T04:46:30.987099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:30.988615Z","iopub.execute_input":"2022-04-16T04:46:30.989216Z","iopub.status.idle":"2022-04-16T04:46:31.281391Z","shell.execute_reply.started":"2022-04-16T04:46:30.989182Z","shell.execute_reply":"2022-04-16T04:46:31.280540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = articles.corr()\ncorr.style.background_gradient (cmap = \"copper_r\")","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:31.282955Z","iopub.execute_input":"2022-04-16T04:46:31.283428Z","iopub.status.idle":"2022-04-16T04:46:31.357935Z","shell.execute_reply.started":"2022-04-16T04:46:31.283385Z","shell.execute_reply":"2022-04-16T04:46:31.357072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrmat = customers.corr()\nf, ax = plt.subplots(figsize=(10,10))\nsns.heatmap(corrmat, square=True, annot = True, annot_kws={'size':10})","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:31.359238Z","iopub.execute_input":"2022-04-16T04:46:31.359800Z","iopub.status.idle":"2022-04-16T04:46:31.650112Z","shell.execute_reply.started":"2022-04-16T04:46:31.359764Z","shell.execute_reply":"2022-04-16T04:46:31.649259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 12\nlistUniBins = customers['age'].unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:31.651555Z","iopub.execute_input":"2022-04-16T04:46:31.651974Z","iopub.status.idle":"2022-04-16T04:46:31.671880Z","shell.execute_reply.started":"2022-04-16T04:46:31.651899Z","shell.execute_reply":"2022-04-16T04:46:31.670983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Transactions_train**","metadata":{}},{"cell_type":"code","source":"transactions_train= pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:46:31.673578Z","iopub.execute_input":"2022-04-16T04:46:31.674120Z","iopub.status.idle":"2022-04-16T04:47:18.627355Z","shell.execute_reply.started":"2022-04-16T04:46:31.674071Z","shell.execute_reply":"2022-04-16T04:47:18.626299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:18.628671Z","iopub.execute_input":"2022-04-16T04:47:18.628910Z","iopub.status.idle":"2022-04-16T04:47:18.640604Z","shell.execute_reply.started":"2022-04-16T04:47:18.628883Z","shell.execute_reply":"2022-04-16T04:47:18.639650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:18.642089Z","iopub.execute_input":"2022-04-16T04:47:18.642449Z","iopub.status.idle":"2022-04-16T04:47:18.653755Z","shell.execute_reply.started":"2022-04-16T04:47:18.642415Z","shell.execute_reply":"2022-04-16T04:47:18.652759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:18.655670Z","iopub.execute_input":"2022-04-16T04:47:18.656309Z","iopub.status.idle":"2022-04-16T04:47:18.675878Z","shell.execute_reply.started":"2022-04-16T04:47:18.656261Z","shell.execute_reply":"2022-04-16T04:47:18.674958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:18.677366Z","iopub.execute_input":"2022-04-16T04:47:18.678025Z","iopub.status.idle":"2022-04-16T04:47:21.523829Z","shell.execute_reply.started":"2022-04-16T04:47:18.677973Z","shell.execute_reply":"2022-04-16T04:47:21.522720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = transactions_train.corr()\ncorr.style.background_gradient (cmap = \"flare_r\")","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:21.525285Z","iopub.execute_input":"2022-04-16T04:47:21.525876Z","iopub.status.idle":"2022-04-16T04:47:22.992089Z","shell.execute_reply.started":"2022-04-16T04:47:21.525821Z","shell.execute_reply":"2022-04-16T04:47:22.991113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrmat = transactions_train.corr()\nf, ax = plt.subplots(figsize=(10,10))\nsns.heatmap(corrmat, square=True, annot = True, annot_kws={'size':10})","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:22.993329Z","iopub.execute_input":"2022-04-16T04:47:22.993650Z","iopub.status.idle":"2022-04-16T04:47:24.645830Z","shell.execute_reply.started":"2022-04-16T04:47:22.993621Z","shell.execute_reply":"2022-04-16T04:47:24.645005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**sample_submission**","metadata":{}},{"cell_type":"code","source":"sample_submission= pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:47:24.647165Z","iopub.execute_input":"2022-04-16T04:47:24.647789Z","iopub.status.idle":"2022-04-16T04:47:29.399998Z","shell.execute_reply.started":"2022-04-16T04:47:24.647742Z","shell.execute_reply":"2022-04-16T04:47:29.399152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:48:35.525579Z","iopub.execute_input":"2022-04-16T04:48:35.526099Z","iopub.status.idle":"2022-04-16T04:48:35.540056Z","shell.execute_reply.started":"2022-04-16T04:48:35.526060Z","shell.execute_reply":"2022-04-16T04:48:35.539021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:49:02.788563Z","iopub.execute_input":"2022-04-16T04:49:02.789091Z","iopub.status.idle":"2022-04-16T04:49:02.794850Z","shell.execute_reply.started":"2022-04-16T04:49:02.789053Z","shell.execute_reply":"2022-04-16T04:49:02.794025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:49:22.135000Z","iopub.execute_input":"2022-04-16T04:49:22.135482Z","iopub.status.idle":"2022-04-16T04:49:22.278123Z","shell.execute_reply.started":"2022-04-16T04:49:22.135447Z","shell.execute_reply":"2022-04-16T04:49:22.276974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:50:17.332568Z","iopub.execute_input":"2022-04-16T04:50:17.333595Z","iopub.status.idle":"2022-04-16T04:50:17.466849Z","shell.execute_reply.started":"2022-04-16T04:50:17.333547Z","shell.execute_reply":"2022-04-16T04:50:17.465757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = sample_submission.corr()\ncorr.style.background_gradient (cmap = \"flare_r\")","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:52:38.863778Z","iopub.execute_input":"2022-04-16T04:52:38.864342Z","iopub.status.idle":"2022-04-16T04:52:38.876080Z","shell.execute_reply.started":"2022-04-16T04:52:38.864304Z","shell.execute_reply":"2022-04-16T04:52:38.875257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install rapids","metadata":{"execution":{"iopub.status.busy":"2022-04-16T04:59:44.127114Z","iopub.execute_input":"2022-04-16T04:59:44.127490Z","iopub.status.idle":"2022-04-16T04:59:57.893531Z","shell.execute_reply.started":"2022-04-16T04:59:44.127454Z","shell.execute_reply":"2022-04-16T04:59:57.892676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# === General ===\nimport sys, warnings, time, os, copy, gc, re, random, pickle \nwarnings.filterwarnings('ignore')\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n# pd.set_option('display.max_rows', 50)\n# pd.set_option('display.max_columns', None)\n# pd.set_option(\"display.max_colwidth\", 10000)\nimport seaborn as sns\nsns.set()\nfrom pandas.io.json import json_normalize\nfrom pprint import pprint\nfrom pathlib import Path\nfrom tqdm import tqdm\ntqdm.pandas()\nfrom collections import Counter\nfrom datetime import datetime, timedelta","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:01:38.451203Z","iopub.execute_input":"2022-04-16T05:01:38.451654Z","iopub.status.idle":"2022-04-16T05:01:38.460702Z","shell.execute_reply.started":"2022-04-16T05:01:38.451621Z","shell.execute_reply":"2022-04-16T05:01:38.459988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEBUG = False\nPATH_INPUT = r'../input/h-and-m-personalized-fashion-recommendations/'","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:02:02.661053Z","iopub.execute_input":"2022-04-16T05:02:02.661513Z","iopub.status.idle":"2022-04-16T05:02:02.665803Z","shell.execute_reply.started":"2022-04-16T05:02:02.661478Z","shell.execute_reply":"2022-04-16T05:02:02.664603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_df(df, head=3):\n    print(f'The shape of df is {df.shape}.\\n')\n    display(df.head(head))","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:03:19.594136Z","iopub.execute_input":"2022-04-16T05:03:19.594982Z","iopub.status.idle":"2022-04-16T05:03:19.600139Z","shell.execute_reply.started":"2022-04-16T05:03:19.594936Z","shell.execute_reply":"2022-04-16T05:03:19.599196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfArticles = pd.read_csv(PATH_INPUT + 'articles.csv', usecols=['article_id', \"product_group_name\", \"perceived_colour_master_name\"])\ndisplay_df(dfArticles, head=3)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:03:44.857754Z","iopub.execute_input":"2022-04-16T05:03:44.858366Z","iopub.status.idle":"2022-04-16T05:03:45.184330Z","shell.execute_reply.started":"2022-04-16T05:03:44.858323Z","shell.execute_reply":"2022-04-16T05:03:45.183382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfCustomers = pd.read_csv(PATH_INPUT + 'customers.csv', usecols=['customer_id', 'age'])\ndisplay_df(dfCustomers, head=3)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:04:48.805224Z","iopub.execute_input":"2022-04-16T05:04:48.805759Z","iopub.status.idle":"2022-04-16T05:04:51.082327Z","shell.execute_reply.started":"2022-04-16T05:04:48.805711Z","shell.execute_reply":"2022-04-16T05:04:51.081434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listBin = [-1, 19, 29, 39, 49, 59, 69, 119]\ndfCustomers['age_bins'] = pd.cut(dfCustomers['age'], listBin)\ndisplay_df(dfCustomers, head=3)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:10:19.021899Z","iopub.execute_input":"2022-04-16T05:10:19.022963Z","iopub.status.idle":"2022-04-16T05:10:19.095009Z","shell.execute_reply.started":"2022-04-16T05:10:19.022905Z","shell.execute_reply":"2022-04-16T05:10:19.093942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dfCustomers[dfCustomers['age_bins'].isnull()].shape[0]\nprint(f'{x} customer_id do not have age information.\\n')","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:10:45.358983Z","iopub.execute_input":"2022-04-16T05:10:45.359362Z","iopub.status.idle":"2022-04-16T05:10:45.380583Z","shell.execute_reply.started":"2022-04-16T05:10:45.359322Z","shell.execute_reply":"2022-04-16T05:10:45.379489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfTransactions = pd.read_csv(PATH_INPUT + 'transactions_train.csv',  \n                               usecols=['t_dat', 'customer_id', 'article_id'],\n                               dtype={'article_id': 'int32', 't_dat': 'string', 'customer_id': 'string'})\ndfTransactions['t_dat'] = pd.to_datetime(dfTransactions['t_dat'])\ndfTransactions.set_index('t_dat', inplace=True)\ndisplay_df(dfTransactions, head=3)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:12:39.735766Z","iopub.execute_input":"2022-04-16T05:12:39.736395Z","iopub.status.idle":"2022-04-16T05:13:17.228533Z","shell.execute_reply.started":"2022-04-16T05:12:39.736351Z","shell.execute_reply":"2022-04-16T05:13:17.227476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 12\nlistUniBins = dfCustomers['age_bins'].unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:13:23.324594Z","iopub.execute_input":"2022-04-16T05:13:23.324933Z","iopub.status.idle":"2022-04-16T05:13:23.342043Z","shell.execute_reply.started":"2022-04-16T05:13:23.324879Z","shell.execute_reply":"2022-04-16T05:13:23.340810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for uniBin in listUniBins:\n    df  = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',\n                            usecols= ['t_dat', 'customer_id', 'article_id'], \n                            dtype={'article_id': 'int32', 't_dat': 'string', 'customer_id': 'string'})\n    if str(uniBin) == 'nan':\n        dfCustomersTemp = dfCustomers[dfCustomers['age_bins'].isnull()]\n    else:\n        dfCustomersTemp = dfCustomers[dfCustomers['age_bins'] == uniBin]\n    \n    dfCustomersTemp = dfCustomersTemp.drop(['age_bins'], axis=1)\n    \n    \n    df = df.merge(dfCustomersTemp[['customer_id', 'age']], on='customer_id', how='inner')\n    print(f'The shape of scope transaction for {uniBin} is {df.shape}. \\n')\n          \n    df ['customer_id'] = df ['customer_id'].str[-16:].str.hex_to_int().astype('int64')\n    df['t_dat'] = pd.to_datetime(df['t_dat'])\n    last_ts = df['t_dat'].max()\n\n    tmp = df[['t_dat']].copy().to_pandas()\n    tmp['dow'] = tmp['t_dat'].dt.dayofweek\n    tmp['ldbw'] = tmp['t_dat'] - pd.TimedeltaIndex(tmp['dow'] - 1, unit='D')\n    tmp.loc[tmp['dow'] >=2 , 'ldbw'] = tmp.loc[tmp['dow'] >=2 , 'ldbw'] + pd.TimedeltaIndex(np.ones(len(tmp.loc[tmp['dow'] >=2])) * 7, unit='D')\n\n    df['ldbw'] = tmp['ldbw'].values\n    \n    weekly_sales = df.drop('customer_id', axis=1).groupby(['ldbw', 'article_id']).count().reset_index()\n    weekly_sales = weekly_sales.rename(columns={'t_dat': 'count'})\n    \n    df = df.merge(weekly_sales, on=['ldbw', 'article_id'], how = 'left')\n    \n    weekly_sales = weekly_sales.reset_index().set_index('article_id')\n\n    df = df.merge(\n        weekly_sales.loc[weekly_sales['ldbw']==last_ts, ['count']],\n        on='article_id', suffixes=(\"\", \"_targ\"))\n\n    df['count_targ'].fillna(0, inplace=True)\n    del weekly_sales\n    \n    df['quotient'] = df['count_targ'] / df['count']\n    \n    target_sales = df.drop('customer_id', axis=1).groupby('article_id')['quotient'].sum()\n    general_pred = target_sales.nlargest(N).index.to_pandas().tolist()\n    general_pred = ['0' + str(article_id) for article_id in general_pred]\n    general_pred_str =  ' '.join(general_pred)\n    del target_sales\n    \n    purchase_dict = {}\n\n    tmp = df.copy().to_pandas()\n    tmp['x'] = ((last_ts - tmp['t_dat']) / np.timedelta64(1, 'D')).astype(int)\n    tmp['dummy_1'] = 1 \n    tmp['x'] = tmp[[\"x\", \"dummy_1\"]].max(axis=1)\n\n    a, b, c, d = 2.5e4, 1.5e5, 2e-1, 1e3\n    tmp['y'] = a / np.sqrt(tmp['x']) + b * np.exp(-c*tmp['x']) - d\n\n    tmp['dummy_0'] = 0 \n    tmp['y'] = tmp[[\"y\", \"dummy_0\"]].max(axis=1)\n    tmp['value'] = tmp['quotient'] * tmp['y'] \n\n    tmp = tmp.groupby(['customer_id', 'article_id']).agg({'value': 'sum'})\n    tmp = tmp.reset_index()\n\n    tmp = tmp.loc[tmp['value'] > 0]\n    tmp['rank'] = tmp.groupby(\"customer_id\")[\"value\"].rank(\"dense\", ascending=False)\n    tmp = tmp.loc[tmp['rank'] <= 12]\n\n    purchase_df = tmp.sort_values(['customer_id', 'value'], ascending = False).reset_index(drop = True)\n    purchase_df['prediction'] = '0' + purchase_df['article_id'].astype(str) + ' '\n    purchase_df = purchase_df.groupby('customer_id').agg({'prediction': sum}).reset_index()\n    purchase_df['prediction'] = purchase_df['prediction'].str.strip()\n    purchase_df = cudf.DataFrame(purchase_df)\n    \n    sub  = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv',\n                            usecols= ['customer_id'], \n                            dtype={'customer_id': 'string'})\n    \n    numCustomers = sub.shape[0]\n    \n    sub = sub.merge(dfCustomersTemp[['customer_id', 'age']], on='customer_id', how='inner')\n\n    sub['customer_id2'] = sub['customer_id'].str[-16:].str.hex_to_int().astype('int64')\n\n    sub = sub.merge(purchase_df, left_on = 'customer_id2', right_on = 'customer_id', how = 'left',\n                   suffixes = ('', '_ignored'))\n\n    sub = sub.to_pandas()\n    sub['prediction'] = sub['prediction'].fillna(general_pred_str)\n    sub['prediction'] = sub['prediction'] + ' ' +  general_pred_str\n    sub['prediction'] = sub['prediction'].str.strip()\n    sub['prediction'] = sub['prediction'].str[:131]\n    sub = sub[['customer_id', 'prediction']]\n    sub.to_csv(f'submission_' + str(uniBin) + '.csv',index=False)\n    print(f'Saved prediction for {uniBin}. The shape is {sub.shape}. \\n')\n    print('-'*50)\nprint('Finished.\\n')\nprint('='*50)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T05:33:38.195746Z","iopub.execute_input":"2022-04-16T05:33:38.196511Z","iopub.status.idle":"2022-04-16T05:34:29.996533Z","shell.execute_reply.started":"2022-04-16T05:33:38.196458Z","shell.execute_reply":"2022-04-16T05:34:29.995478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}