{"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":82253,"databundleVersionId":8965849,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-29T05:10:56.422019Z","iopub.execute_input":"2024-06-29T05:10:56.422581Z","iopub.status.idle":"2024-06-29T05:10:56.430307Z","shell.execute_reply.started":"2024-06-29T05:10:56.422539Z","shell.execute_reply":"2024-06-29T05:10:56.429075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/f1nalyze-datathon-ieeecsmuj/train.csv')\ntest_df = pd.read_csv('/kaggle/input/f1nalyze-datathon-ieeecsmuj/test.csv')\nvalidation_df = pd.read_csv('/kaggle/input/f1nalyze-datathon-ieeecsmuj/validation.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:18:22.762067Z","iopub.execute_input":"2024-06-29T05:18:22.762807Z","iopub.status.idle":"2024-06-29T05:18:51.865060Z","shell.execute_reply.started":"2024-06-29T05:18:22.762766Z","shell.execute_reply":"2024-06-29T05:18:51.864065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport xgboost as xgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import KNNImputer\n\n# Load data\n\n# Define columns to drop\ncolumns_to_drop = [\n    'driverRef', 'driver_num', 'driver_code', 'fp1_date', 'fp1_time', 'fp2_date', 'fp2_time',\n    'fp3_date', 'fp3_time', 'quali_time', 'quali_date', 'sprint_date', 'sprint_time', 'url',\n    'nationality_y', 'constructorRef', 'url_y', 'nationality', 'surname', 'forename', 'url_x',\n    'grand_prix', 'number', 'company', 'positionText_y', 'position_x', 'positionText_x',\n    'positionOrder', 'laps', 'time_x', 'timetaken_in_millisec', 'rank', 'max_speed', 'round',\n    'points', 'fastestLap', 'statusId', 'driverStandingsId', 'points_y', 'raceId_y', 'status',\n     'time_y', 'fastestLapTime'\n]\n\ncolumns_to_drop_for_test = [\n    'driverRef', 'driver_num', 'driver_code', 'fp1_date', 'fp1_time', 'fp2_date', 'fp2_time',\n    'fp3_date', 'fp3_time', 'quali_time', 'quali_date', 'sprint_date', 'sprint_time', 'url',\n    'nationality_y', 'constructorRef', 'url_y', 'nationality', 'surname', 'forename', 'url_x',\n    'grand_prix', 'number', 'company', 'position_x', 'positionText_x',\n    'positionOrder', 'laps', 'time_x', 'timetaken_in_millisec', 'rank', 'max_speed', 'round',\n    'points', 'fastestLap', 'statusId', 'driverStandingsId', 'points_y', 'raceId_y', 'status',\n     'time_y', 'fastestLapTime'\n]\n# Drop columns from train, test, and validation datasets\ntrain_df = train_df.drop(columns=columns_to_drop)\ntest_df = test_df.drop(columns=columns_to_drop_for_test)\nvalidation_df = validation_df.drop(columns=columns_to_drop)\n\n# Calculate age\ntrain_df['date'] = pd.to_datetime(train_df['date'])\ntrain_df['dob'] = pd.to_datetime(train_df['dob'])\ntest_df['date'] = pd.to_datetime(test_df['date'])\ntest_df['dob'] = pd.to_datetime(test_df['dob'])\nvalidation_df['date'] = pd.to_datetime(validation_df['date'])\nvalidation_df['dob'] = pd.to_datetime(validation_df['dob'])\n\ndef calculate_age(row):\n    age = row['date'].year - row['dob'].year\n    if (row['date'].month, row['date'].day) < (row['dob'].month, row['dob'].day):\n        age -= 1\n    return int(age)\n\ntrain_df['age'] = train_df.apply(calculate_age, axis=1)\ntest_df['age'] = test_df.apply(calculate_age, axis=1)\nvalidation_df['age'] = validation_df.apply(calculate_age, axis=1)\n\n# Drop remaining date columns\ntrain_df = train_df.drop(columns=['date', 'dob'])\ntest_df = test_df.drop(columns=['date', 'dob'])\nvalidation_df = validation_df.drop(columns=['date', 'dob'])\n\n# Remove duplicates from train dataset\ntrain_df = train_df.drop_duplicates()\n\n# Split features and target\nX_train = train_df.drop('position', axis=1)  # Features\ny_train = train_df['position']  # Target\nX_valid = validation_df.drop('position', axis=1)\ny_valid = validation_df['position']\n\n# Impute missing values using KNN imputer\nknn_imputer = KNNImputer(n_neighbors=5)\nX_train = knn_imputer.fit_transform(X_train)\nX_valid = knn_imputer.transform(X_valid)\nX_test = knn_imputer.transform(test_df)\n\n# Convert data to DMatrix format for XGBoost\ndtrain = xgb.DMatrix(X_train, label=y_train)\ndvalid = xgb.DMatrix(X_valid, label=y_valid)\ndtest = xgb.DMatrix(X_test)\n\n# Define XGBoost parameters\nparams = {\n    'objective': 'reg:squarederror',\n    'max_depth': 7,\n    'learning_rate': 0.05,\n    'subsample': 0.8,\n    'colsample_bytree': 0.8,\n    'n_estimators': 1000,\n    'early_stopping_rounds': 10\n}\n\n# Train the model with validation\nxgbr = xgb.train(params, dtrain, evals=[(dvalid, 'validation')], early_stopping_rounds=10)\n\n# Predict on the test data\ny_pred = xgbr.predict(dtest)\n\n# Prepare the submission file\ntest_df['predicted_position'] = y_pred\nsubmission_df = test_df[['predicted_position', 'result_driver_standing']]\nsubmission_df.columns = ['Position', 'Driver Standings']\n\n# Save to submission.csv\nsubmission_df.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-29T05:19:41.995084Z","iopub.execute_input":"2024-06-29T05:19:41.995473Z","iopub.status.idle":"2024-06-29T05:21:25.715405Z","shell.execute_reply.started":"2024-06-29T05:19:41.995439Z","shell.execute_reply":"2024-06-29T05:21:25.714375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/f1nalyze-datathon-ieeecsmuj/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:30:31.011575Z","iopub.execute_input":"2024-06-28T13:30:31.012101Z","iopub.status.idle":"2024-06-28T13:30:57.695984Z","shell.execute_reply.started":"2024-06-28T13:30:31.012062Z","shell.execute_reply":"2024-06-28T13:30:57.694482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_to_drop = ['driverRef', 'driver_num', 'driver_code','fp1_date','fp1_time','fp2_date','fp2_time','fp3_date','fp3_time','quali_time','quali_date','sprint_date','sprint_time','url','nationality_y','constructorRef','url_y','nationality','surname','forename','url_x','grand_prix','number','company','positionText_y']\ndf = df.drop(columns=columns_to_drop)","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:30:58.942444Z","iopub.execute_input":"2024-06-28T13:30:58.942950Z","iopub.status.idle":"2024-06-28T13:30:59.998684Z","shell.execute_reply.started":"2024-06-28T13:30:58.942887Z","shell.execute_reply":"2024-06-28T13:30:59.997499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:32:21.808046Z","iopub.execute_input":"2024-06-28T13:32:21.808484Z","iopub.status.idle":"2024-06-28T13:32:21.819672Z","shell.execute_reply.started":"2024-06-28T13:32:21.808447Z","shell.execute_reply":"2024-06-28T13:32:21.818101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:12:26.538013Z","iopub.execute_input":"2024-06-28T13:12:26.538377Z","iopub.status.idle":"2024-06-28T13:12:28.026915Z","shell.execute_reply.started":"2024-06-28T13:12:26.538351Z","shell.execute_reply":"2024-06-28T13:12:28.025621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-06-28T07:56:20.874592Z","iopub.execute_input":"2024-06-28T07:56:20.874992Z","iopub.status.idle":"2024-06-28T07:56:23.886655Z","shell.execute_reply.started":"2024-06-28T07:56:20.874959Z","shell.execute_reply":"2024-06-28T07:56:23.885597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['status'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:33:12.482964Z","iopub.execute_input":"2024-06-28T13:33:12.484281Z","iopub.status.idle":"2024-06-28T13:33:12.513378Z","shell.execute_reply.started":"2024-06-28T13:33:12.484236Z","shell.execute_reply":"2024-06-28T13:33:12.511883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:22:44.806587Z","iopub.execute_input":"2024-06-28T13:22:44.807204Z","iopub.status.idle":"2024-06-28T13:22:44.816655Z","shell.execute_reply.started":"2024-06-28T13:22:44.807154Z","shell.execute_reply":"2024-06-28T13:22:44.815267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['fastestLap'] = pd.to_numeric(df['fastestLap'], errors='coerce').fillna(0).astype('Int64')\ndf['max_speed'] = pd.to_numeric(df['max_speed'], errors='coerce').fillna(0).astype('float64')\ndf['positionText_x'] = pd.to_numeric(df['positionText_x'], errors='coerce').fillna(0).astype('Int64')\ndf['position_x'] = pd.to_numeric(df['position_x'], errors='coerce').fillna(0).astype('Int64')\ndf['rank'] = pd.to_numeric(df['rank'], errors='coerce').fillna(0).astype('Int64')\ndf['timetaken_in_millisec'] = pd.to_numeric(df['timetaken_in_millisec'], errors='coerce').fillna(0).astype('Int64')","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:32:56.155438Z","iopub.execute_input":"2024-06-28T13:32:56.155918Z","iopub.status.idle":"2024-06-28T13:33:05.626418Z","shell.execute_reply.started":"2024-06-28T13:32:56.155883Z","shell.execute_reply":"2024-06-28T13:33:05.625034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['status'], labels = pd.factorize(df['status'])","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:33:09.033401Z","iopub.execute_input":"2024-06-28T13:33:09.033895Z","iopub.status.idle":"2024-06-28T13:33:09.222784Z","shell.execute_reply.started":"2024-06-28T13:33:09.033825Z","shell.execute_reply":"2024-06-28T13:33:09.221367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_time = 0\ndef convert_to_seconds(lap_time_str, base_time):\n    if isinstance(lap_time_str, str):\n        # Check for full timestamp format (hh:mm:ss.S)\n        if ':' in lap_time_str:\n            parts = lap_time_str.split(':')\n            if len(parts) == 3:\n                # Format is hh:mm:ss.S\n                base_time = pd.to_timedelta(lap_time_str).total_seconds()\n            elif len(parts) == 2:\n                # Format is mm:ss.S\n                minutes, seconds = parts\n                base_time = int(minutes) * 60 + float(seconds)\n            return base_time\n        \n        # Handle relative time format (+S.S or +SS.S)\n        if lap_time_str[0] == '+':\n            lap_time_str = lap_time_str[1:]\n            return base_time + float(lap_time_str)\n    \n    # Default case for unexpected formats or already numeric values\n    return float(lap_time_str)\n# Apply conversion function to 'lap_time' column\ndf['time_x'] = df['time_x'].replace('\\\\N', np.nan)\ndf['time_x'] = df['time_x'].fillna('0')\n\ntime_x_seconds = []\nfor lap_time in df['time_x']:\n    base_time = convert_to_seconds(lap_time, base_time)\n    time_x_seconds.append(base_time)\n\ndf['time_x'] = time_x_seconds","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:33:24.951756Z","iopub.execute_input":"2024-06-28T13:33:24.953110Z","iopub.status.idle":"2024-06-28T13:33:29.452484Z","shell.execute_reply.started":"2024-06-28T13:33:24.953053Z","shell.execute_reply":"2024-06-28T13:33:29.451260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['fastestLapTime'] = df['fastestLapTime'].replace('\\\\N', np.nan)\n\n# Function to convert lap time strings to seconds\ndef convert_to_seconds(lap_time_str):\n    if isinstance(lap_time_str, str):\n        parts = lap_time_str.split(':')\n        if len(parts) == 2:\n            minutes, seconds = parts\n            return int(minutes) * 60 + float(seconds)\n    return lap_time_str\n\n# Apply conversion function to 'lap_time' column\ndf['fastestLapTime'] = df['fastestLapTime'].apply(convert_to_seconds)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:33:33.682269Z","iopub.execute_input":"2024-06-28T13:33:33.683663Z","iopub.status.idle":"2024-06-28T13:33:35.335153Z","shell.execute_reply.started":"2024-06-28T13:33:33.683603Z","shell.execute_reply":"2024-06-28T13:33:35.333614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['time_y'] = df['time_y'].replace('\\\\N', np.nan)\ndf['time_y'] = df['time_y'].fillna('0')\ndf['time_y'] = pd.to_timedelta(df['time_y'])","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:33:37.924754Z","iopub.execute_input":"2024-06-28T13:33:37.925841Z","iopub.status.idle":"2024-06-28T13:33:41.557107Z","shell.execute_reply.started":"2024-06-28T13:33:37.925797Z","shell.execute_reply":"2024-06-28T13:33:41.555709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date'] = pd.to_datetime(df['date'])\ndf['dob'] = pd.to_datetime(df['dob'])\n\ndef calculate_age(row):\n    age = row['date'].year - row['dob'].year\n    if (row['date'].month, row['date'].day) < (row['dob'].month, row['dob'].day):\n        age -= 1\n    return age\n\ndf['age'] = df.apply(calculate_age, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:33:42.953112Z","iopub.execute_input":"2024-06-28T13:33:42.954313Z","iopub.status.idle":"2024-06-28T13:35:13.243136Z","shell.execute_reply.started":"2024-06-28T13:33:42.954266Z","shell.execute_reply":"2024-06-28T13:35:13.241754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['age']","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:20:32.635247Z","iopub.execute_input":"2024-06-28T13:20:32.635696Z","iopub.status.idle":"2024-06-28T13:20:32.646522Z","shell.execute_reply.started":"2024-06-28T13:20:32.635662Z","shell.execute_reply":"2024-06-28T13:20:32.644966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"duplicate_rows = df[df.duplicated()]\n\n\n# Remove duplicates\ndf = df.drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:35:18.099428Z","iopub.execute_input":"2024-06-28T13:35:18.099905Z","iopub.status.idle":"2024-06-28T13:35:23.151400Z","shell.execute_reply.started":"2024-06-28T13:35:18.099867Z","shell.execute_reply":"2024-06-28T13:35:23.150041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:35:25.976786Z","iopub.execute_input":"2024-06-28T13:35:25.977308Z","iopub.status.idle":"2024-06-28T13:35:25.983360Z","shell.execute_reply.started":"2024-06-28T13:35:25.977269Z","shell.execute_reply":"2024-06-28T13:35:25.982037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['status']","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:35:29.604857Z","iopub.execute_input":"2024-06-28T13:35:29.605358Z","iopub.status.idle":"2024-06-28T13:35:29.616361Z","shell.execute_reply.started":"2024-06-28T13:35:29.605324Z","shell.execute_reply":"2024-06-28T13:35:29.614970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nnumeric_columns = df.select_dtypes(include=['int64', 'float64'])\ncorr_matrix = numeric_columns.corr()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:35:34.430313Z","iopub.execute_input":"2024-06-28T13:35:34.430784Z","iopub.status.idle":"2024-06-28T13:35:41.300853Z","shell.execute_reply.started":"2024-06-28T13:35:34.430748Z","shell.execute_reply":"2024-06-28T13:35:41.298613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 16))  # Optional: Adjust the figure size for better readability\nsns.heatmap(corr_matrix, annot=True, cmap='coolwarm', linewidths=0.5)\n\n# Display the heatmap\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-28T13:35:43.375543Z","iopub.execute_input":"2024-06-28T13:35:43.376105Z","iopub.status.idle":"2024-06-28T13:35:45.711773Z","shell.execute_reply.started":"2024-06-28T13:35:43.376064Z","shell.execute_reply":"2024-06-28T13:35:45.710297Z"},"trusted":true},"execution_count":null,"outputs":[]}]}