{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":116062,"databundleVersionId":14084779,"sourceType":"competition"},{"sourceId":13738853,"sourceType":"datasetVersion","datasetId":8741713},{"sourceId":268734519,"sourceType":"kernelVersion"},{"sourceId":275639766,"sourceType":"kernelVersion"}],"dockerImageVersionId":31192,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 🧬 CAFA 6 - Ensemble Blend\n\n**Approach:** Weighted ensemble of multiple prediction models.\n\n**Acknowledgments:** Thanks to Jirka Borovec, whose work made this possible.","metadata":{}},{"cell_type":"code","source":"# Imports\nimport pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-13T15:00:20.626164Z","iopub.execute_input":"2025-11-13T15:00:20.626934Z","iopub.status.idle":"2025-11-13T15:00:20.982316Z","shell.execute_reply.started":"2025-11-13T15:00:20.626876Z","shell.execute_reply":"2025-11-13T15:00:20.981341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Ensemble weights\nWEIGHT_MODEL_A = 0.80\nWEIGHT_MODEL_B = 0.20\nDIVERSITY_BONUS = 0.05\nCONFIDENCE_THRESHOLD = 0.01","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-13T15:00:20.984015Z","iopub.execute_input":"2025-11-13T15:00:20.984495Z","iopub.status.idle":"2025-11-13T15:00:20.989263Z","shell.execute_reply.started":"2025-11-13T15:00:20.984464Z","shell.execute_reply":"2025-11-13T15:00:20.988272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Model A\nmodel_a = pd.read_csv('/kaggle/input/cafa-6-blend-goa-negative-propagation/submission.tsv',\n                      sep='\\t', header=None, \n                      names=['protein', 'go_term', 'score'])\nmodel_a['key'] = model_a['protein'] + '|' + model_a['go_term']\nmodel_a['score'] = model_a['score'].clip(0, 1.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-13T15:00:20.990317Z","iopub.execute_input":"2025-11-13T15:00:20.990872Z","iopub.status.idle":"2025-11-13T15:00:54.255682Z","shell.execute_reply.started":"2025-11-13T15:00:20.990846Z","shell.execute_reply":"2025-11-13T15:00:54.254615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Model B\nmodel_b = pd.read_csv('/kaggle/input/cafa-6-protein-function-prediction-with-1d-cnn/submission.tsv',\n                      sep='\\t', header=None,\n                      names=['protein', 'go_term', 'score'])\nmodel_b['key'] = model_b['protein'] + '|' + model_b['go_term']\nmodel_b['score'] = model_b['score'].clip(0, 1.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-13T15:00:54.257438Z","iopub.execute_input":"2025-11-13T15:00:54.257809Z","iopub.status.idle":"2025-11-13T15:01:06.702099Z","shell.execute_reply.started":"2025-11-13T15:00:54.257785Z","shell.execute_reply":"2025-11-13T15:01:06.701258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Noise strength\nNOISE_LEVEL = 0.02\n\n# Create Ensemble\ndict_a = dict(zip(model_a['key'], model_a['score']))\ndict_b = dict(zip(model_b['key'], model_b['score']))\n\nall_keys = set(model_a['key'].unique()) | set(model_b['key'].unique())\n\nresults = []\nfor key in all_keys:\n    protein, go_term = key.split('|')\n    \n    score_a = dict_a.get(key, 0)\n    score_b = dict_b.get(key, 0)\n    \n    weighted_score = (score_a * WEIGHT_MODEL_A + score_b * WEIGHT_MODEL_B)\n    \n    if score_a > 0 and score_b > 0:\n        final_score = min(weighted_score + DIVERSITY_BONUS, 1.0)\n    else:\n        final_score = weighted_score\n\n    # 🔥 Add small uniform noise\n    final_score += np.random.uniform(-NOISE_LEVEL, NOISE_LEVEL)\n    final_score = np.clip(final_score, 0, 1)\n\n    results.append({\n        'protein': protein,\n        'go_term': go_term,\n        'score': final_score\n    })\n\nensemble = pd.DataFrame(results)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-13T15:01:06.702999Z","iopub.execute_input":"2025-11-13T15:01:06.703217Z","iopub.status.idle":"2025-11-13T15:05:15.026256Z","shell.execute_reply.started":"2025-11-13T15:01:06.703199Z","shell.execute_reply":"2025-11-13T15:05:15.024779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Filter and Save\nensemble = ensemble.sort_values('score', ascending=False)\nensemble = ensemble[ensemble['score'] >= CONFIDENCE_THRESHOLD]\n\nensemble[['protein', 'go_term', 'score']].to_csv('submission.tsv', \n                                                   sep='\\t', \n                                                   index=False, \n                                                   header=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-13T15:05:15.028068Z","iopub.execute_input":"2025-11-13T15:05:15.02839Z","iopub.status.idle":"2025-11-13T15:08:00.415447Z","shell.execute_reply.started":"2025-11-13T15:05:15.028357Z","shell.execute_reply":"2025-11-13T15:08:00.414326Z"}},"outputs":[],"execution_count":null}]}