{"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","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2022-04-23T14:07:52.113217Z","iopub.status.busy":"2022-04-23T14:07:52.111728Z","iopub.status.idle":"2022-04-23T14:07:52.120275Z","shell.execute_reply":"2022-04-23T14:07:52.120671Z","shell.execute_reply.started":"2022-04-18T10:22:56.125032Z"},"papermill":{"duration":0.020406,"end_time":"2022-04-23T14:07:52.120895","exception":false,"start_time":"2022-04-23T14:07:52.100489","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To ensemble I used submissions from 8 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.0223 - https://www.kaggle.com/code/astrung/lstm-model-with-item-infor-fix-missing-last-item/notebook\n* LB: 0.0225 - https://www.kaggle.com/lunapandachan/h-m-trending-products-weekly-add-test/notebook\n* LB: 0.0231 - https://www.kaggle.com/code/ebn7amdi/trending/notebook?scriptVersionId=90980162\n* LB: 0.0227 - https://www.kaggle.com/code/hechtjp/h-m-eda-rule-base-by-customer-age\n","metadata":{"papermill":{"duration":0.007358,"end_time":"2022-04-23T14:07:52.13554","exception":false,"start_time":"2022-04-23T14:07:52.128182","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sub0 = 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)\nsub2 = pd.read_csv('../input/handmbestperforming/hnm-exponential-decay-with-alternate-items.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)\nsub5 = pd.read_csv('../input/handmbestperforming/time-is-our-best-friend-v2.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)\nsub7 = pd.read_csv('../input/h-m-faster-trending-products-weekly/submission.csv').sort_values('customer_id').reset_index(drop=True)\n# sub8 = pd.read_csv('../input/h-m-framework-for-partitioned-validation/submission.csv').sort_values('customer_id').reset_index(drop=True)  ","metadata":{"execution":{"iopub.execute_input":"2022-04-23T14:07:52.157872Z","iopub.status.busy":"2022-04-23T14:07:52.157307Z","iopub.status.idle":"2022-04-23T14:08:52.844274Z","shell.execute_reply":"2022-04-23T14:08:52.843318Z","shell.execute_reply.started":"2022-04-18T10:29:10.321248Z"},"papermill":{"duration":60.70143,"end_time":"2022-04-23T14:08:52.844425","exception":false,"start_time":"2022-04-23T14:07:52.142995","status":"completed"},"tags":[]},"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']\nsub0['prediction7'] = sub7['prediction'].astype(str)\n\ndel sub1, sub2, sub3, sub4, sub5, sub6, sub7\ngc.collect()\nsub0.head()","metadata":{"execution":{"iopub.execute_input":"2022-04-23T14:08:52.870403Z","iopub.status.busy":"2022-04-23T14:08:52.869501Z","iopub.status.idle":"2022-04-23T14:08:53.76499Z","shell.execute_reply":"2022-04-23T14:08:53.765394Z","shell.execute_reply.started":"2022-04-18T10:30:00.102483Z"},"papermill":{"duration":0.912698,"end_time":"2022-04-23T14:08:53.765548","exception":false,"start_time":"2022-04-23T14:08:52.85285","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cust_blend(dt, W = [1,1,1,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    REC.append(dt['prediction7'].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 = [1.05, 0.75, 1.00, 0.95, 0.65, 0.90, 0.80, 0.55], axis=1)\nsub0['prediction'] = sub0.apply(cust_blend, W = [1.05, 0.78, 0.86, 0.85, 0.68, 0.64, 0.70, 0.24], axis=1)\n# sub0['prediction'] = sub0.apply(cust_blend, W = [1.05, 0.78, 0.86, 0.85, 0.68, 0.64, 0.70, 0.24, 1.01], axis=1)\n\nsub0.head()","metadata":{"execution":{"iopub.execute_input":"2022-04-23T14:08:53.785475Z","iopub.status.busy":"2022-04-23T14:08:53.784584Z","iopub.status.idle":"2022-04-23T14:11:19.816142Z","shell.execute_reply":"2022-04-23T14:11:19.816574Z","shell.execute_reply.started":"2022-04-18T10:30:01.021476Z"},"papermill":{"duration":146.042668,"end_time":"2022-04-23T14:11:19.816751","exception":false,"start_time":"2022-04-23T14:08:53.774083","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"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']\ndel sub0['prediction7']\ngc.collect()","metadata":{"execution":{"iopub.execute_input":"2022-04-23T14:11:19.860177Z","iopub.status.busy":"2022-04-23T14:11:19.859312Z","iopub.status.idle":"2022-04-23T14:11:31.424863Z","shell.execute_reply":"2022-04-23T14:11:31.425311Z","shell.execute_reply.started":"2022-04-18T10:26:44.145695Z"},"papermill":{"duration":11.582373,"end_time":"2022-04-23T14:11:31.425482","exception":false,"start_time":"2022-04-23T14:11:19.843109","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make a submission","metadata":{"papermill":{"duration":0.008635,"end_time":"2022-04-23T14:11:19.834502","exception":false,"start_time":"2022-04-23T14:11:19.825867","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sub1 = pd.read_csv('../input/h-m-framework-for-partitioned-validation/submission.csv').sort_values('customer_id').reset_index(drop=True)\nsub1['prediction'] = sub1['prediction'].astype(str)\n\nsub0.columns = ['customer_id', 'prediction0']\nsub0['prediction1'] = sub1['prediction']\n\ndel sub1\ngc.collect()","metadata":{},"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\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])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0['prediction'] = sub0.apply(cust_blend, W = [1.20, 0.85], axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del sub0['prediction0']\ndel sub0['prediction1']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0.to_csv('submission.csv', index=False)","metadata":{"papermill":{"duration":0.008509,"end_time":"2022-04-23T14:11:31.442856","exception":false,"start_time":"2022-04-23T14:11:31.434347","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}