{"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\">Smart Ensembling</h1>    \n    <h1 align=\"center\">G2Net Gravitational Wave Detection</h1> \n    <h4 align=\"center\">By: Somayyeh Gholami & Mehran Kazeminia</h4>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">\n    <h1 align=\"center\">If you find this work useful, please don't forget upvoting :)</h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"## Import","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport seaborn as sns\n\nimport matplotlib.pyplot as plt\nimport plotly.figure_factory as ff\nimport plotly.express as px\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:29.589177Z","iopub.execute_input":"2021-08-16T06:56:29.589792Z","iopub.status.idle":"2021-08-16T06:56:33.712028Z","shell.execute_reply.started":"2021-08-16T06:56:29.589645Z","shell.execute_reply":"2021-08-16T06:56:33.710927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, roc_curve, auc","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:33.713864Z","iopub.execute_input":"2021-08-16T06:56:33.714284Z","iopub.status.idle":"2021-08-16T06:56:33.887676Z","shell.execute_reply.started":"2021-08-16T06:56:33.714236Z","shell.execute_reply":"2021-08-16T06:56:33.886696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"## Functions","metadata":{}},{"cell_type":"code","source":"def ensembling(main, support, coeff1, coeff2, coeff3, coeff4, coeff5, coeff6, coeff7, coeff8): \n    \n    suba  = main.copy()\n    subav = suba.values\n    \n    subb  = support.copy()\n    subbv = subb.values    \n              \n    ense  = main.copy()\n    ensev = ense.values\n \n    for i in range (len(main)):       \n        idst = subav[i, 0]\n        pera = subav[i, 1]       \n        perb = subbv[i, 1] \n        \n        if ((idst[0]=='0') or (idst[0]=='1')):        \n            per = (pera * coeff1) + (perb * (1.0 - coeff1))\n\n        if ((idst[0]=='2') or (idst[0]=='3')):        \n            per = (pera * coeff2) + (perb * (1.0 - coeff2))\n            \n        if ((idst[0]=='4') or (idst[0]=='5')):        \n            per = (pera * coeff3) + (perb * (1.0 - coeff3))\n\n        if ((idst[0]=='6') or (idst[0]=='7')):        \n            per = (pera * coeff4) + (perb * (1.0 - coeff4))   \n                      \n        if ((idst[0]=='8') or (idst[0]=='9')):        \n            per = (pera * coeff5) + (perb * (1.0 - coeff5))\n\n        if ((idst[0]=='a') or (idst[0]=='b')):        \n            per = (pera * coeff6) + (perb * (1.0 - coeff6))\n            \n        if ((idst[0]=='c') or (idst[0]=='d')):        \n            per = (pera * coeff7) + (perb * (1.0 - coeff7))\n\n        if ((idst[0]=='e') or (idst[0]=='f')):        \n            per = (pera * coeff8) + (perb * (1.0 - coeff8))                             \n           \n        ensev[i, 1] = per\n        \n    ense.iloc[:, 1] = ensev[:, 1]\n\n    ###############################    \n    X  = suba.iloc[:, 1]\n    Y1 = subb.iloc[:, 1]\n    Y2 = ense.iloc[:, 1]\n    \n    plt.style.use('seaborn-whitegrid') \n    plt.figure(figsize=(9, 9), facecolor='lightgray')\n    plt.title(f'\\nE N S E M B L I N G\\n')   \n      \n    plt.scatter(X, Y1, s=1.5, label='Support')    \n    plt.scatter(X, Y2, s=1.5, label='Generated')\n    plt.scatter(X, X , s=0.1, label='Main(X=Y)')\n    \n    plt.legend(fontsize=12, loc=2)\n    #plt.savefig('Ensembling_1.png')\n    plt.show()     \n    ###############################   \n    ense.iloc[:, 1] = ense.iloc[:, 1].astype(float)\n    hist_data = [subb.iloc[:, 1], ense.iloc[:, 1], suba.iloc[:, 1]] \n    group_labels = ['Support', 'Ensembling', 'Main']\n    \n    fig = ff.create_distplot(hist_data, group_labels, bin_size=.2, show_hist=False, show_rug=False)\n    fig.show()   \n    ###############################   \n    \n    return ense     \n","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:33.889508Z","iopub.execute_input":"2021-08-16T06:56:33.889804Z","iopub.status.idle":"2021-08-16T06:56:33.910946Z","shell.execute_reply.started":"2021-08-16T06:56:33.889767Z","shell.execute_reply":"2021-08-16T06:56:33.909421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def drawing(ense, comp):    \n    comp.iloc[:, 1] = comp.iloc[:, 1].astype(float)\n    hist_data = [ense.iloc[:, 1], comp.iloc[:, 1]] \n    group_labels = [ 'Ensembling', 'Comparative Method']\n    \n    fig = ff.create_distplot(hist_data, group_labels, bin_size=.2, show_hist=False, show_rug=False)\n    fig.show()   ","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:33.913242Z","iopub.execute_input":"2021-08-16T06:56:33.913819Z","iopub.status.idle":"2021-08-16T06:56:33.9287Z","shell.execute_reply.started":"2021-08-16T06:56:33.913687Z","shell.execute_reply":"2021-08-16T06:56:33.927577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"## Data Set","metadata":{}},{"cell_type":"markdown","source":"Thanks to: @miklgr500 https://www.kaggle.com/miklgr500/g2net-efficientnetb1-tpu-evaluate/output","metadata":{}},{"cell_type":"code","source":"path0 = '../input/g2net-834/submission.csv'\n\nsub834a = pd.read_csv(path0).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:33.930502Z","iopub.execute_input":"2021-08-16T06:56:33.931107Z","iopub.status.idle":"2021-08-16T06:56:34.54233Z","shell.execute_reply.started":"2021-08-16T06:56:33.931063Z","shell.execute_reply":"2021-08-16T06:56:34.540907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks to: @mrigendraagrawal https://www.kaggle.com/mrigendraagrawal/tf-g2net-eda-and-starter","metadata":{}},{"cell_type":"code","source":"path1 = '../input/g2net-855a/submission.csv'\n\nsub855a = pd.read_csv(path1).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:34.543818Z","iopub.execute_input":"2021-08-16T06:56:34.544142Z","iopub.status.idle":"2021-08-16T06:56:35.074064Z","shell.execute_reply.started":"2021-08-16T06:56:34.544114Z","shell.execute_reply":"2021-08-16T06:56:35.07175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks to: @yasufuminakama https://www.kaggle.com/yasufuminakama/g2net-efficientnet-b7-baseline-inference","metadata":{}},{"cell_type":"code","source":"path2 = '../input/g2net-860/submission.csv'\n\nsub860a = pd.read_csv(path2).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:35.076141Z","iopub.execute_input":"2021-08-16T06:56:35.076565Z","iopub.status.idle":"2021-08-16T06:56:35.605124Z","shell.execute_reply.started":"2021-08-16T06:56:35.076529Z","shell.execute_reply":"2021-08-16T06:56:35.604105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks to: @wabinab https://www.kaggle.com/wabinab/submission-baseline","metadata":{}},{"cell_type":"code","source":"path3 = '../input/g2net-861/submission.csv'\n\nsub861a = pd.read_csv(path3).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:35.608226Z","iopub.execute_input":"2021-08-16T06:56:35.608718Z","iopub.status.idle":"2021-08-16T06:56:36.119421Z","shell.execute_reply.started":"2021-08-16T06:56:35.608673Z","shell.execute_reply":"2021-08-16T06:56:36.118435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks to: @ihelon https://www.kaggle.com/ihelon/g2net-eda-and-modeling/output?select=model_submission.csv","metadata":{}},{"cell_type":"code","source":"path4 = '../input/g2net-864/model_submission.csv'\n\nsub864a = pd.read_csv(path4).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:36.121247Z","iopub.execute_input":"2021-08-16T06:56:36.121733Z","iopub.status.idle":"2021-08-16T06:56:36.681374Z","shell.execute_reply.started":"2021-08-16T06:56:36.12168Z","shell.execute_reply":"2021-08-16T06:56:36.679957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks to: @miklgr500 https://www.kaggle.com/miklgr500/cqt-g2net-efficientnetb1-tpu-inference/output?select=submission.csv\n\nThanks to: @xuxu1234 https://www.kaggle.com/xuxu1234/lb-0-866-g2net-efficientnetb7-tpu-inference","metadata":{}},{"cell_type":"code","source":"path5 = '../input/g2net-866/submission.csv'\n\nsub866a = pd.read_csv(path5).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:36.683205Z","iopub.execute_input":"2021-08-16T06:56:36.683698Z","iopub.status.idle":"2021-08-16T06:56:37.243542Z","shell.execute_reply.started":"2021-08-16T06:56:36.68365Z","shell.execute_reply":"2021-08-16T06:56:37.242236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Thanks to: @hidehisaarai1213 https://www.kaggle.com/hidehisaarai1213/g2net-tf-on-the-fly-cqt-tpu-inference","metadata":{}},{"cell_type":"code","source":"path6 = '../input/g2net-869/submission.csv'\n\nsub869a = pd.read_csv(path6).sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:37.247546Z","iopub.execute_input":"2021-08-16T06:56:37.247902Z","iopub.status.idle":"2021-08-16T06:56:38.118135Z","shell.execute_reply.started":"2021-08-16T06:56:37.247872Z","shell.execute_reply":"2021-08-16T06:56:38.116839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = [path0, path1, path2, path3, path4, path5, path6]","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:38.119712Z","iopub.execute_input":"2021-08-16T06:56:38.120121Z","iopub.status.idle":"2021-08-16T06:56:38.125125Z","shell.execute_reply.started":"2021-08-16T06:56:38.120079Z","shell.execute_reply":"2021-08-16T06:56:38.124145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_data = [sub834a.target, sub855a.target, sub860a.target, sub861a.target, sub864a.target, sub866a.target, sub869a.target]  \n\ngroup_labels = ['0.834a', '0.855a', '0.860a', '0.861a', '0.864a', '0.866a', '0.869a']\n    \nfig = ff.create_distplot(hist_data, group_labels, bin_size=.2, show_hist=False, show_rug=False) \n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:56:38.126457Z","iopub.execute_input":"2021-08-16T06:56:38.126796Z","iopub.status.idle":"2021-08-16T06:56:56.92378Z","shell.execute_reply.started":"2021-08-16T06:56:38.126766Z","shell.execute_reply":"2021-08-16T06:56:56.922581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">\n    <h1 align=\"center\">Ensembling</h1>\n</div>","metadata":{}},{"cell_type":"code","source":"sub1 = ensembling(sub855a, sub834a, 0.42, 0.42, 0.42, 1.00, 1.00, 0.42, 0.42, 0.42)\n\nsub2 = ensembling(sub860a,    sub1, 0.60, 0.60, 0.60, 0.65, 0.65, 0.60, 0.60, 0.60)\n\nsub3 = ensembling(sub861a,    sub2, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45, 0.45)\n\nsub4 = ensembling(sub864a,    sub3, 0.45, 0.45, 0.40, 0.55, 0.30, 0.45, 0.45, 0.45)\n\nsub5 = ensembling(sub866a,    sub4, 0.50, 0.50, 0.50, 0.50, 0.55, 0.50, 0.50, 0.50)\n\nsub6 = ensembling(sub869a,    sub5, 0.25, 0.25, 0.25, 0.25, 0.35, 0.25, 0.25, 0.25)","metadata":{},"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":"sub1.to_csv(\"submission1.csv\",index=False)\nsub2.to_csv(\"submission2.csv\",index=False)\nsub3.to_csv(\"submission3.csv\",index=False)\nsub4.to_csv(\"submission4.csv\",index=False)\nsub5.to_csv(\"submission5.csv\",index=False)\n\nsub6.to_csv(\"submission_final.csv\",index=False)\n!ls","metadata":{"execution":{"iopub.status.busy":"2021-08-16T06:57:56.507647Z","iopub.execute_input":"2021-08-16T06:57:56.508239Z","iopub.status.idle":"2021-08-16T06:58:02.463414Z","shell.execute_reply.started":"2021-08-16T06:57:56.508185Z","shell.execute_reply":"2021-08-16T06:58:02.46203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}},{"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>","metadata":{}}]}