{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":105399,"databundleVersionId":12733338,"sourceType":"competition"},{"sourceId":12753960,"sourceType":"datasetVersion","datasetId":8057028}],"dockerImageVersionId":31042,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Flight Recommendation System for Business Travelers**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom pathlib import Path\n\n# --- 1. Configuration ---\n# ❗️ IMPORTANT: Add the outputs of your base notebooks as data sources to this one\nINPUT_DIR = Path('/kaggle/input/blendmy/')\n\n# List your submission files\nsub_files = {\n    'sub_A': INPUT_DIR / 'submission (19).csv',\n    'sub_B': INPUT_DIR / 'submission (21).csv',\n    'sub_C': INPUT_DIR / 'submission (23).csv'\n}\n\n# --- 2. Helper Functions ---\ndef rank_to_score(sr):\n    \"\"\"Converts ranks (lower is better) to scores (higher is better).\"\"\"\n    return 1 / sr\n\ndef score_to_rank(s):\n    \"\"\"Converts scores back to ranks.\"\"\"\n    return s.rank(method='first', ascending=False).astype(int)\n\n# --- 3. Blending Logic ---\nprint(\"Loading submission files...\")\n# Load all submission files into a list\ndfs = [pd.read_csv(path) for path in sub_files.values()]\n\n# Convert ranks to scores\nscore_frames = []\nfor i, df in enumerate(dfs):\n    tmp = df[['Id', 'ranker_id', 'selected']].copy()\n    tmp['score'] = tmp.groupby('ranker_id')['selected'].transform(rank_to_score)\n    score_frames.append(tmp[['Id', 'ranker_id', 'score']].rename(columns={'score': f'score_{i}'}))\n\n# Merge all score dataframes together\nmerged = score_frames[0]\nfor frame in score_frames[1:]:\n    merged = merged.merge(frame, on=['Id', 'ranker_id'], how='left')\n\n# --- 4. Weighted Averaging ---\n# Define weights for each submission. Give more weight to your best-performing models.\nweights = [0.70, 0.25, 0.10] # Corresponds to sub_A, sub_B, sub_C\nscore_cols = [f'score_{i}' for i in range(len(dfs))]\nw = pd.Series(weights, index=score_cols)\n\n# Compute the weighted average score\nmerged['final_score'] = (merged[score_cols] * w).sum(axis=1) / w.sum()\n\n# Convert the final blended scores back to ranks\nmerged['selected'] = merged.groupby('ranker_id')['final_score'].transform(score_to_rank)\n\n# --- 5. Save Final Submission ---\nfinal_submission = merged[['Id', 'ranker_id', 'selected']]\nfinal_submission.to_csv(\"submission.csv\", index=False)\nprint(\"✅ Final blended submission created successfully!\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}