{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0 = pd.read_csv('../input/fashions/submission231.csv').sort_values('customer_id').reset_index(drop=True)\nsub1 = pd.read_csv('../input/fashions/sub-age.csv').sort_values('customer_id').reset_index(drop=True) #227\nsub2 = pd.read_csv('../input/fashions/sub-weekly.csv').sort_values('customer_id').reset_index(drop=True) #225\nsub3 = pd.read_csv('../input/fashions/submission225.csv').sort_values('customer_id').reset_index(drop=True)\n\n#W = [0.96, 0.81, 0.75, 0.75] ","metadata":{},"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'].astype(str)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del sub1, sub2, sub3, \ngc.collect()\nsub0.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cust_blend(dt, W = [1,1,1,1]):\n    REC = []\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    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    res = list(dict(sorted(res.items(), key=lambda item: -item[1])).keys())\n    return ' '.join(res[:12])\n\nsub0['prediction'] = sub0.apply(cust_blend, W = [0.96, 0.81, 0.75, 0.75] , axis=1)\nsub0.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del sub0['prediction0']\ndel sub0['prediction1']\ndel sub0['prediction2']\ndel sub0['prediction3']\ngc.collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub00 = pd.read_csv('../input/fashions/submission224.csv').sort_values('customer_id').reset_index(drop=True)\nsub1 = pd.read_csv('../input/fashions/sub-lstm.csv').sort_values('customer_id').reset_index(drop=True) #221\nsub2 = pd.read_csv('../input/fashions/sub-time.csv').sort_values('customer_id').reset_index(drop=True) #220\nsub3 = pd.read_csv('../input/fashions/sub-decay.csv').sort_values('customer_id').reset_index(drop=True) #217\n\n#W = [0.72, 0.63, 0.60, 0.51] ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub00.columns = ['customer_id', 'prediction0']\nsub00['prediction1'] = sub1['prediction']\nsub00['prediction2'] = sub2['prediction']\nsub00['prediction3'] = sub3['prediction'].astype(str)\n\ndel sub1, sub2, sub3, \ngc.collect()\nsub00.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub00['prediction'] = sub00.apply(cust_blend, W = [0.72, 0.63, 0.60, 0.51] , axis=1)\nsub00.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del sub00['prediction0']\ndel sub00['prediction1']\ndel sub00['prediction2']\ndel sub00['prediction3']\ngc.collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub1 = pd.read_csv('../input/fashions/sub-parq.csv').sort_values('customer_id').reset_index(drop=True)\nsub1['prediction'] = sub1['prediction'].astype(str)\nsub0['prediction'] = sub0['prediction'].astype(str)\nsub00['prediction'] = sub00['prediction'].astype(str)\nsub1.columns = ['customer_id', 'prediction0']\nsub1['prediction1'] = sub0['prediction']\nsub1['prediction2'] = sub00['prediction']\ndel sub0, sub00\ngc.collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cust_blend(dt, W = [1,1,1]):\n    REC = []\n    REC.append(dt['prediction0'].split())\n    REC.append(dt['prediction1'].split())\n    REC.append(dt['prediction2'].split())\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    res = list(dict(sorted(res.items(), key=lambda item: -item[1])).keys())\n    return ' '.join(res[:12])\n\nsub1['prediction'] = sub1.apply(cust_blend, W = [0.75, 1, 0.25], axis=1)\n\ndel sub1['prediction0']\ndel sub1['prediction1']\ndel sub1['prediction2']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub1.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}