{"cells":[{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>"},{"metadata":{},"cell_type":"markdown","source":"<div>\n    <h1 align=\"center\">\"AutoML in Kaggle Kernels\"</h1></h1>\n    <h1 align=\"center\">Rainforest Connection Species Audio 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":"### Import & Data Set"},{"metadata":{"trusted":true},"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\nked = pd.DataFrame({      \n    'Kernel ID': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M'],\n    'Symbol':    ['SoliSet', '[Inference] ResNest RFCX Audio Detection',  'notebookba481ef16a', 'All-in-one RFCX baseline for beginners', 'RFCX: train resnet50 with TPU',  'RFCX Resnet50 TPU', 'ResNet34 More Augmentations+Mixup+TTA (Inference)', '[Inference][TPU] RFCX Audio Detection Fast++','RFX Bagging Different Weights','resnetwavenet','bestbag','pytorch adas optimizer','pytorch adas better'],\n    'Score':     [ 0.589 , 0.594 , 0.613 , 0.748 , 0.793 , 0.824 , 0.845 , 0.861, 0.876, 0.877,0.877,0.825, 0.849 ],\n    'File Path': ['../input/audio-detection-soliset-201/submission.csv', '../input/inference-resnest-rfcx-audio-detection/submission.csv', '../input/minimal-fastai-solution-score-0-61/submission.csv', '../input/all-in-one-rfcx-baseline-for-beginners/submission.csv', '../input/rfcx-train-resnet50-with-tpu/submission.csv', '../input/rfcx-resnet50-tpu/submission.csv', '../input/resnet34-more-augmentations-mixup-tta-inference/submission.csv', '../input/inference-tpu-rfcx-audio-detection-fast/submission.csv','../input/rfcx-bagging-with-different-weights-0-876-score/submission.csv','../input/resnet-wavenet-my-best-single-model-ensemble/submission.csv','../input/bagging-rainforest/submission_best.csv','../input/rfcx-adas-optimizer-pytorch/submission.csv','../input/pytorch-training-rfcx-adas-optimizer-resnest/submission.csv'],        \n    'Note'     : ['xgboost & cuml(https://rapids.ai)', 'torch & resnest50', 'fastai.vision & torchaudio', 'torch & resnest50', 'tensorflow & tf.keras.Sequential', 'tensorflow & tf.keras.Sequential', 'tensorflow & classification_models.keras', 'torch & resnest50', 'bagging','0.877','0.877','0.825','0.849']\n})    \n    \nked    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>"},{"metadata":{},"cell_type":"markdown","source":"### Kernel Class & Instances"},{"metadata":{"trusted":true},"cell_type":"code","source":"class Kernel():    \n    '''\n       Class Kernel V 1.0\n       Input Argument:       \n       - symbol      (kernel name OR author)       \n       - score       (Score for the kernel)\n       - file_path   (CSV file address)\n    ''' \n      \n    def __init__(self, symbol, score, file_path):  \n        \n        self.symbol = symbol\n        self.score = score\n        \n        self.file_path = file_path\n        self.sub = pd.read_csv(self.file_path)\n        \n            \n    def __str__(self):\n        return f'Kernel: {self.symbol}\\t| Score: {self.score}'\n\n    \n    def __repr__(self):\n        return f'Class: {self.__class__.__name__}\\nName: {repr(self.symbol)}\\t| Score: {self.score}'   \n\n        \n    def print_head(self):\n        print(self)\n        print(f'\\nHead:\\n')\n        print(self.sub.head())        \n    \n    \n    def print_description(self):\n        print(self)      \n        print(f'\\nDescription:\\n')\n        print(self.sub.describe())\n        \n        \n    def generation(self, other, coeff):\n        g1 = self.sub.copy()\n        g2 = self.sub.copy()\n        g3 = self.sub.copy()\n        g4 = self.sub.copy() \n        \n        if isinstance(other, Kernel):             \n            for i in self.sub.columns[1:]: \n                \n                lm, Is = [], []                \n                lm = self.sub[i].tolist()\n                ls = other.sub[i].tolist()        \n                res1, res2, res3, res4 = [], [], [], []  \n                \n                for j in range(len(self.sub)): \n                    \n                    res1.append(max(lm[j] , ls[j]))\n                    res2.append(min(lm[j] , ls[j]))\n                    res3.append((lm[j] + ls[j]) / 2)\n                    res4.append((lm[j] * coeff) + (ls[j] * (1.- coeff)))        \n        \n                g1[i] = res1\n                g2[i] = res2\n                g3[i] = res3\n                g4[i] = res4\n                \n        return g1,g2,g3,g4   \n    \n# ____________________________________________\n    \n# Seven instance of \"Kernel\" class is defined.\n\nfor i in range(13):   \n    ked.iloc[i, 0] = Kernel(ked.iloc[i, 1], ked.iloc[i, 2], ked.iloc[i, 3])     \n#    ked.iloc[i, 0].print_head() \n#    ked.iloc[i, 0].print_description() \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[0, 0])\nked.iloc[0, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[1, 0])\nked.iloc[1, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[2, 0])\nked.iloc[2, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[3, 0])\nked.iloc[3, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[4, 0])\nked.iloc[4, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[5, 0])\nked.iloc[5, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[6, 0])\nked.iloc[6, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[7, 0])\nked.iloc[7, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[8, 0])\nked.iloc[8, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ked.iloc[9, 0])\nked.iloc[9, 0].sub.describe()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>"},{"metadata":{},"cell_type":"markdown","source":"### Increase the best score.\nCan the results of the better kernels support each other? YES:)"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Auxiliary function\ndef generate(main, support, coeff):\n    g1 = main.copy()\n    g2 = main.copy()\n    g3 = main.copy()\n    g4 = main.copy()\n    \n    for i in main.columns[1:]:\n        lm, Is = [], []                \n        lm = main[i].tolist()\n        ls = support[i].tolist() \n        \n        res1, res2, res3, res4 = [], [], [], []          \n        for j in range(len(main)):\n            res1.append(max(lm[j] , ls[j]))\n            res2.append(min(lm[j] , ls[j]))\n            res3.append((lm[j] + ls[j]) / 2)\n            res4.append((lm[j] * coeff) + (ls[j] * (1.- coeff)))\n            \n        g1[i] = res1\n        g2[i] = res2\n        g3[i] = res3\n        g4[i] = res4\n        \n    return g1,g2,g3,g4\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>"},{"metadata":{},"cell_type":"markdown","source":"### For example:\nTo increase the score of the best kernel (Score: 0.845), we get help from the kernel with a score of 0.824."},{"metadata":{"trusted":true},"cell_type":"code","source":"g1,g2,g3,g4 = generate(ked.iloc[10, 0].sub, ked.iloc[12, 0].sub, 0.8)\n\n# g1,g2,g3,g4 = ked.iloc[6, 0].generation(ked.iloc[5, 0], 0.8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Maximum function    | Score: 0.828')\ng1.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Minimum function    | Score: 0.848')\ng2.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Mean function    | Score: 0.845')\ng3.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Coefficient function (Coeff: 0.8, 0.2)    | Score: 0.847')\ng4.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Version 1\n# We have now selected the minimum function.\n# sub = g2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>"},{"metadata":{},"cell_type":"markdown","source":"### Example: 2\nTo increase the score of the best kernel (Score: 0.861), we get help from the g2 kernel with a score of 0.848."},{"metadata":{"trusted":true},"cell_type":"code","source":"f1,f2,f3,f4 = generate(ked.iloc[9, 0].sub, g2, 0.8)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Maximum function    | Score: 0.000')\nf1.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Minimum function    | Score: 0.866')\nf2.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Mean function    | Score: 0.000')\nf3.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Coefficient function (Coeff: 0.8, 0.2)    | Score: 0.000')\nf4.describe()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> <div class=\"alert alert-success\">  \n</div>"},{"metadata":{"trusted":true},"cell_type":"code","source":"# We have selected the minimum function.\nsub = f2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)\nf1.to_csv(\"submission1.csv\", index=False)\nf2.to_csv(\"submission2.csv\", index=False)\nf3.to_csv(\"submission3.csv\", index=False)\nf4.to_csv(\"submission4.csv\", index=False)\n\n!ls","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Example: 2\nTo increase the score of the best kernel (Score: 0.876), we get help from the g2 kernel with a score of 0.861."},{"metadata":{"trusted":true},"cell_type":"code","source":"h1,h2,h3,h4 = generate(ked.iloc[12, 0].sub, g2, 1.0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = h2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<div class=\"alert alert-success\">  \n</div>"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)\nh1.to_csv(\"submission1.csv\", index=False)\nh2.to_csv(\"submission2.csv\", index=False)\nh3.to_csv(\"submission3.csv\", index=False)\nh4.to_csv(\"submission4.csv\", index=False)\n\n!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}