{"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":"I just apply the [solution](https://www.kaggle.com/code/hirotakanogami/h-m-eda-customer-clustering-by-kmeans) to this [notebook](https://www.kaggle.com/code/baekseungyun/lb-0-0235-ensemble-gives-you-bronze-medal). Thanks.","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-04T14:19:40.952939Z","iopub.execute_input":"2022-04-04T14:19:40.953572Z","iopub.status.idle":"2022-04-04T14:19:40.977308Z","shell.execute_reply.started":"2022-04-04T14:19:40.953474Z","shell.execute_reply":"2022-04-04T14:19:40.976619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To ensemble I used submissions from 7 public notebooks:\n* LB: 0.0217 - https://www.kaggle.com/tarique7/hnm-exponential-decay-with-alternate-items/notebook\n* LB: 0.0220 - https://www.kaggle.com/code/hengzheng/time-is-our-best-friend-v2/notebook\n* LB: 0.0221 - https://www.kaggle.com/astrung/lstm-sequential-modelwith-item-features-tutorial\n* LB: 0.0224 - https://www.kaggle.com/code/hirotakanogami/h-m-eda-customer-clustering-by-kmeans\n* LB: 0.0225 - https://www.kaggle.com/lunapandachan/h-m-trending-products-weekly-add-test/notebook\n* LB: 0.0227 - https://www.kaggle.com/code/hechtjp/h-m-eda-rule-base-by-customer-age\n* LB: 0.0231 - https://www.kaggle.com/code/ebn7amdi/trending/notebook?scriptVersionId=90980162","metadata":{}},{"cell_type":"code","source":"sub2 = pd.read_csv('../input/handmbestperforming/hnm-exponential-decay-with-alternate-items.csv').sort_values('customer_id').reset_index(drop=True)\nsub5 = pd.read_csv('../input/handmbestperforming/time-is-our-best-friend-v2.csv').sort_values('customer_id').reset_index(drop=True)\nsub3 = pd.read_csv('../input/handmbestperforming/lstm-sequential-modelwith-item-features-tutorial.csv').sort_values('customer_id').reset_index(drop=True)\nsub4 = pd.read_csv('../input/hm-00224-solution/submission.csv').sort_values('customer_id').reset_index(drop=True)\n\nsub0 = pd.read_csv('../input/hm-00231-solution/submission.csv').sort_values('customer_id').reset_index(drop=True)\nsub1 = pd.read_csv('../input/handmbestperforming/h-m-trending-products-weekly-add-test.csv').sort_values('customer_id').reset_index(drop=True)\nsub6 = pd.read_csv('../input/handmbestperforming/rule-based-by-customer-age.csv').sort_values('customer_id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T14:22:40.170011Z","iopub.execute_input":"2022-04-04T14:22:40.170357Z","iopub.status.idle":"2022-04-04T14:23:25.677289Z","shell.execute_reply.started":"2022-04-04T14:22:40.170324Z","shell.execute_reply":"2022-04-04T14:23:25.675808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0.columns = ['customer_id', 'prediction0']\nsub0['prediction1'] = sub1['prediction']\nsub0['prediction2'] = sub2['prediction']\nsub0['prediction3'] = sub3['prediction']\nsub0['prediction4'] = sub4['prediction']\nsub0['prediction5'] = sub5['prediction']\nsub0['prediction6'] = sub6['prediction']\n\ndel sub1, sub2, sub3, sub4, sub5, sub6\ngc.collect()\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-04T14:23:25.680603Z","iopub.execute_input":"2022-04-04T14:23:25.682416Z","iopub.status.idle":"2022-04-04T14:23:26.520662Z","shell.execute_reply.started":"2022-04-04T14:23:25.682359Z","shell.execute_reply":"2022-04-04T14:23:26.519815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cust_blend(dt, W = [1,1,1,1,1]):\n    #Global ensemble weights\n    #W = [1.15,0.95,0.85]\n\n    #Create a list of all model predictions\n    REC = []\n\n    # Second Try\n    REC.append(dt['prediction0'].split())\n    REC.append(dt['prediction1'].split())\n    REC.append(dt['prediction2'].split())\n    REC.append(dt['prediction3'].split())\n    REC.append(dt['prediction4'].split())\n    REC.append(dt['prediction5'].split())\n    REC.append(dt['prediction6'].split())\n\n    #Create a dictionary of items recommended.\n    #Assign a weight according the order of appearance and multiply by global weights\n    res = {}\n    for M in range(len(REC)):\n        for n, v in enumerate(REC[M]):\n            if v in res:\n                res[v] += (W[M]/(n+1))\n            else:\n                res[v] = (W[M]/(n+1))\n\n    # Sort dictionary by item weights\n    res = list(dict(sorted(res.items(), key=lambda item: -item[1])).keys())\n\n    # Return the top 12 items only\n    return ' '.join(res[:12])\n\n# sub0['prediction'] = sub0.apply(cust_blend, W = [0.95,1.00,0.55,0.75,0.85,0.65,1.05], axis=1)\n\nsub0['prediction'] = sub0.apply(cust_blend, W = [1.05,1.00,0.95,0.85,0.75,0.65,0.55], axis=1)\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-04T14:23:26.522414Z","iopub.execute_input":"2022-04-04T14:23:26.522898Z","iopub.status.idle":"2022-04-04T14:25:36.279282Z","shell.execute_reply.started":"2022-04-04T14:23:26.522866Z","shell.execute_reply":"2022-04-04T14:25:36.278245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make a submission","metadata":{}},{"cell_type":"code","source":"del sub0['prediction0']\ndel sub0['prediction1']\ndel sub0['prediction2']\ndel sub0['prediction3']\ndel sub0['prediction4']\ndel sub0['prediction5']\ndel sub0['prediction6']\ngc.collect()\n\n\nsub0.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T14:25:36.281686Z","iopub.execute_input":"2022-04-04T14:25:36.281967Z","iopub.status.idle":"2022-04-04T14:25:49.391202Z","shell.execute_reply.started":"2022-04-04T14:25:36.281935Z","shell.execute_reply":"2022-04-04T14:25:49.390192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}