{"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":"2022-03-25T17:10:50.155966Z","iopub.execute_input":"2022-03-25T17:10:50.156589Z","iopub.status.idle":"2022-03-25T17:10:50.160893Z","shell.execute_reply.started":"2022-03-25T17:10:50.156544Z","shell.execute_reply":"2022-03-25T17:10:50.160105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/hm-public-submissions","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:10:50.166841Z","iopub.execute_input":"2022-03-25T17:10:50.167118Z","iopub.status.idle":"2022-03-25T17:10:50.980664Z","shell.execute_reply.started":"2022-03-25T17:10:50.167081Z","shell.execute_reply":"2022-03-25T17:10:50.979648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Adding LSTM models to the ensemble**","metadata":{}},{"cell_type":"markdown","source":"# Note\n* I am not sure whether I should get any credit for this notebook as this is based(mostly) on the work of others.\n* I added two more submissions to the ensemble and tried a few options with them.","metadata":{}},{"cell_type":"markdown","source":"# New Notebooks\n* LB: 0.0210 - https://www.kaggle.com/astrung/recbole-lstm-sequential-for-recomendation-tutorial\n* LB: 0.0221 - https://www.kaggle.com/astrung/lstm-sequential-modelwith-item-features-tutorial\n* both notebooks by @astrung","metadata":{}},{"cell_type":"markdown","source":"# Predictions in this competition are a list of 12 itens ordered by most relevant first.\n# In this notebook I will show how to ensemble lists of different models\n# To ensemble I used submissions from 3 public notebooks:\n- LB: 0.0225 - https://www.kaggle.com/lichtlab/0-0226-byfone-chris-combination-approach/data?scriptVersionId=89289696\n- LB: 0.0225 - https://www.kaggle.com/lunapandachan/h-m-trending-products-weekly-add-test/notebook\n- LB: 0.0217 - https://www.kaggle.com/tarique7/hnm-exponential-decay-with-alternate-items/notebook","metadata":{}},{"cell_type":"code","source":"sub0 = pd.read_csv('../input/hm-public-submissions/0-0226-byfone-chris-combination-approach.csv').sort_values('customer_id').reset_index(drop=True)\nsub1 = pd.read_csv('../input/hm-public-submissions/h-m-trending-products-weekly-add-test.csv').sort_values('customer_id').reset_index(drop=True)\nsub2 = pd.read_csv('../input/hm-public-submissions/hnm-exponential-decay-with-alternate-items.csv').sort_values('customer_id').reset_index(drop=True)\n\nsub0.shape, sub1.shape, sub2.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:10:50.983108Z","iopub.execute_input":"2022-03-25T17:10:50.986302Z","iopub.status.idle":"2022-03-25T17:11:10.373521Z","shell.execute_reply.started":"2022-03-25T17:10:50.986250Z","shell.execute_reply":"2022-03-25T17:11:10.372664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub3 = pd.read_csv('../input/submission-recbole-lstm/submission.csv').sort_values('customer_id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:10.374579Z","iopub.execute_input":"2022-03-25T17:11:10.374792Z","iopub.status.idle":"2022-03-25T17:11:16.047632Z","shell.execute_reply.started":"2022-03-25T17:11:10.374766Z","shell.execute_reply":"2022-03-25T17:11:16.046617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub4 = pd.read_csv('../input/submission-lstm-sequential/submission (1).csv').sort_values('customer_id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:16.052134Z","iopub.execute_input":"2022-03-25T17:11:16.052404Z","iopub.status.idle":"2022-03-25T17:11:21.638012Z","shell.execute_reply.started":"2022-03-25T17:11:16.052371Z","shell.execute_reply":"2022-03-25T17:11:21.637361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common between models\n\nprint((sub0['prediction']==sub1['prediction']).mean())\nprint((sub0['prediction']==sub2['prediction']).mean())\nprint((sub1['prediction']==sub2['prediction']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:21.639053Z","iopub.execute_input":"2022-03-25T17:11:21.639455Z","iopub.status.idle":"2022-03-25T17:11:22.679352Z","shell.execute_reply.started":"2022-03-25T17:11:21.639403Z","shell.execute_reply":"2022-03-25T17:11:22.678310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common between old models and new \n\nprint((sub3['prediction']==sub0['prediction']).mean())\nprint((sub3['prediction']==sub1['prediction']).mean())\nprint((sub3['prediction']==sub2['prediction']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:22.680732Z","iopub.execute_input":"2022-03-25T17:11:22.681407Z","iopub.status.idle":"2022-03-25T17:11:23.693426Z","shell.execute_reply.started":"2022-03-25T17:11:22.681360Z","shell.execute_reply":"2022-03-25T17:11:23.692375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common between old models and new\n\nprint((sub4['prediction']==sub0['prediction']).mean())\nprint((sub4['prediction']==sub1['prediction']).mean())\nprint((sub4['prediction']==sub2['prediction']).mean())\nprint((sub4['prediction']==sub3['prediction']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:23.694741Z","iopub.execute_input":"2022-03-25T17:11:23.695126Z","iopub.status.idle":"2022-03-25T17:11:25.042094Z","shell.execute_reply.started":"2022-03-25T17:11:23.695082Z","shell.execute_reply":"2022-03-25T17:11:25.041282Z"},"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']\ndel sub1, sub2, sub3, sub4\ngc.collect()\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:25.043166Z","iopub.execute_input":"2022-03-25T17:11:25.043384Z","iopub.status.idle":"2022-03-25T17:11:25.502965Z","shell.execute_reply.started":"2022-03-25T17:11:25.043357Z","shell.execute_reply":"2022-03-25T17:11:25.502097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cust_blend(dt, W = [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    \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 itens only\n    return ' '.join(res[:12])","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:25.504323Z","iopub.execute_input":"2022-03-25T17:11:25.504646Z","iopub.status.idle":"2022-03-25T17:11:25.514149Z","shell.execute_reply.started":"2022-03-25T17:11:25.504607Z","shell.execute_reply":"2022-03-25T17:11:25.513128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0['prediction'] = sub0.apply(cust_blend, W = [1.05,1.00,0.95,0.85], axis=1)\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:11:25.516725Z","iopub.execute_input":"2022-03-25T17:11:25.517041Z","iopub.status.idle":"2022-03-25T17:12:48.793077Z","shell.execute_reply.started":"2022-03-25T17:11:25.517009Z","shell.execute_reply":"2022-03-25T17:12:48.792202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common with ensemble\n\nprint((sub0['prediction']==sub0['prediction0']).mean())\nprint((sub0['prediction']==sub0['prediction1']).mean())\nprint((sub0['prediction']==sub0['prediction2']).mean())\nprint((sub0['prediction']==sub0['prediction3']).mean())\nprint((sub0['prediction']==sub0['prediction4']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:12:48.794541Z","iopub.execute_input":"2022-03-25T17:12:48.795127Z","iopub.status.idle":"2022-03-25T17:12:50.467716Z","shell.execute_reply.started":"2022-03-25T17:12:48.795090Z","shell.execute_reply":"2022-03-25T17:12:50.466618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The results of comparison seem interesting**","metadata":{}},{"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']\ngc.collect()\nsub0.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:12:50.469510Z","iopub.execute_input":"2022-03-25T17:12:50.469829Z","iopub.status.idle":"2022-03-25T17:13:03.381404Z","shell.execute_reply.started":"2022-03-25T17:12:50.469785Z","shell.execute_reply":"2022-03-25T17:13:03.380702Z"},"trusted":true},"execution_count":null,"outputs":[]}]}