{"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 all notebook authors whose work made this possible, especially Guntas Dhanjal.","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-18T00:27:43.284882Z","iopub.execute_input":"2025-11-18T00:27:43.285196Z","iopub.status.idle":"2025-11-18T00:27:44.962158Z","shell.execute_reply.started":"2025-11-18T00:27:43.285166Z","shell.execute_reply":"2025-11-18T00:27:44.961109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Ensemble weights\nWEIGHT_MODEL_A = 0.20  # Adjusted weight for Model A\nWEIGHT_MODEL_B = 0.20  # Weight for Model B\nWEIGHT_MODEL_C = 0.20  # New weight for Model C\nDIVERSITY_BONUS = 0.05\nCONFIDENCE_THRESHOLD = 0.01","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-18T00:27:44.964726Z","iopub.execute_input":"2025-11-18T00:27:44.965214Z","iopub.status.idle":"2025-11-18T00:27:44.970955Z","shell.execute_reply.started":"2025-11-18T00:27:44.965190Z","shell.execute_reply":"2025-11-18T00:27:44.969805Z"}},"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-18T00:27:44.971917Z","iopub.execute_input":"2025-11-18T00:27:44.972189Z","iopub.status.idle":"2025-11-18T00:28:20.867887Z","shell.execute_reply.started":"2025-11-18T00:27:44.972169Z","shell.execute_reply":"2025-11-18T00:28:20.866612Z"}},"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-18T00:28:20.868806Z","iopub.execute_input":"2025-11-18T00:28:20.869063Z","iopub.status.idle":"2025-11-18T00:28:58.935979Z","shell.execute_reply.started":"2025-11-18T00:28:20.869044Z","shell.execute_reply":"2025-11-18T00:28:58.934094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Model C\nmodel_c = pd.read_csv('/kaggle/input/temp81304597/submission.tsv',\n                      sep='\\t', header=None,\n                      names=['protein', 'go_term', 'score'])\nmodel_c['key'] = model_c['protein'] + '|' + model_c['go_term']\nmodel_c['score'] = model_c['score'].clip(0, 1.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-18T00:28:58.937850Z","iopub.execute_input":"2025-11-18T00:28:58.938169Z","iopub.status.idle":"2025-11-18T00:29:15.472838Z","shell.execute_reply.started":"2025-11-18T00:28:58.938141Z","shell.execute_reply":"2025-11-18T00:29:15.471269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Noise strength\nNOISE_LEVEL = 0.0015\n# Create Ensemble\ndict_a = dict(zip(model_a['key'], model_a['score']))\ndict_b = dict(zip(model_b['key'], model_b['score']))\ndict_c = dict(zip(model_c['key'], model_c['score']))  # Added Model C\n\n# Get all unique keys from Models A, B, and C\nall_keys = set(model_a['key'].unique()) | set(model_b['key'].unique()) | set(model_c['key'].unique())\n\nresults = []\nfor key in all_keys:\n    protein, go_term = key.split('|')\n    \n    # Get scores from each model (default to 0 if the key isn't in the model)\n    score_a = dict_a.get(key, 0)\n    score_b = dict_b.get(key, 0)\n    score_c = dict_c.get(key, 0)  # Added Model C score\n    \n    # Calculate weighted average of scores from all models\n    weighted_score = (score_a * WEIGHT_MODEL_A + \n                      score_b * WEIGHT_MODEL_B + \n                      score_c * WEIGHT_MODEL_C)\n    \n    # Apply diversity bonus if both scores are greater than 0\n    if score_a > 0 and score_b > 0 and score_c > 0:  # Adjusted condition for all models\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    # Append result\n    results.append({\n        'protein': protein,\n        'go_term': go_term,\n        'score': final_score\n    })\n\n# Create a DataFrame for the final ensemble results\nensemble = pd.DataFrame(results)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-18T00:29:15.474052Z","iopub.execute_input":"2025-11-18T00:29:15.474330Z","iopub.status.idle":"2025-11-18T00:43:07.245511Z","shell.execute_reply.started":"2025-11-18T00:29:15.474309Z","shell.execute_reply":"2025-11-18T00:43:07.244085Z"}},"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-18T00:43:07.250111Z","iopub.execute_input":"2025-11-18T00:43:07.250454Z","execution_failed":"2025-11-18T00:43:16.352Z"}},"outputs":[],"execution_count":null}]}