{"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":"<div>\n    <h1 align=\"center\"> Ensembling & Comparative Method - Part(2)</h1></h1>\n    <h2 align=\"center\">Identify the position of a smartphone in a shopping mall</h2>\n    <h3 align=\"center\">By: Somayyeh Gholami & Mehran Kazeminia</h3>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"# Description:","metadata":{}},{"cell_type":"markdown","source":"### - In Notebook (No. 1) we optimized the results of ten public notebooks separately. In this notebook (No. 2) we use \"Ensembling\" and \"Comparative Method\" to merge the previous results to get better scores. Finally, we publish notebooks (No. 3) called \"Snap to Grid\".\n\n### - Thanks to everyone who shared their notebooks, the addresses of some of the used notebooks are as follows:\n\nhttps://www.kaggle.com/robikscube/indoor-navigation-snap-to-grid-post-processing\n\nhttps://www.kaggle.com/therocket290/lstm-unified-wi-fi-training-x-and-y-with-floor\n\nhttps://www.kaggle.com/kokitanisaka/lstm-by-keras-with-unified-wi-fi-feats\n\nhttps://www.kaggle.com/oxzplvifi/indoor-gbm-postprocessing-xy-prediction\n\nhttps://www.kaggle.com/nigelhenry/simple-99-accurate-floor-model\n\nhttps://www.kaggle.com/hiro5299834/wifi-features-with-lightgbm-kfold\n\nhttps://www.kaggle.com/ebinan92/time-series-rnn-xy-prediction\n\n\n### -  For \"Generated1\", we used two notebooks with scores of 5.471 and 5.995. And for the \"Comparative Method\" we got help from the remaining seven notebooks. If you read the code carefully, you will see that we have only allowed some of the results of these seven notebooks to affect the overall result. That is, they can only be effective if their X and Y values are between the X and Y of the two main notebooks (Rectangle for decision). This is a very important point.\n\n### - To get \"Generated2\" we did exactly the same as \"Generated1\". But we used notebooks with scores of 5.694 and 5.995. That is another rectangle for the decision that is shared at one vertex with the previous rectangle. The rest of the steps of this notebook are to use \"Ensembling\".\n\n## >>> Good Luck <<<\n","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"# If you find this work useful, please don't forget upvoting :)","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"# Import & Data Set","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\n\n%matplotlib inline\n\n# _______________________________________\n\n# Kernels Data (Public Score & File Path)\n\ndfk = pd.DataFrame({ \n    'Kernel ID': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],  \n    'Score'    : [7.110, 6.674, 6.290, 6.189, 6.077, 6.062, 5.995, 5.694, 5.471, 5.847],  \n    'File Path': ['../input/1-3-indoor-navigation-cost-minimization-floor/a9773cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/b9530cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/c8500cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/d8418cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/e8333cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/f8073cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/g7745cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/h7661cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/i7274cm99.csv', '../input/1-3-indoor-navigation-cost-minimization-floor/j7285cm99.csv']     \n})    \n    \ndfk   ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"# Ensembling & Comparative Method","metadata":{}},{"cell_type":"markdown","source":"## Generated(No. 1)\n\ngenerated1 >>> (Public Score: ------)","metadata":{}},{"cell_type":"code","source":"ff1x = 0.60   \nff1y = 0.60  \n\nff2x = 0.60   \nff2y = 0.60 \n\nff3x = 0.90   \nff3y = 0.90 \n\nff4x = 0.95   \nff4y = 0.95    \n\nmain = pd.read_csv(dfk.iloc[8, 2]) \nsupport = pd.read_csv(dfk.iloc[6, 2])\n\nrx , ry = [] , []\ngenerated1  = main.copy()  \n\nmx , my , mz = [] , [] , []\nmx = main.iloc[:, 2].tolist()\nmy = main.iloc[:, 3].tolist()\nmz = main.iloc[:, 1].tolist()\n\nsx , sy = [] , []\nsx = support.iloc[:, 2].tolist()\nsy = support.iloc[:, 3].tolist()\n\nkx = [[],[],[],[],[],[]]\nky = [[],[],[],[],[],[]]\nfor n in range (6):\n    kernel   = pd.read_csv(dfk.iloc[n, 2])  \n    kx[n] = kernel.iloc[:, 2].tolist()\n    ky[n] = kernel.iloc[:, 3].tolist()\n\nfor j in range(len(main)):\n    \n    ex = (mx[j] * ff1x) + (sx[j] * (1.- ff1x))\n    ey = (my[j] * ff1y) + (sy[j] * (1.- ff1y))\n    \n    if (mx[j] >= sx[j]):\n        a = mx[j]\n        b = sx[j]\n    else: \n        a = sx[j]\n        b = mx[j]\n        \n    if (my[j] >= sy[j]):\n        c = my[j]\n        d = sy[j]\n    else: \n        c = sy[j]\n        d = my[j]    \n        \n    for k in range (5, 6):            \n        if ((b <= kx[k][j] <= a) and (d <= ky[k][j] <= c)):\n            \n            ex = (ex * ff2x) + (kx[k][j] * (1.- ff2x))\n            ey = (ey * ff2y) + (ky[k][j] * (1.- ff2y))\n            \n            print(f'Row:{j}\\t Kernel Number:{k}\\t Floor:{mz[j]}')\n            print(f'Main:({mx[j]},{my[j]})\\nSupport:({sx[j]},{sy[j]})\\nKernel:({kx[k][j]},{ky[k][j]})\\nResult:({ex},{ey})\\n')        \n            \n    for k in range (3, 5):            \n        if ((b <= kx[k][j] <= a) and (d <= ky[k][j] <= c)):\n            \n            ex = (ex * ff3x) + (kx[k][j] * (1.- ff3x))\n            ey = (ey * ff3y) + (ky[k][j] * (1.- ff3y))\n            \n            print(f'Row:{j}\\t Kernel Number:{k}\\t Floor:{mz[j]}')\n            print(f'Main:({mx[j]},{my[j]})\\nSupport:({sx[j]},{sy[j]})\\nKernel:({kx[k][j]},{ky[k][j]})\\nResult:({ex},{ey})\\n')       \n        \n    for k in range (3):            \n        if ((b <= kx[k][j] <= a) and (d <= ky[k][j] <= c)):\n            \n            ex = (ex * ff4x) + (kx[k][j] * (1.- ff4x))\n            ey = (ey * ff4y) + (ky[k][j] * (1.- ff4y))\n            \n            print(f'Row:{j}\\t Kernel Number:{k}\\t Floor:{mz[j]}')\n            print(f'Main:({mx[j]},{my[j]})\\nSupport:({sx[j]},{sy[j]})\\nKernel:({kx[k][j]},{ky[k][j]})\\nResult:({ex},{ey})\\n')\n                        \n    rx.append(ex)\n    ry.append(ey)    \n\ngenerated1.iloc[:, 2] = rx\ngenerated1.iloc[:, 3] = ry\n","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generated(No. 2)\n\ngenerated2 >>> (Public Score: ------)","metadata":{}},{"cell_type":"code","source":"ff1x = 0.60   \nff1y = 0.60 \n\nff2x = 0.60   \nff2y = 0.60 \n\nff3x = 0.90   \nff3y = 0.90   \n\nff4x = 0.95   \nff4y = 0.95   \n\n\nmain = pd.read_csv(dfk.iloc[7, 2]) \nsupport = pd.read_csv(dfk.iloc[6, 2])\n\nrx , ry = [] , []\ngenerated2  = main.copy()  \n\nmx , my , mz = [] , [] , []\nmx = main.iloc[:, 2].tolist()\nmy = main.iloc[:, 3].tolist()\nmz = main.iloc[:, 1].tolist()\n\nsx , sy = [] , []\nsx = support.iloc[:, 2].tolist()\nsy = support.iloc[:, 3].tolist()\n\nkx = [[],[],[],[],[],[]]\nky = [[],[],[],[],[],[]]\nfor n in range (6):\n    kernel   = pd.read_csv(dfk.iloc[n, 2])  \n    kx[n] = kernel.iloc[:, 2].tolist()\n    ky[n] = kernel.iloc[:, 3].tolist()\n\nfor j in range(len(main)):\n    \n    ex = (mx[j] * ff1x) + (sx[j] * (1.- ff1x))\n    ey = (my[j] * ff1y) + (sy[j] * (1.- ff1y))\n    \n    if (mx[j] >= sx[j]):\n        a = mx[j]\n        b = sx[j]\n    else: \n        a = sx[j]\n        b = mx[j]\n        \n    if (my[j] >= sy[j]):\n        c = my[j]\n        d = sy[j]\n    else: \n        c = sy[j]\n        d = my[j]    \n        \n    for k in range (5, 6):            \n        if ((b <= kx[k][j] <= a) and (d <= ky[k][j] <= c)):\n            \n            ex = (ex * ff2x) + (kx[k][j] * (1.- ff2x))\n            ey = (ey * ff2y) + (ky[k][j] * (1.- ff2y))\n            \n            print(f'Row:{j}\\t Kernel Number:{k}\\t Floor:{mz[j]}')\n            print(f'Main:({mx[j]},{my[j]})\\nSupport:({sx[j]},{sy[j]})\\nKernel:({kx[k][j]},{ky[k][j]})\\nResult:({ex},{ey})\\n')        \n        \n    for k in range (3, 5):            \n        if ((b <= kx[k][j] <= a) and (d <= ky[k][j] <= c)):\n            \n            ex = (ex * ff3x) + (kx[k][j] * (1.- ff3x))\n            ey = (ey * ff3y) + (ky[k][j] * (1.- ff3y))\n            \n            print(f'Row:{j}\\t Kernel Number:{k}\\t Floor:{mz[j]}')\n            print(f'Main:({mx[j]},{my[j]})\\nSupport:({sx[j]},{sy[j]})\\nKernel:({kx[k][j]},{ky[k][j]})\\nResult:({ex},{ey})\\n')           \n\n    for k in range (3):            \n        if ((b <= kx[k][j] <= a) and (d <= ky[k][j] <= c)):\n            \n            ex = (ex * ff4x) + (kx[k][j] * (1.- ff4x))\n            ey = (ey * ff4y) + (ky[k][j] * (1.- ff4y))\n            \n            print(f'Row:{j}\\t Kernel Number:{k}\\t Floor:{mz[j]}')\n            print(f'Main:({mx[j]},{my[j]})\\nSupport:({sx[j]},{sy[j]})\\nKernel:({kx[k][j]},{ky[k][j]})\\nResult:({ex},{ey})\\n')\n                        \n    rx.append(ex)\n    ry.append(ey)    \n\ngenerated2.iloc[:, 2] = rx\ngenerated2.iloc[:, 3] = ry\n","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generated(No. 3)\n\nUse \"Ensembling\" on: \"generated1\" and \"generated2\"\n\ngenerated3 >>> (Public Score: 5.266)","metadata":{}},{"cell_type":"code","source":"ff1x = 0.60   \nff1y = 0.60  \n\nmain = generated1\nsupport = generated2\n\nrx , ry = [] , []\ngenerated3  = main.copy()  \n\nmx , my , mz = [] , [] , []\nmx = main.iloc[:, 2].tolist()\nmy = main.iloc[:, 3].tolist()\nmz = main.iloc[:, 1].tolist()\n\nsx , sy = [] , []\nsx = support.iloc[:, 2].tolist()\nsy = support.iloc[:, 3].tolist()\n\n\nfor j in range(len(main)):\n    \n    ex = (mx[j] * ff1x) + (sx[j] * (1.- ff1x))\n    ey = (my[j] * ff1y) + (sy[j] * (1.- ff1y))\n    \n    rx.append(ex)\n    ry.append(ey)    \n\ngenerated3.iloc[:, 2] = rx\ngenerated3.iloc[:, 3] = ry\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generated(No. 4)\n\nUse \"Ensembling\" on: 'I' , 'H' , 'G'\n\ngenerated4 >>> (Public Score: 5.262)","metadata":{}},{"cell_type":"code","source":"ff1x = 0.44   \nff1y = 0.44  \n\nff2x = 0.24   \nff2y = 0.24 \n\nff3x = 0.32   \nff3y = 0.32 \n\nmain = pd.read_csv(dfk.iloc[8, 2])\nsupport1 = pd.read_csv(dfk.iloc[7, 2])\nsupport2 = pd.read_csv(dfk.iloc[6, 2])\n\nrx , ry = [] , []\ngenerated4 = main.copy()  \n\nmx , my , mz = [] , [] , []\nmx = main.iloc[:, 2].tolist()\nmy = main.iloc[:, 3].tolist()\nmz = main.iloc[:, 1].tolist()\n\ns1x , s1y = [] , []\ns1x = support1.iloc[:, 2].tolist()\ns1y = support1.iloc[:, 3].tolist()\n\ns2x , s2y = [] , []\ns2x = support2.iloc[:, 2].tolist()\ns2y = support2.iloc[:, 3].tolist()\n\n\nfor j in range(len(main)):\n    \n    ex = (mx[j] * ff1x) + (s1x[j] * ff2x) + (s2x[j] * ff3x)\n    ey = (my[j] * ff1y) + (s1y[j] * ff2y) + (s2y[j] * ff3y)\n    \n    rx.append(ex)\n    ry.append(ey)    \n\ngenerated4.iloc[:, 2] = rx\ngenerated4.iloc[:, 3] = ry\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generated(No. 5)\n\nUse \"Ensembling\" on: \"generated3\" and \"generated4\"\n\ngenerated5 >>> (Public Score: 5.255)","metadata":{}},{"cell_type":"code","source":"ff1x = 0.50   \nff1y = 0.50  \n\nmain = generated3\nsupport = generated4\n\nrx , ry = [] , []\ngenerated5  = main.copy()  \n\nmx , my , mz = [] , [] , []\nmx = main.iloc[:, 2].tolist()\nmy = main.iloc[:, 3].tolist()\nmz = main.iloc[:, 1].tolist()\n\nsx , sy = [] , []\nsx = support.iloc[:, 2].tolist()\nsy = support.iloc[:, 3].tolist()\n\n\nfor j in range(len(main)):\n    \n    ex = (mx[j] * ff1x) + (sx[j] * (1.- ff1x))\n    ey = (my[j] * ff1y) + (sy[j] * (1.- ff1y))\n    \n    rx.append(ex)\n    ry.append(ey)    \n\ngenerated5.iloc[:, 2] = rx\ngenerated5.iloc[:, 3] = ry\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generated(No. 6)\n\nUse \"Ensembling\" on: 'I' , 'H' , 'G'\n\ngenerated6 >>> (Public Score: 5.265)","metadata":{}},{"cell_type":"code","source":"ff1x = 0.40   \nff1y = 0.40  \n\nff2x = 0.25   \nff2y = 0.25 \n\nff3x = 0.35   \nff3y = 0.35 \n\nmain = pd.read_csv(dfk.iloc[8, 2])\nsupport1 = pd.read_csv(dfk.iloc[7, 2])\nsupport2 = pd.read_csv(dfk.iloc[6, 2])\n\nrx , ry = [] , []\ngenerated6 = main.copy()  \n\nmx , my , mz = [] , [] , []\nmx = main.iloc[:, 2].tolist()\nmy = main.iloc[:, 3].tolist()\nmz = main.iloc[:, 1].tolist()\n\ns1x , s1y = [] , []\ns1x = support1.iloc[:, 2].tolist()\ns1y = support1.iloc[:, 3].tolist()\n\ns2x , s2y = [] , []\ns2x = support2.iloc[:, 2].tolist()\ns2y = support2.iloc[:, 3].tolist()\n\n\nfor j in range(len(main)):\n    \n    ex = (mx[j] * ff1x) + (s1x[j] * ff2x) + (s2x[j] * ff3x)\n    ey = (my[j] * ff1y) + (s1y[j] * ff2y) + (s2y[j] * ff3y)\n    \n    rx.append(ex)\n    ry.append(ey)    \n\ngenerated6.iloc[:, 2] = rx\ngenerated6.iloc[:, 3] = ry\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"sub = generated5\nsub.to_csv(\"submission.csv\", index=False)\n\ngenerated1.to_csv(\"generated1.csv\", index=False)\ngenerated2.to_csv(\"generated2.csv\", index=False)\ngenerated3.to_csv(\"generated3.csv\", index=False)\ngenerated4.to_csv(\"generated4.csv\", index=False)\ngenerated5.to_csv(\"generated5.csv\", index=False)\ngenerated6.to_csv(\"generated6.csv\", index=False)\n\n!ls","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}}]}