{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.18","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":105399,"databundleVersionId":12733338,"sourceType":"competition"}],"dockerImageVersionId":31091,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Requirements","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:03.776249Z","iopub.execute_input":"2025-08-14T07:44:03.776557Z","iopub.status.idle":"2025-08-14T07:44:03.790040Z","shell.execute_reply.started":"2025-08-14T07:44:03.776534Z","shell.execute_reply":"2025-08-14T07:44:03.785464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install polars","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:03.791947Z","iopub.execute_input":"2025-08-14T07:44:03.792178Z","iopub.status.idle":"2025-08-14T07:44:10.924823Z","shell.execute_reply.started":"2025-08-14T07:44:03.792156Z","shell.execute_reply":"2025-08-14T07:44:10.919459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import polars as pl","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:10.927544Z","iopub.execute_input":"2025-08-14T07:44:10.927784Z","iopub.status.idle":"2025-08-14T07:44:10.939550Z","shell.execute_reply.started":"2025-08-14T07:44:10.927758Z","shell.execute_reply":"2025-08-14T07:44:10.934368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:10.942058Z","iopub.execute_input":"2025-08-14T07:44:10.942337Z","iopub.status.idle":"2025-08-14T07:44:10.954028Z","shell.execute_reply.started":"2025-08-14T07:44:10.942295Z","shell.execute_reply":"2025-08-14T07:44:10.950343Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Reading Data","metadata":{}},{"cell_type":"code","source":"dft = pl.read_parquet('/kaggle/input/aeroclub-recsys-2025/train.parquet')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:10.957371Z","iopub.execute_input":"2025-08-14T07:44:10.957611Z","iopub.status.idle":"2025-08-14T07:44:13.292146Z","shell.execute_reply.started":"2025-08-14T07:44:10.957588Z","shell.execute_reply":"2025-08-14T07:44:13.286368Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Analysis","metadata":{}},{"cell_type":"code","source":"pl.read_parquet_schema('/kaggle/input/aeroclub-recsys-2025/train.parquet')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.294424Z","iopub.execute_input":"2025-08-14T07:44:13.294696Z","iopub.status.idle":"2025-08-14T07:44:13.324085Z","shell.execute_reply.started":"2025-08-14T07:44:13.294671Z","shell.execute_reply":"2025-08-14T07:44:13.318178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.326511Z","iopub.execute_input":"2025-08-14T07:44:13.326782Z","iopub.status.idle":"2025-08-14T07:44:13.354625Z","shell.execute_reply.started":"2025-08-14T07:44:13.326759Z","shell.execute_reply":"2025-08-14T07:44:13.346357Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Cleaning Data & Feature Engineering","metadata":{}},{"cell_type":"code","source":"dft= dft.drop('bySelf','sex','legs0_segments0_cabinClass','nationality','legs0_segments0_marketingCarrier_code','legs0_segments2_marketingCarrier_code','legs0_segments3_marketingCarrier_code','legs1_segments0_marketingCarrier_code','legs1_segments2_marketingCarrier_code','legs1_segments3_marketingCarrier_code')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.356688Z","iopub.execute_input":"2025-08-14T07:44:13.356933Z","iopub.status.idle":"2025-08-14T07:44:13.427295Z","shell.execute_reply.started":"2025-08-14T07:44:13.356910Z","shell.execute_reply":"2025-08-14T07:44:13.421241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft = dft.drop('legs1_segments0_duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.428107Z","iopub.execute_input":"2025-08-14T07:44:13.428373Z","iopub.status.idle":"2025-08-14T07:44:13.489722Z","shell.execute_reply.started":"2025-08-14T07:44:13.428349Z","shell.execute_reply":"2025-08-14T07:44:13.483323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft = dft.with_columns(\n    TotalPrice = pl.col(\"totalPrice\") + pl.col(\"taxes\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.491864Z","iopub.execute_input":"2025-08-14T07:44:13.492100Z","iopub.status.idle":"2025-08-14T07:44:13.558658Z","shell.execute_reply.started":"2025-08-14T07:44:13.492078Z","shell.execute_reply":"2025-08-14T07:44:13.551864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft['TotalPrice']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.560491Z","iopub.execute_input":"2025-08-14T07:44:13.560732Z","iopub.status.idle":"2025-08-14T07:44:13.575651Z","shell.execute_reply.started":"2025-08-14T07:44:13.560710Z","shell.execute_reply":"2025-08-14T07:44:13.569556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft = dft.drop('legs0_segments0_duration','legs0_segments1_duration','legs0_segments2_duration','legs0_segments3_duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.577826Z","iopub.execute_input":"2025-08-14T07:44:13.578078Z","iopub.status.idle":"2025-08-14T07:44:13.640780Z","shell.execute_reply.started":"2025-08-14T07:44:13.578055Z","shell.execute_reply":"2025-08-14T07:44:13.634803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft = dft.drop('legs1_segments1_duration','legs1_segments2_duration','legs1_segments3_duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.643336Z","iopub.execute_input":"2025-08-14T07:44:13.643630Z","iopub.status.idle":"2025-08-14T07:44:13.704296Z","shell.execute_reply.started":"2025-08-14T07:44:13.643603Z","shell.execute_reply":"2025-08-14T07:44:13.699239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft['legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.706825Z","iopub.execute_input":"2025-08-14T07:44:13.707081Z","iopub.status.idle":"2025-08-14T07:44:13.723256Z","shell.execute_reply.started":"2025-08-14T07:44:13.707056Z","shell.execute_reply":"2025-08-14T07:44:13.718169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft= dft.with_columns(\n    pl.col('legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration').str.replace(r\"T\", \"  \"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:13.725503Z","iopub.execute_input":"2025-08-14T07:44:13.725750Z","iopub.status.idle":"2025-08-14T07:44:14.054022Z","shell.execute_reply.started":"2025-08-14T07:44:13.725726Z","shell.execute_reply":"2025-08-14T07:44:14.049444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft['legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.054813Z","iopub.execute_input":"2025-08-14T07:44:14.055053Z","iopub.status.idle":"2025-08-14T07:44:14.069925Z","shell.execute_reply.started":"2025-08-14T07:44:14.055028Z","shell.execute_reply":"2025-08-14T07:44:14.065693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft= dft.with_columns(\n    pl.col('legs0_departureAt',\n'legs0_arrivalAt',\n'legs1_departureAt',\n'legs1_arrivalAt').str.to_datetime(\"%Y-%m-%d %H:%M:%S\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.070793Z","iopub.execute_input":"2025-08-14T07:44:14.070982Z","iopub.status.idle":"2025-08-14T07:44:14.362353Z","shell.execute_reply.started":"2025-08-14T07:44:14.070962Z","shell.execute_reply":"2025-08-14T07:44:14.357860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft['legs0_departureAt',\n'legs0_arrivalAt',\n'legs1_departureAt',\n'legs1_arrivalAt']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.364190Z","iopub.execute_input":"2025-08-14T07:44:14.364445Z","iopub.status.idle":"2025-08-14T07:44:14.377622Z","shell.execute_reply.started":"2025-08-14T07:44:14.364422Z","shell.execute_reply":"2025-08-14T07:44:14.373180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft['legs0_duration',\n'legs1_duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.379872Z","iopub.execute_input":"2025-08-14T07:44:14.380100Z","iopub.status.idle":"2025-08-14T07:44:14.394456Z","shell.execute_reply.started":"2025-08-14T07:44:14.380078Z","shell.execute_reply":"2025-08-14T07:44:14.388867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft = dft.with_columns(\n    pl.col('legs0_duration',\n'legs1_duration').str.replace_all(r\"\\.\\d{2}\", \"\")\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.395211Z","iopub.execute_input":"2025-08-14T07:44:14.395445Z","iopub.status.idle":"2025-08-14T07:44:14.535429Z","shell.execute_reply.started":"2025-08-14T07:44:14.395422Z","shell.execute_reply":"2025-08-14T07:44:14.530903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft=dft.with_columns(\n    pl.col('legs0_duration',\n'legs1_duration').str.to_time(\"%H:%M:%S\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.537592Z","iopub.execute_input":"2025-08-14T07:44:14.537805Z","iopub.status.idle":"2025-08-14T07:44:14.619795Z","shell.execute_reply.started":"2025-08-14T07:44:14.537784Z","shell.execute_reply":"2025-08-14T07:44:14.615035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft['legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.621729Z","iopub.execute_input":"2025-08-14T07:44:14.621960Z","iopub.status.idle":"2025-08-14T07:44:14.637679Z","shell.execute_reply.started":"2025-08-14T07:44:14.621937Z","shell.execute_reply":"2025-08-14T07:44:14.631677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt = pl.read_parquet('/kaggle/input/aeroclub-recsys-2025/test.parquet')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:14.639188Z","iopub.execute_input":"2025-08-14T07:44:14.639412Z","iopub.status.idle":"2025-08-14T07:44:15.373355Z","shell.execute_reply.started":"2025-08-14T07:44:14.639392Z","shell.execute_reply":"2025-08-14T07:44:15.368114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt= dftt.drop('bySelf','sex','legs0_segments0_cabinClass','nationality','legs0_segments0_marketingCarrier_code','legs0_segments2_marketingCarrier_code','legs0_segments3_marketingCarrier_code','legs1_segments0_marketingCarrier_code','legs1_segments2_marketingCarrier_code','legs1_segments3_marketingCarrier_code',)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.374376Z","iopub.execute_input":"2025-08-14T07:44:15.374602Z","iopub.status.idle":"2025-08-14T07:44:15.412059Z","shell.execute_reply.started":"2025-08-14T07:44:15.374580Z","shell.execute_reply":"2025-08-14T07:44:15.406746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt = dftt.with_columns(\n    TotalPrice = pl.col(\"totalPrice\") + pl.col(\"taxes\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.413634Z","iopub.execute_input":"2025-08-14T07:44:15.413852Z","iopub.status.idle":"2025-08-14T07:44:15.447062Z","shell.execute_reply.started":"2025-08-14T07:44:15.413831Z","shell.execute_reply":"2025-08-14T07:44:15.442678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt = dftt.drop('legs1_segments0_duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.448995Z","iopub.execute_input":"2025-08-14T07:44:15.449226Z","iopub.status.idle":"2025-08-14T07:44:15.481802Z","shell.execute_reply.started":"2025-08-14T07:44:15.449204Z","shell.execute_reply":"2025-08-14T07:44:15.476350Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt = dftt.drop('legs0_segments0_duration','legs0_segments1_duration','legs0_segments2_duration','legs0_segments3_duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.483403Z","iopub.execute_input":"2025-08-14T07:44:15.483610Z","iopub.status.idle":"2025-08-14T07:44:15.514346Z","shell.execute_reply.started":"2025-08-14T07:44:15.483589Z","shell.execute_reply":"2025-08-14T07:44:15.510251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt = dftt.drop('legs1_segments1_duration','legs1_segments2_duration','legs1_segments3_duration')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.516178Z","iopub.execute_input":"2025-08-14T07:44:15.516547Z","iopub.status.idle":"2025-08-14T07:44:15.551060Z","shell.execute_reply.started":"2025-08-14T07:44:15.516511Z","shell.execute_reply":"2025-08-14T07:44:15.545511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt['legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.551722Z","iopub.execute_input":"2025-08-14T07:44:15.552691Z","iopub.status.idle":"2025-08-14T07:44:15.567679Z","shell.execute_reply.started":"2025-08-14T07:44:15.552667Z","shell.execute_reply":"2025-08-14T07:44:15.563337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt= dftt.with_columns(\n    pl.col('legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration').str.replace(r\"T\", \"  \"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.569998Z","iopub.execute_input":"2025-08-14T07:44:15.570206Z","iopub.status.idle":"2025-08-14T07:44:15.718106Z","shell.execute_reply.started":"2025-08-14T07:44:15.570186Z","shell.execute_reply":"2025-08-14T07:44:15.712449Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt= dftt.with_columns(\n    pl.col('legs0_departureAt',\n'legs0_arrivalAt',\n'legs1_departureAt',\n'legs1_arrivalAt').str.to_datetime(\"%Y-%m-%d %H:%M:%S\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.720558Z","iopub.execute_input":"2025-08-14T07:44:15.721053Z","iopub.status.idle":"2025-08-14T07:44:15.822254Z","shell.execute_reply.started":"2025-08-14T07:44:15.720991Z","shell.execute_reply":"2025-08-14T07:44:15.817627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt = dftt.with_columns(\n    pl.col('legs0_duration',\n'legs1_duration').str.replace_all(r\"\\.\\d{2}\", \"\")\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.824468Z","iopub.execute_input":"2025-08-14T07:44:15.826248Z","iopub.status.idle":"2025-08-14T07:44:15.908934Z","shell.execute_reply.started":"2025-08-14T07:44:15.826217Z","shell.execute_reply":"2025-08-14T07:44:15.903264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt=dftt.with_columns(\n    pl.col('legs0_duration',\n'legs1_duration').str.to_time(\"%H:%M:%S\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.910439Z","iopub.execute_input":"2025-08-14T07:44:15.910645Z","iopub.status.idle":"2025-08-14T07:44:15.952797Z","shell.execute_reply.started":"2025-08-14T07:44:15.910624Z","shell.execute_reply":"2025-08-14T07:44:15.948619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dftt['legs0_departureAt',\n'legs0_arrivalAt',\n'legs0_duration',\n'legs1_departureAt',\n'legs1_arrivalAt',\n'legs1_duration']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.954803Z","iopub.execute_input":"2025-08-14T07:44:15.955056Z","iopub.status.idle":"2025-08-14T07:44:15.971997Z","shell.execute_reply.started":"2025-08-14T07:44:15.955031Z","shell.execute_reply":"2025-08-14T07:44:15.966161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess(df: pl.DataFrame) -> pl.DataFrame:\n    datetime_cols = [\"legs0_departureAt\", \"legs0_arrivalAt\",\n                     \"legs1_departureAt\", \"legs1_arrivalAt\"]\n\n    for col in datetime_cols:\n        # Ensure datetime type\n        df = df.with_columns(\n            pl.col(col).cast(pl.Datetime(time_unit=\"us\"))\n        )\n\n        # Basic datetime features\n        df = df.with_columns([\n            pl.col(col).dt.hour().alias(f\"{col}_hour\"),\n            pl.col(col).dt.weekday().alias(f\"{col}_dow\"),\n            pl.col(col).dt.month().alias(f\"{col}_month\"),\n            (pl.col(col).dt.weekday() >= 5).cast(pl.Int8).alias(f\"{col}_is_weekend\"),\n        ])\n\n        # Cyclical hour encoding\n        df = df.with_columns([\n            (pl.col(f\"{col}_hour\") * (2 * np.pi) / 24).sin().alias(f\"{col}_hour_sin\"),\n            (pl.col(f\"{col}_hour\") * (2 * np.pi) / 24).cos().alias(f\"{col}_hour_cos\"),\n        ])\n\n    # Duration to minutes\n    duration_cols = [\"legs0_duration\", \"legs1_duration\"]\n    for col in duration_cols:\n    # If already a Duration, just convert to minutes\n        if df[col].dtype == pl.Duration(time_unit=\"us\"):\n            df = df.with_columns(\n                (pl.col(col).dt.seconds() / 60).alias(col)\n            )\n        # If it's a Time (HH:MM:SS)\n        elif df[col].dtype == pl.Time:\n            df = df.with_columns(\n                (pl.col(col).dt.hour() * 60 +\n                 pl.col(col).dt.minute() +\n                 pl.col(col).dt.second() / 60).alias(col)\n            )\n        # If it's a string like \"02:40:00\"\n        else:\n            df = df.with_columns(\n                (pl.col(col).str.strptime(pl.Time, \"%H:%M:%S\", strict=False).dt.hour() * 60 +\n                 pl.col(col).str.strptime(pl.Time, \"%H:%M:%S\", strict=False).dt.minute() +\n                 pl.col(col).str.strptime(pl.Time, \"%H:%M:%S\", strict=False).dt.second() / 60\n                ).alias(col)\n            )\n   \n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:15.989753Z","iopub.execute_input":"2025-08-14T07:44:15.989965Z","iopub.status.idle":"2025-08-14T07:44:16.020778Z","shell.execute_reply.started":"2025-08-14T07:44:15.989944Z","shell.execute_reply":"2025-08-14T07:44:16.014241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Preprocessing...\")\ndft = preprocess(dft)\ndftt = preprocess(dftt)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:16.022960Z","iopub.execute_input":"2025-08-14T07:44:16.023233Z","iopub.status.idle":"2025-08-14T07:44:17.341251Z","shell.execute_reply.started":"2025-08-14T07:44:16.023212Z","shell.execute_reply":"2025-08-14T07:44:17.336736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dft = dft.with_columns(\n    pl.col(\"legs1_duration\",\"legs0_duration\").abs()\n)\ndftt = dftt.with_columns(\n    pl.col(\"legs1_duration\",\"legs0_duration\").abs()\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.343985Z","iopub.execute_input":"2025-08-14T07:44:17.344218Z","iopub.status.idle":"2025-08-14T07:44:17.436969Z","shell.execute_reply.started":"2025-08-14T07:44:17.344197Z","shell.execute_reply":"2025-08-14T07:44:17.432506Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Selection","metadata":{}},{"cell_type":"code","source":"y = dft['selected']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.439249Z","iopub.execute_input":"2025-08-14T07:44:17.440535Z","iopub.status.idle":"2025-08-14T07:44:17.449371Z","shell.execute_reply.started":"2025-08-14T07:44:17.440509Z","shell.execute_reply":"2025-08-14T07:44:17.444884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = dft['ranker_id','Id','TotalPrice','searchRoute','corporateTariffCode','profileId','companyID',\n'legs0_duration' ,\n'legs1_duration','legs0_segments0_seatsAvailable','legs0_segments1_seatsAvailable','legs1_segments0_seatsAvailable','legs1_segments1_seatsAvailable',\n'miniRules0_monetaryAmount' ,\n'miniRules0_percentage',\n'miniRules0_statusInfos' ,\n'miniRules1_monetaryAmount' ,\n'miniRules1_percentage' ,\n'miniRules1_statusInfos' ,\n'pricingInfo_isAccessTP' ,\n'pricingInfo_passengerCount','legs0_segments0_baggageAllowance_quantity','legs0_segments1_baggageAllowance_quantity','legs1_segments0_baggageAllowance_quantity','legs1_segments1_baggageAllowance_quantity',\n'legs0_segments0_arrivalTo_airport_iata','legs0_segments1_arrivalTo_airport_iata',\n'legs1_segments0_arrivalTo_airport_iata','legs1_segments1_arrivalTo_airport_iata',\n'legs0_segments0_arrivalTo_airport_city_iata','legs0_segments1_arrivalTo_airport_city_iata',\n'legs1_segments0_arrivalTo_airport_city_iata','legs1_segments1_arrivalTo_airport_city_iata']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.451541Z","iopub.execute_input":"2025-08-14T07:44:17.452098Z","iopub.status.idle":"2025-08-14T07:44:17.465404Z","shell.execute_reply.started":"2025-08-14T07:44:17.452075Z","shell.execute_reply":"2025-08-14T07:44:17.458725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"string_columns = [col_name for col_name, dtype in zip(x.columns, x.dtypes) if dtype == pl.Utf8]\nprint(string_columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.467384Z","iopub.execute_input":"2025-08-14T07:44:17.467578Z","iopub.status.idle":"2025-08-14T07:44:17.477500Z","shell.execute_reply.started":"2025-08-14T07:44:17.467559Z","shell.execute_reply":"2025-08-14T07:44:17.471800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = x.with_columns(\n    pl.col('searchRoute', 'legs0_segments0_arrivalTo_airport_iata', 'legs0_segments1_arrivalTo_airport_iata', 'legs1_segments0_arrivalTo_airport_iata', 'legs1_segments1_arrivalTo_airport_iata','legs0_segments0_arrivalTo_airport_city_iata', 'legs0_segments1_arrivalTo_airport_city_iata', 'legs1_segments0_arrivalTo_airport_city_iata', 'legs1_segments1_arrivalTo_airport_city_iata', 'ranker_id').cast(pl.Categorical).cast(pl.UInt32)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.479100Z","iopub.execute_input":"2025-08-14T07:44:17.479294Z","iopub.status.idle":"2025-08-14T07:44:17.865852Z","shell.execute_reply.started":"2025-08-14T07:44:17.479276Z","shell.execute_reply":"2025-08-14T07:44:17.860004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.868141Z","iopub.execute_input":"2025-08-14T07:44:17.868427Z","iopub.status.idle":"2025-08-14T07:44:17.886111Z","shell.execute_reply.started":"2025-08-14T07:44:17.868401Z","shell.execute_reply":"2025-08-14T07:44:17.880574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = x.fill_nan(0.0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:17.887047Z","iopub.execute_input":"2025-08-14T07:44:17.887252Z","iopub.status.idle":"2025-08-14T07:44:18.152468Z","shell.execute_reply.started":"2025-08-14T07:44:17.887233Z","shell.execute_reply":"2025-08-14T07:44:18.148036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xt = dftt['ranker_id','Id','TotalPrice','searchRoute','corporateTariffCode','profileId','companyID',\n'legs0_duration' ,\n'legs1_duration','legs0_segments0_seatsAvailable','legs0_segments1_seatsAvailable','legs1_segments0_seatsAvailable','legs1_segments1_seatsAvailable',\n'miniRules0_monetaryAmount' ,\n'miniRules0_percentage',\n'miniRules0_statusInfos' ,\n'miniRules1_monetaryAmount' ,\n'miniRules1_percentage' ,\n'miniRules1_statusInfos' ,\n'pricingInfo_isAccessTP' ,\n'pricingInfo_passengerCount','legs0_segments0_baggageAllowance_quantity','legs0_segments1_baggageAllowance_quantity','legs1_segments0_baggageAllowance_quantity','legs1_segments1_baggageAllowance_quantity',\n'legs0_segments0_arrivalTo_airport_iata','legs0_segments1_arrivalTo_airport_iata',\n'legs1_segments0_arrivalTo_airport_iata','legs1_segments1_arrivalTo_airport_iata',\n'legs0_segments0_arrivalTo_airport_city_iata','legs0_segments1_arrivalTo_airport_city_iata',\n'legs1_segments0_arrivalTo_airport_city_iata','legs1_segments1_arrivalTo_airport_city_iata']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:18.153396Z","iopub.execute_input":"2025-08-14T07:44:18.153643Z","iopub.status.idle":"2025-08-14T07:44:18.169057Z","shell.execute_reply.started":"2025-08-14T07:44:18.153620Z","shell.execute_reply":"2025-08-14T07:44:18.164456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xt = xt.with_columns(\n    pl.col('searchRoute', 'legs0_segments0_arrivalTo_airport_iata', 'legs0_segments1_arrivalTo_airport_iata', 'legs1_segments0_arrivalTo_airport_iata', 'legs1_segments1_arrivalTo_airport_iata','legs0_segments0_arrivalTo_airport_city_iata', 'legs0_segments1_arrivalTo_airport_city_iata', 'legs1_segments0_arrivalTo_airport_city_iata', 'legs1_segments1_arrivalTo_airport_city_iata', 'ranker_id').cast(pl.Categorical).cast(pl.UInt32)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:18.170942Z","iopub.execute_input":"2025-08-14T07:44:18.171175Z","iopub.status.idle":"2025-08-14T07:44:18.348084Z","shell.execute_reply.started":"2025-08-14T07:44:18.171152Z","shell.execute_reply":"2025-08-14T07:44:18.341541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"groups = x.select('ranker_id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:18.349599Z","iopub.execute_input":"2025-08-14T07:44:18.349820Z","iopub.status.idle":"2025-08-14T07:44:18.369351Z","shell.execute_reply.started":"2025-08-14T07:44:18.349799Z","shell.execute_reply":"2025-08-14T07:44:18.363856Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Trainig Model","metadata":{}},{"cell_type":"code","source":"! pip install XGBoost","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:18.371243Z","iopub.execute_input":"2025-08-14T07:44:18.371527Z","iopub.status.idle":"2025-08-14T07:44:26.546966Z","shell.execute_reply.started":"2025-08-14T07:44:18.371504Z","shell.execute_reply":"2025-08-14T07:44:26.542069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:26.549967Z","iopub.execute_input":"2025-08-14T07:44:26.550262Z","iopub.status.idle":"2025-08-14T07:44:26.561684Z","shell.execute_reply.started":"2025-08-14T07:44:26.550225Z","shell.execute_reply":"2025-08-14T07:44:26.555859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -U optuna\nimport optuna\nfrom sklearn.metrics import ndcg_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:26.563098Z","iopub.execute_input":"2025-08-14T07:44:26.563334Z","iopub.status.idle":"2025-08-14T07:44:34.667320Z","shell.execute_reply.started":"2025-08-14T07:44:26.563288Z","shell.execute_reply":"2025-08-14T07:44:34.662732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {\n    'objective': 'rank:pairwise',\n    'eval_metric': 'ndcg@3',\n    \"learning_rate\": 0.022641389657079056,\n    \"max_depth\": 14,\n    \"min_child_weight\": 2,\n    \"subsample\": 0.8842234913702768,\n    \"colsample_bytree\": 0.45840689146263086,\n    \"gamma\": 3.3084297630544888,\n    \"lambda\": 6.952586917313028,\n    \"alpha\": 0.6395254133055179,\n    'seed': 42,\n    'n_jobs': -1,\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:34.669555Z","iopub.execute_input":"2025-08-14T07:44:34.669802Z","iopub.status.idle":"2025-08-14T07:44:34.681570Z","shell.execute_reply.started":"2025-08-14T07:44:34.669773Z","shell.execute_reply":"2025-08-14T07:44:34.675657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"groups= groups.group_by('ranker_id', maintain_order=True).agg(pl.len())['len'].to_numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:34.682438Z","iopub.execute_input":"2025-08-14T07:44:34.682652Z","iopub.status.idle":"2025-08-14T07:44:34.822997Z","shell.execute_reply.started":"2025-08-14T07:44:34.682631Z","shell.execute_reply":"2025-08-14T07:44:34.817824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = x.to_pandas().drop(columns=[\"legs0_duration\", \"legs1_duration\"])\ndtrain = xgb.DMatrix(df, group=groups, label=y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:44:34.825821Z","iopub.execute_input":"2025-08-14T07:44:34.826084Z","iopub.status.idle":"2025-08-14T07:44:41.263356Z","shell.execute_reply.started":"2025-08-14T07:44:34.826059Z","shell.execute_reply":"2025-08-14T07:44:41.257159Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## bst = xgb.train(params, dtrain, num_boost_round=1000)","metadata":{"execution":{"iopub.status.busy":"2025-08-13T20:34:47.961748Z","iopub.execute_input":"2025-08-13T20:34:47.961963Z","iopub.status.idle":"2025-08-13T20:47:25.024613Z","shell.execute_reply.started":"2025-08-13T20:34:47.961942Z","shell.execute_reply":"2025-08-13T20:47:25.017669Z"}}},{"cell_type":"code","source":"import xgboost as xgb\n\n# Assuming dtrain is already defined\nparams = {\n    \"objective\": \"rank:pairwise\",\n    \"eval_metric\": \"ndcg\",\n    \"verbosity\": 1\n}\nnum_round = 100\nbst = xgb.train(params, dtrain, num_round)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:49:18.462620Z","iopub.execute_input":"2025-08-14T07:49:18.462934Z","iopub.status.idle":"2025-08-14T07:50:09.150293Z","shell.execute_reply.started":"2025-08-14T07:49:18.462910Z","shell.execute_reply":"2025-08-14T07:50:09.144574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"groupt = xt.select('ranker_id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:50:09.152617Z","iopub.execute_input":"2025-08-14T07:50:09.152841Z","iopub.status.idle":"2025-08-14T07:50:09.174468Z","shell.execute_reply.started":"2025-08-14T07:50:09.152820Z","shell.execute_reply":"2025-08-14T07:50:09.170270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" groupt= groupt.group_by('ranker_id', maintain_order=True).agg(pl.len())['len'].to_numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:50:09.175728Z","iopub.execute_input":"2025-08-14T07:50:09.175965Z","iopub.status.idle":"2025-08-14T07:50:09.238748Z","shell.execute_reply.started":"2025-08-14T07:50:09.175943Z","shell.execute_reply":"2025-08-14T07:50:09.232767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dtest = xgb.DMatrix(xt,group = groupt)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:50:09.240740Z","iopub.execute_input":"2025-08-14T07:50:09.240967Z","iopub.status.idle":"2025-08-14T07:50:12.034007Z","shell.execute_reply.started":"2025-08-14T07:50:09.240945Z","shell.execute_reply":"2025-08-14T07:50:12.028319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract expected feature names from training data\nexpected_features = dtrain.feature_names\n\n# Filter your test DataFrame BEFORE converting to DMatrix\nx_test_filtered = xt.select(expected_features)\n\n# Convert to DMatrix\ndtest = xgb.DMatrix(x_test_filtered)\n\n# Make predictions\npredictions = bst.predict(dtest)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:53:54.566695Z","iopub.execute_input":"2025-08-14T07:53:54.567019Z","iopub.status.idle":"2025-08-14T07:53:57.520110Z","shell.execute_reply.started":"2025-08-14T07:53:54.566992Z","shell.execute_reply":"2025-08-14T07:53:57.515648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = (\n    dftt.select(['Id','ranker_id'])\n    .with_columns(pl.Series('pred_score', predictions))\n    .with_columns(\n        pl.col('pred_score')\n        .rank(method='ordinal', descending=True)\n        .over('ranker_id')\n        .cast(pl.Int32)\n        .alias('selected')\n    )\n    .select(['Id','ranker_id', 'selected'])\n)\nsubmission.write_csv('/kaggle/working/submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:53:57.522091Z","iopub.execute_input":"2025-08-14T07:53:57.522330Z","iopub.status.idle":"2025-08-14T07:53:58.747940Z","shell.execute_reply.started":"2025-08-14T07:53:57.522293Z","shell.execute_reply":"2025-08-14T07:53:58.743261Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:53:58.749117Z","iopub.execute_input":"2025-08-14T07:53:58.749377Z","iopub.status.idle":"2025-08-14T07:53:58.760152Z","shell.execute_reply.started":"2025-08-14T07:53:58.749342Z","shell.execute_reply":"2025-08-14T07:53:58.754829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"z = pd.read_csv('/kaggle/working/submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:53:58.762386Z","iopub.execute_input":"2025-08-14T07:53:58.763784Z","iopub.status.idle":"2025-08-14T07:54:01.736604Z","shell.execute_reply.started":"2025-08-14T07:53:58.763757Z","shell.execute_reply":"2025-08-14T07:54:01.731972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"z.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:54:01.740224Z","iopub.execute_input":"2025-08-14T07:54:01.740794Z","iopub.status.idle":"2025-08-14T07:54:01.754494Z","shell.execute_reply.started":"2025-08-14T07:54:01.740769Z","shell.execute_reply":"2025-08-14T07:54:01.748750Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"z.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-14T07:54:01.756177Z","iopub.execute_input":"2025-08-14T07:54:01.756527Z","iopub.status.idle":"2025-08-14T07:54:01.797862Z","shell.execute_reply.started":"2025-08-14T07:54:01.756506Z","shell.execute_reply":"2025-08-14T07:54:01.793346Z"}},"outputs":[],"execution_count":null}]}