{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-01T04:54:39.789919Z","iopub.execute_input":"2023-05-01T04:54:39.790566Z","iopub.status.idle":"2023-05-01T04:54:39.850434Z","shell.execute_reply.started":"2023-05-01T04:54:39.790505Z","shell.execute_reply":"2023-05-01T04:54:39.849078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"LB: 0.0217 - https://www.kaggle.com/tarique7/hnm-exponential-decay-with-alternate-items/notebook\nLB: 0.0220 - https://www.kaggle.com/code/hengzheng/time-is-our-best-friend-v2/notebook\nLB: 0.0221 - https://www.kaggle.com/astrung/lstm-sequential-modelwith-item-features-tutorial\nLB: 0.0223 - https://www.kaggle.com/code/astrung/lstm-model-with-item-infor-fix-missing-last-item/notebook\nLB: 0.0225 - https://www.kaggle.com/lunapandachan/h-m-trending-products-weekly-add-test/notebook\nLB: 0.0231 - https://www.kaggle.com/code/ebn7amdi/trending/notebook?scriptVersionId=90980162\nLB: 0.0227 - https://www.kaggle.com/code/hechtjp/h-m-eda-rule-base-by-customer-age","metadata":{}},{"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.status.busy":"2023-05-01T05:05:46.346623Z","iopub.execute_input":"2023-05-01T05:05:46.347567Z","iopub.status.idle":"2023-05-01T05:06:17.343404Z","shell.execute_reply.started":"2023-05-01T05:05:46.347516Z","shell.execute_reply":"2023-05-01T05:06:17.342281Z"},"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']\nsub0['prediction7'] = sub7['prediction']\n\ndel sub1, sub2, sub3, sub4, sub5, sub6, sub7\ngc.collect()\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T05:06:17.346982Z","iopub.execute_input":"2023-05-01T05:06:17.347770Z","iopub.status.idle":"2023-05-01T05:06:17.979744Z","shell.execute_reply.started":"2023-05-01T05:06:17.347731Z","shell.execute_reply":"2023-05-01T05:06:17.978818Z"},"trusted":true},"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#     return pd.DataFrame.from_dict(res)\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,1.00,0.95,0.85,0.75,0.65,0.55,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)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T05:06:17.981354Z","iopub.execute_input":"2023-05-01T05:06:17.981740Z","iopub.status.idle":"2023-05-01T05:08:07.644554Z","shell.execute_reply.started":"2023-05-01T05:06:17.981702Z","shell.execute_reply":"2023-05-01T05:08:07.643463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-01T05:08:07.646899Z","iopub.execute_input":"2023-05-01T05:08:07.647193Z","iopub.status.idle":"2023-05-01T05:08:07.664143Z","shell.execute_reply.started":"2023-05-01T05:08:07.647166Z","shell.execute_reply":"2023-05-01T05:08:07.663134Z"},"trusted":true},"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.status.busy":"2023-05-01T05:08:07.665786Z","iopub.execute_input":"2023-05-01T05:08:07.666556Z","iopub.status.idle":"2023-05-01T05:08:07.779626Z","shell.execute_reply.started":"2023-05-01T05:08:07.666505Z","shell.execute_reply":"2023-05-01T05:08:07.778281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"iopub.status.busy":"2023-05-01T05:08:51.339787Z","iopub.execute_input":"2023-05-01T05:08:51.340168Z","iopub.status.idle":"2023-05-01T05:08:58.114173Z","shell.execute_reply.started":"2023-05-01T05:08:51.340134Z","shell.execute_reply":"2023-05-01T05:08:58.113130Z"},"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\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    # 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":{"iopub.status.busy":"2023-05-01T05:09:02.483010Z","iopub.execute_input":"2023-05-01T05:09:02.483393Z","iopub.status.idle":"2023-05-01T05:09:02.492381Z","shell.execute_reply.started":"2023-05-01T05:09:02.483359Z","shell.execute_reply":"2023-05-01T05:09:02.490944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0['prediction'] = sub0.apply(cust_blend, W = [1.30, 0.85], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-01T05:09:07.961900Z","iopub.execute_input":"2023-05-01T05:09:07.962835Z","iopub.status.idle":"2023-05-01T05:09:48.082536Z","shell.execute_reply.started":"2023-05-01T05:09:07.962780Z","shell.execute_reply":"2023-05-01T05:09:48.081264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del sub0['prediction0']\ndel sub0['prediction1']\nsub0.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T05:09:48.084335Z","iopub.execute_input":"2023-05-01T05:09:48.085029Z","iopub.status.idle":"2023-05-01T05:09:53.260738Z","shell.execute_reply.started":"2023-05-01T05:09:48.084991Z","shell.execute_reply":"2023-05-01T05:09:53.259568Z"},"trusted":true},"execution_count":null,"outputs":[]}]}