{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67356,"databundleVersionId":8006601,"sourceType":"competition"},{"sourceId":8134894,"sourceType":"datasetVersion","datasetId":4808715},{"sourceId":8137337,"sourceType":"datasetVersion","datasetId":4810555},{"sourceId":172552710,"sourceType":"kernelVersion"},{"sourceId":172553682,"sourceType":"kernelVersion"},{"sourceId":172554050,"sourceType":"kernelVersion"},{"sourceId":173330471,"sourceType":"kernelVersion"},{"sourceId":173358569,"sourceType":"kernelVersion"},{"sourceId":173358791,"sourceType":"kernelVersion"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"![](https://cdn-images-1.medium.com/max/1000/1*VZNekSiJoJCcYACvIseF9Q.png)\n","metadata":{}},{"cell_type":"code","source":"from IPython.display import HTML\nimport time\n\nhandle = display(HTML(\"\"\"<marquee>👌</marquee>\"\"\"), display_id='html_marquee1')\ntime.sleep(2)\nhandle = display(HTML(\"\"\"<marquee>Note: Due to memory limitations, data separation and regression calculations were done in six notebooks.</marquee>\"\"\"), display_id='html_marquee1', update=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-22T16:53:37.311408Z","iopub.execute_input":"2024-04-22T16:53:37.312503Z","iopub.status.idle":"2024-04-22T16:53:39.356005Z","shell.execute_reply.started":"2024-04-22T16:53:37.312458Z","shell.execute_reply":"2024-04-22T16:53:39.354813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## <div style=\"color:lightgray;background-color:navy;padding:1.2%;border-radius:12px 12px;font-size:1em;text-align:center\">[6/6]⚕️Competition Submission - KNN</div>\n\n## <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:lightgray;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:navy;overflow:hidden;font-size:70%;letter-spacing:0.5px;margin:0\"><b> </b>Description</p></div>\n\n- The examples in the competition dataset are represented by a binary classification of whether a given small molecule is a binder or not to one of three protein targets.\n\n- Three protein targets were screened for this competition :\n\n>\n> **EPHX2 (sEH)**\n>\n> The first target, epoxide hydrolase 2, is encoded by the EPHX2 genetic locus, and its protein product is commonly named “soluble epoxide hydrolase”, or abbreviated to sEH.\n>\n> **BRD4**\n>\n> The second target, bromodomain 4, is encoded by the BRD4 locus and its protein product is also named BRD4. \n>\n> **ALB (HSA)**\n>\n> The third target, serum albumin, is encoded by the ALB locus and its protein product is also named ALB. \n>\n\n- The specifications of the columns of the train file and the test file are as follows :\n\n| Number | Features | Description |\n| ----------- | ----------- | ----------- |\n| 1 | **<span style=\"color: green;\">'id'** | A unique example_id that we use to identify the molecule-binding target pair. |  \n| 2 | **<span style=\"color: green;\">'buildingblock1_smiles'** | The structure, in SMILES, of the first building block | \n| 3 | **<span style=\"color: green;\">'buildingblock2_smiles'** | The structure, in SMILES, of the second building block |  \n| 4 | **<span style=\"color: green;\">'buildingblock3_smiles'** | The structure, in SMILES, of the third building block |  \n| 5 | **<span style=\"color: green;\">'molecule_smiles'** | The structure of the fully assembled molecule, in SMILES. This includes the three building blocks and the triazine core. - Note we use a [Dy] as the stand-in for the DNA linker. |  \n| 6 | **<span style=\"color: green;\">'protein_name'** | The protein target name | \n| 7 | **<span style=\"color: green;\">'binds'** | The target column. A binary class label of whether the molecule binds to the protein. Not available for the test set. |  \n ","metadata":{}},{"cell_type":"markdown","source":"## <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:lightgray;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:navy;overflow:hidden;font-size:70%;letter-spacing:0.5px;margin:0\"><b> </b>Details about the experiments</p></div>\n\n- DELs are libraries of small molecules with unique DNA barcodes covalently attached\n\n- DELs are manufactured by combining different building blocks\n\n![](https://cdn-images-1.medium.com/max/1000/1*4Fu8NvfopCK-1XN6p7FeUw.png)\n\n## <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:lightgray;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:navy;overflow:hidden;font-size:70%;letter-spacing:0.5px;margin:0\"><b> </b>Insight :</p></div>\n\n>\n> The **testing data** has 1674896 rows. A large number of rows slows down the work and practically limits the use of a variety of solutions. Perhaps it would have been better if the evaluation in this competition was done with fewer samples.\n>\n> In the first notebook, we randomly separated 2,000,000 rows of **training data** by protein type. Of course, in the second notebook, we used only part of the rows for modeling. The third, fourth, and fifth notebooks are for the prediction of each protein. In the sixth notebook, only the submission of results for the challenge is done.\n>\n> So if the first notebook is run several times and each time the regression calculations are done in subsequent notebooks, all these results can be used as **cross-validation**.\n>\n> In addition to the output of first notebook, you can also use the public dataset **\"BELKA_frag_1\"** at the following address :\n>\n> https://www.kaggle.com/datasets/mehrankazeminia/belka-frag-1\n>\n## <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:lightgray;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:navy;overflow:hidden;font-size:70%;letter-spacing:0.5px;margin:0\"><b> </b>Notebooks :</p></div>\n\n| Number | Notebooks |\n| ----------- | ----------- |\n| 1 | **<span style=\"color: red;\"> ⚕️EDA & Data separation based on protein type** |\n| 2 | **<span style=\"color: navy;\">⚕️Create Models [LinearSVR], [KNN], [LGBM], ...** |\n| 3 | **<span style=\"color: red;\"> ⚕️Prediction for protein_name= sEH** |\n| 4 | **<span style=\"color: navy;\">⚕️Prediction for protein_name= BRD4** |\n| 5 | **<span style=\"color: red;\"> ⚕️Prediction for protein_name= HSA** |\n| 6 | **<span style=\"color: navy;\">⚕️Competition Submission** |\n","metadata":{}},{"cell_type":"markdown","source":"<p style=\"border-bottom: 10px solid navy\"></p>\n\n<p style=\"border-bottom: 10px solid navy\"></p>","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport glob\nimport random\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nfrom tqdm import tqdm\nfrom scipy import stats\nfrom pathlib import Path\nfrom itertools import groupby\n# ..................................\nimport matplotlib.pyplot as plt\nimport plotly.figure_factory as ff\nimport plotly.express as px\n%matplotlib inline","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-22T16:53:39.358203Z","iopub.execute_input":"2024-04-22T16:53:39.358618Z","iopub.status.idle":"2024-04-22T16:53:43.863490Z","shell.execute_reply.started":"2024-04-22T16:53:39.358583Z","shell.execute_reply":"2024-04-22T16:53:43.862060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/*","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:43.871905Z","iopub.execute_input":"2024-04-22T16:53:43.872405Z","iopub.status.idle":"2024-04-22T16:53:45.070487Z","shell.execute_reply.started":"2024-04-22T16:53:43.872364Z","shell.execute_reply":"2024-04-22T16:53:45.069004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:darkcyan;\">Competition Submission</span>\n\n<p style=\"border-bottom: 10px solid darkcyan\"></p>","metadata":{}},{"cell_type":"code","source":"# protein_name ='sEH'\npred1 = pd.read_csv('../input/p-3-6-belka-prediction-for-seh-knn/pred1.csv')\n\npred1.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:45.076491Z","iopub.execute_input":"2024-04-22T16:53:45.076990Z","iopub.status.idle":"2024-04-22T16:53:45.412189Z","shell.execute_reply.started":"2024-04-22T16:53:45.076940Z","shell.execute_reply":"2024-04-22T16:53:45.411008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# protein_name ='BRD4'\npred2 = pd.read_csv('../input/p-4-6-belka-prediction-for-brd4-knn/pred2.csv')\n\npred2.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:45.413623Z","iopub.execute_input":"2024-04-22T16:53:45.414030Z","iopub.status.idle":"2024-04-22T16:53:45.736653Z","shell.execute_reply.started":"2024-04-22T16:53:45.413998Z","shell.execute_reply":"2024-04-22T16:53:45.735396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# protein_name ='HSA'\npred3 = pd.read_csv('../input/p-5-6-belka-prediction-for-hsa-knn/pred3.csv')\n\npred3.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:45.738342Z","iopub.execute_input":"2024-04-22T16:53:45.738880Z","iopub.status.idle":"2024-04-22T16:53:46.073675Z","shell.execute_reply.started":"2024-04-22T16:53:45.738821Z","shell.execute_reply":"2024-04-22T16:53:46.072090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.concat([pred1, pred2, pred3])\npred","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:46.075977Z","iopub.execute_input":"2024-04-22T16:53:46.076842Z","iopub.status.idle":"2024-04-22T16:53:46.133412Z","shell.execute_reply.started":"2024-04-22T16:53:46.076785Z","shell.execute_reply":"2024-04-22T16:53:46.132167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\nplt.figure(figsize=(10, 4))\nplt.hist(pred['binds'], bins=50)\n\nplt.gca().set_facecolor('lightyellow')\nplt.suptitle('Prediction Histogram', y=0.96, fontsize=16, c='darkred')\n\nround(pred['binds'].min(), 3), round(pred['binds'].max(), 3)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:46.137712Z","iopub.execute_input":"2024-04-22T16:53:46.140117Z","iopub.status.idle":"2024-04-22T16:53:46.804215Z","shell.execute_reply.started":"2024-04-22T16:53:46.140067Z","shell.execute_reply":"2024-04-22T16:53:46.802923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_knn_1 = pred.sort_values(by=['id'])\nsub_knn_1.to_csv('sub_knn_1.csv', index=False)\n# Public Score: 0.416\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:46.805942Z","iopub.execute_input":"2024-04-22T16:53:46.806369Z","iopub.status.idle":"2024-04-22T16:53:51.613384Z","shell.execute_reply.started":"2024-04-22T16:53:46.806332Z","shell.execute_reply":"2024-04-22T16:53:51.611611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training is done with different rows of training data.\n\nsub_knn_2 = pd.read_csv('../input/belka-knn-410/submission.csv')\nsub_knn_3 = pd.read_csv('../input/belka-knn-411/submission.csv')\n\n# Similar to cross validation\n\nsub_knn = pd.read_csv('../input/leash-BELKA/sample_submission.csv')\nsub_knn['binds'] = (sub_knn_1['binds'].values + sub_knn_2['binds'].values + sub_knn_3['binds'].values) / 3\nsub_knn.to_csv('sub_knn.csv', index=False)\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:53:51.616505Z","iopub.execute_input":"2024-04-22T16:53:51.617007Z","iopub.status.idle":"2024-04-22T16:54:00.101882Z","shell.execute_reply.started":"2024-04-22T16:53:51.616964Z","shell.execute_reply":"2024-04-22T16:54:00.100194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:darkcyan;\">Ensembling</span>\n\n<p style=\"border-bottom: 10px solid darkcyan\"></p>","metadata":{}},{"cell_type":"code","source":"# Training is done with different rows of training data.\n# Thanks to: @motono0223\n\nsub_auto_1 = pd.read_csv('../input/leash-bio-automl-baseline/submission.csv')\nsub_auto_2 = pd.read_csv('../input/leash-bio-automl-baseline-2/submission.csv')\nsub_auto_3 = pd.read_csv('../input/k/mehrankazeminia/leash-bio-automl-baseline/submission.csv')\n\n# Similar to cross validation\n\nsub_auto = pd.read_csv('../input/leash-BELKA/sample_submission.csv')\nsub_auto['binds'] = (sub_auto_1['binds'].values + sub_auto_2['binds'].values + sub_auto_3['binds'].values) / 3\nsub_auto.to_csv('sub_auto.csv', index=False)\n# Public Score: 0.502\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:54:00.103902Z","iopub.execute_input":"2024-04-22T16:54:00.104344Z","iopub.status.idle":"2024-04-22T16:54:10.614741Z","shell.execute_reply.started":"2024-04-22T16:54:00.104303Z","shell.execute_reply":"2024-04-22T16:54:10.613024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/leash-BELKA/sample_submission.csv')\nsubmission['binds'] = (sub_knn['binds'].values * 0.20) + (sub_auto['binds'].values * 0.80)\nsubmission.to_csv('submission.csv', index=False)\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:54:10.616634Z","iopub.execute_input":"2024-04-22T16:54:10.617099Z","iopub.status.idle":"2024-04-22T16:54:17.995785Z","shell.execute_reply.started":"2024-04-22T16:54:10.617061Z","shell.execute_reply":"2024-04-22T16:54:17.994273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\nplt.figure(figsize=(10, 4))\nplt.hist(submission['binds'], bins=50)\n\nplt.gca().set_facecolor('lightyellow')\nplt.suptitle('Ensembling Histogram', y=0.96, fontsize=16, c='darkred')\n\nround(submission['binds'].min(), 3), round(submission['binds'].max(), 3)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T16:54:17.998286Z","iopub.execute_input":"2024-04-22T16:54:17.998711Z","iopub.status.idle":"2024-04-22T16:54:18.570520Z","shell.execute_reply.started":"2024-04-22T16:54:17.998649Z","shell.execute_reply":"2024-04-22T16:54:18.569145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"border-bottom: 10px solid darkcyan\"></p>\n<p style=\"border-bottom: 10px solid darkcyan\"></p>","metadata":{}}]}