{"cells":[{"metadata":{},"cell_type":"markdown","source":"**This notebook is based on ensembling of the predictions. I've tried to make things easier by implementing functions for the ensembling task.**\n\n**You could run the notebook for either of the modes of submission viz multi-mode or single-mode.**","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"**I've ensembled the predictions from Peter's as well as Paulo Pinto's csv files along with the predictions from the model that I trained. You could look over my model's training process [here](https://www.kaggle.com/forwet/lyft-efficientnet-model-train).**","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# IMPORTS\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Load Submission files\nsubmission_1 = pd.read_csv(\"../input/submission-v3/submission_1000_iter.csv\")\nsubmission_2 = pd.read_csv(\"../input/submission-peter/submission_peter.csv\")\nsubmission_3 = pd.read_csv(\"../input/score-by-confidence/submission_new.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Utility Scripts\n\ndef load_submission(mode=\"multi\"):\n    \"\"\"Returns submission file for corresponding mode viz multi-mode and single mode\"\"\"\n    if mode==\"multi\":\n        return pd.read_csv(\"../input/lyft-motion-prediction-autonomous-vehicles/multi_mode_sample_submission.csv\")\n    else:\n        return pd.read_csv(\"../input/lyft-motion-prediction-autonomous-vehicles/single_mode_sample_submission.csv\")\n\ndef generate_submission(base_submission, coefs, submissions=None):\n    \"\"\"Updates base_submission to the new predictions\"\"\"\n    \n    # Getting Columns\n    cols = list(base_submission.columns)\n    \n    # Getting Coefficients and column indices\n    # for predictions of different models\n    coords = []\n    coff = cols[2:5]\n    cords1 = cols[5: 105]\n    coords.append(cols[5: 105])\n    coords.append(cols[105: 205])\n    coords.append(cols[205:305])\n\n    assert submissions != None , \"At least one submission file should be provided\"\n    \n    # Updating columns for corresponding submission file.\n    # Note that even if you provide more than three submission files,\n    # only the first three would be selected as size of coords is 3.\n    print(\"Ensembling predictions...\")\n    for cord, each in zip(coords, submissions):\n        base_submission[cord] = each[cords1]\n    print(\"Ensembling done...\")\n    \n    # Updating the coefs \n    assert len(coefs) == 3, \"Length of coefs must be exactly three\"\n    assert sum(coefs) == 1, \"Sum of coefs must be exactly one\"\n    \n    base_submission[coff] = coefs\n    \n    return base_submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission = load_submission(mode=\"multi\")\n\nsubmission = generate_submission(sample_submission, [0.2 ,0.1, 0.7], \n                                 [submission_1, submission_2, submission_3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False, float_format=\"%.6g\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}