{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"nvidiaTeslaT4","dataSources":[{"sourceId":82253,"databundleVersionId":8965849,"sourceType":"competition"},{"sourceId":8808953,"sourceType":"datasetVersion","datasetId":5298232}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.model_selection import StratifiedKFold, cross_val_score\nfrom sklearn.ensemble import BaggingClassifier\nfrom sklearn.metrics import accuracy_score\nfrom xgboost import XGBRegressor\nfrom hyperopt import STATUS_OK, Trials, fmin, hp, tpe\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.experimental import enable_iterative_imputer\nfrom sklearn.impute import IterativeImputer\nfrom sklearn.metrics import mean_squared_error\nimport warnings\n\nwarnings.filterwarnings(\"ignore\")\n# Load the datasets\nX = pd.read_csv(\"/kaggle/input/f1-analyze-dataset/train.csv\")\np = pd.read_csv(\"/kaggle/input/f1-analyze-dataset/test.csv\")\nv = pd.read_csv(\"/kaggle/input/f1-analyze-dataset/validation.csv\")\n\ncolumns_to_drop = [\n    'fp1_date','position','number','driverRef','driver_num','driver_code','forename','surname',\n    'fp1_time', 'fp2_date', 'fp2_time', 'fp3_date', 'fp3_time',\n    'quali_date', 'quali_time', 'sprint_date', 'sprint_time', 'url_x', 'url_y',\n    'url', 'positionText_y', 'positionText_x', 'position_x', 'grand_prix',\n    'status', 'nationality_y', 'constructorRef', 'company', 'dob', 'nationality',\n    'result_driver_standing', 'resultId', 'raceId_y', 'timetaken_in_millisec',\n    'fastestLapTime', 'time_x', 'fastestLap', 'max_speed', 'date', 'time_y'\n]\nt1 = X['position']\nX.drop(columns=columns_to_drop, inplace=True)\n\nt2 = v['position']\nv.drop(columns=columns_to_drop, inplace=True)\n\nX['rank'] = X['rank'].replace('\\\\N', np.nan)\nX['rank'] = pd.to_numeric(X['rank'], errors='coerce')\n\n# Convert to nullable integer type\nX['rank'] = X['rank'].astype('Int64')\n\npipe = Pipeline(steps=[\n    ('imputer', IterativeImputer(max_iter=100, tol=0.001, random_state=42)),  # Impute missing values with mean\n    ('scaler', StandardScaler())  # Standardize features\n])\n\npreprocessor = ColumnTransformer(\n    transformers=[\n         ('pi',pipe, X.columns) # Apply transformation pipeline to features\n    ])\n","metadata":{"id":"IvtnpXGd87Zu","execution":{"iopub.status.busy":"2024-06-29T06:39:16.305848Z","iopub.execute_input":"2024-06-29T06:39:16.306210Z","iopub.status.idle":"2024-06-29T06:39:48.987751Z","shell.execute_reply.started":"2024-06-29T06:39:16.306179Z","shell.execute_reply":"2024-06-29T06:39:48.987014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a hyperparameter search space for BaggingCLassifier and XGBClassifier\nspace = {\n        'n_estimators' : hp.choice('n_estimators', [500, 550 ,600 ,650 ,700,750,800]),\n        'max_depth': hp.choice('max_depth', range(2, 13)),\n        'learning_rate': hp.uniform('learning_rate', 0.0005, 0.002),\n        'min_child_weight': hp.uniform('min_child_weight', 1, 18),\n        'gamma': hp.loguniform('gamma', low=np.log(0.001), high=np.log(5)),  # Gamma from 0 to 5 (log-uniform)\n        'reg_alpha': hp.loguniform('reg_alpha', low=np.log(0.0001), high=np.log(0.8)),  # Alpha from 0 to 0.8 (log-uniform)\n        'reg_lambda': hp.loguniform('reg_lambda', low=np.log(1), high=np.log(5)),\n        'colsample_bytree': hp.uniform('colsample_bytree', 0.6, 1.0),\n        'colsample_bynode': hp.uniform('colsample_bynode', 0.6, 1.0),\n        'colsample_bylevel': hp.uniform('colsample_bylevel', 0.6, 1.0)\n    }","metadata":{"id":"Rs9hq3jj8_fo"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pre = preprocessor.fit_transform(X)\nV_pre = preprocessor.transform(v)","metadata":{"id":"H6tlhmQu9AMR","execution":{"iopub.status.busy":"2024-06-29T06:40:27.527430Z","iopub.execute_input":"2024-06-29T06:40:27.528503Z","iopub.status.idle":"2024-06-29T06:41:04.748242Z","shell.execute_reply.started":"2024-06-29T06:40:27.528463Z","shell.execute_reply":"2024-06-29T06:41:04.747356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pre.shape","metadata":{"id":"OnWGlYej97qZ","outputId":"91cd16c7-088c-4023-8248-392c38e1ff34"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def objective(space):\n\n    # Flatten the target variable y\n    y_data  = t1.values.ravel()\n    # Define the BaggingClassifier with XGBClassifier as the base estimator\n    model = XGBRegressor(device = 'cuda' ,\n                        random_state=42, **space, objective='reg:squarederror',n_jobs = -1)\n\n    # Fit the model\n    model.fit(X_pre, y_data)\n    y2_pred = model.predict(V_pre)\n    rmse = np.sqrt(mean_squared_error(t2.values.ravel(),y2_pred))\n\n    # Return loss (negative mean accuracy) and optimization status\n    return {'loss': rmse, 'status': STATUS_OK}\n\n# Run hyperparameter optimization using Hyperopt\ntrials = Trials()\nbest_params = fmin(objective, space, rstate=np.random.default_rng(42), algo=tpe.suggest,\n                   max_evals=100, trials=trials)\n# Print best hyperparameters found\nprint(\"Best hyperparameters:\", best_params)\n","metadata":{"id":"w87Uyk0y9F49","outputId":"059c70eb-5db1-4567-fc6f-31b3ea4360d1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ID = p['result_driver_standing']\ncolumns_to_drop_test = [\n    'fp1_date','number','driverRef','driver_num','driver_code','forename','surname',\n    'fp1_time', 'fp2_date', 'fp2_time', 'fp3_date', 'fp3_time',\n    'quali_date', 'quali_time', 'sprint_date', 'sprint_time', 'url_x', 'url_y',\n    'url', 'positionText_x', 'position_x', 'grand_prix',\n    'status', 'nationality_y', 'constructorRef', 'company', 'dob', 'nationality',\n    'result_driver_standing', 'resultId', 'raceId_y', 'timetaken_in_millisec',\n    'fastestLapTime', 'time_x', 'fastestLap', 'max_speed', 'date', 'time_y'\n]\n\np.drop(columns = columns_to_drop_test, inplace = True)\nfinal = preprocessor.transform(p)","metadata":{"id":"yR9vtCIMD3M-","execution":{"iopub.status.busy":"2024-06-29T06:41:18.534433Z","iopub.execute_input":"2024-06-29T06:41:18.534792Z","iopub.status.idle":"2024-06-29T06:41:18.710149Z","shell.execute_reply.started":"2024-06-29T06:41:18.534763Z","shell.execute_reply":"2024-06-29T06:41:18.709158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_params = { 'colsample_bylevel': 0.7829573243380741, 'colsample_bynode': 0.8409512818863321,\n               'colsample_bytree': 0.9380717847017378, 'gamma': 1.6608611376082092, 'learning_rate': 0.0018821333068856992, 'max_depth': 11,\n                'min_child_weight': 17.888457276697828, 'n_estimators': 750, 'reg_alpha': 0.010806799409259934, 'reg_lambda': 4.552090852516414}\nmodel = XGBRegressor(device = 'cuda' ,\n                    random_state=42, **best_params, objective='reg:squarederror',n_jobs = -1)\n\n# Fit the model\nmodel.fit(X_pre, t1.values.ravel())\ntest_pred = model.predict(final)","metadata":{"id":"oAzNfhpXDcaM","execution":{"iopub.status.busy":"2024-06-29T06:41:23.375917Z","iopub.execute_input":"2024-06-29T06:41:23.376271Z","iopub.status.idle":"2024-06-29T06:42:22.189317Z","shell.execute_reply.started":"2024-06-29T06:41:23.376243Z","shell.execute_reply":"2024-06-29T06:42:22.188527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_df = pd.DataFrame({\n    'position' : test_pred,\n    'result_driver_standing': ID\n})\n\n# Save results to CSV file\npredictions_df.to_csv(\"/kaggle/working/Code Knights_output.csv\", index=False)","metadata":{"id":"WalldS1ZkAj4","execution":{"iopub.status.busy":"2024-06-29T06:42:37.370403Z","iopub.execute_input":"2024-06-29T06:42:37.370780Z","iopub.status.idle":"2024-06-29T06:42:38.160547Z","shell.execute_reply.started":"2024-06-29T06:42:37.370750Z","shell.execute_reply":"2024-06-29T06:42:38.159791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"VOQEK9n_FFBu"},"execution_count":null,"outputs":[]}]}