{"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":"# Recommend Customer's most expensive purchases in the hope that she will buy it again\nWell the the Header is self explanatory. One nuance is that if the customer has made < 12 purchases, he will be recommended from another list of overall most expensive items\nFor example, \n* if a customer X has purchased 20 items, most expensive 12 of her purchases will be recommended again.\n* if a customer Y has purchased 5 items, those 5 AND 7 of the overall most expensive 7 across all customers will be recommended.\n* if a customer Z has purchased 0 items, 12 of the overall most expensive 7 across all customers will be recommended.\n\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2022-04-27T10:58:29.220002Z","iopub.execute_input":"2022-04-27T10:58:29.220709Z","iopub.status.idle":"2022-04-27T10:58:29.229974Z","shell.execute_reply.started":"2022-04-27T10:58:29.220613Z","shell.execute_reply":"2022-04-27T10:58:29.229282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load Transactions**\n","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntrain['article_id'] = train.article_id.astype('int32')\ntrain.t_dat = pd.to_datetime(train.t_dat)\ntrain = train[['t_dat','customer_id','article_id','price']]\ntrain.to_parquet('train.pqt',index=False)\nprint( train.shape )\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T10:58:29.231443Z","iopub.execute_input":"2022-04-27T10:58:29.232128Z","iopub.status.idle":"2022-04-27T10:59:37.040677Z","shell.execute_reply.started":"2022-04-27T10:58:29.232062Z","shell.execute_reply":"2022-04-27T10:59:37.039825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get the most expensive purchase of each customer**","metadata":{}},{"cell_type":"code","source":"trans_costliest_first = train.sort_values(['customer_id','price'],ascending=False)\ntrans_costliest_first['article_id'] = '0' + trans_costliest_first['article_id'].astype(str)\ntrans_costliest_first['article_id'] = trans_costliest_first['article_id'].astype(str)\ntrans_costliest_first = trans_costliest_first.drop_duplicates(subset=['customer_id','article_id'],keep='last')\ntrans_costliest_first.to_parquet('trans_costliest_first.pqt')\ntrans_costliest_first.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T10:59:37.042887Z","iopub.execute_input":"2022-04-27T10:59:37.043561Z","iopub.status.idle":"2022-04-27T11:01:12.10038Z","shell.execute_reply.started":"2022-04-27T10:59:37.043516Z","shell.execute_reply":"2022-04-27T11:01:12.099599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_top_buys_only = trans_costliest_first[['customer_id','article_id']]","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:01:12.101932Z","iopub.execute_input":"2022-04-27T11:01:12.106325Z","iopub.status.idle":"2022-04-27T11:01:13.037738Z","shell.execute_reply.started":"2022-04-27T11:01:12.106278Z","shell.execute_reply":"2022-04-27T11:01:13.036946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get Overall most expensive**","metadata":{}},{"cell_type":"code","source":"top12 = trans_costliest_first.sort_values('price',ascending=False).drop_duplicates().head(12)\ntop12 = top12['article_id']\ntop12 = ' '.join(list(top12))\ntop12 = [top12]\ntop12","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:01:13.042787Z","iopub.execute_input":"2022-04-27T11:01:13.043406Z","iopub.status.idle":"2022-04-27T11:01:57.924741Z","shell.execute_reply.started":"2022-04-27T11:01:13.043373Z","shell.execute_reply":"2022-04-27T11:01:57.924037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict = {}\n\nfor i,x in enumerate(zip(trans_top_buys_only['customer_id'], trans_top_buys_only['article_id'])):\n    cust_id, art_id = x\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    purchase_dict[cust_id][art_id] += 1\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:01:57.926404Z","iopub.execute_input":"2022-04-27T11:01:57.927113Z","iopub.status.idle":"2022-04-27T11:02:30.133108Z","shell.execute_reply.started":"2022-04-27T11:01:57.927074Z","shell.execute_reply":"2022-04-27T11:02:30.132329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\nsubmission =  submission.drop('prediction',axis=1)\n#submission","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:02:30.134363Z","iopub.execute_input":"2022-04-27T11:02:30.136552Z","iopub.status.idle":"2022-04-27T11:02:32.572818Z","shell.execute_reply.started":"2022-04-27T11:02:30.13651Z","shell.execute_reply":"2022-04-27T11:02:32.572068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simplest = submission[['customer_id']]\n#simplest= simplest.to_pandas()\nprediction_list=[]\nfor i, cust_id in enumerate(submission['customer_id'].values.reshape((-1,))):\n    if cust_id in purchase_dict:\n        l = sorted((purchase_dict[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+top12[:(12-len(l))])\n    else:\n        s = top12\n    prediction_list.append(s)\n    \nsimplest['prediction'] = prediction_list\n#print(simplest.shape)\n#simplest.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:02:32.574789Z","iopub.execute_input":"2022-04-27T11:02:32.575674Z","iopub.status.idle":"2022-04-27T11:02:41.831606Z","shell.execute_reply.started":"2022-04-27T11:02:32.575633Z","shell.execute_reply":"2022-04-27T11:02:41.830871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simplest","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:02:41.832788Z","iopub.execute_input":"2022-04-27T11:02:41.833058Z","iopub.status.idle":"2022-04-27T11:02:41.846034Z","shell.execute_reply.started":"2022-04-27T11:02:41.833022Z","shell.execute_reply":"2022-04-27T11:02:41.845201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simplest.to_csv(f'submission.csv',index=False)\nsimplest.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T11:02:41.847196Z","iopub.execute_input":"2022-04-27T11:02:41.847975Z","iopub.status.idle":"2022-04-27T11:02:54.319821Z","shell.execute_reply.started":"2022-04-27T11:02:41.847932Z","shell.execute_reply":"2022-04-27T11:02:54.319066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Credits:\n* Some ideas and code is from Chris Deotte's excellent https://www.kaggle.com/code/cdeotte/recommend-items-purchased-together-0-021\n* Some ideas and code is also from Abhilash Awasthis' https://www.kaggle.com/code/abhilashawasthi/not-so-fancy-but-fast-benchmark ","metadata":{}}]}