{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\ntrain=pd.read_csv(\"/kaggle/input/brist1d/train.csv\")\ntrain.sample(n=10)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-20T09:36:11.541766Z","iopub.execute_input":"2024-11-20T09:36:11.542585Z","iopub.status.idle":"2024-11-20T09:36:19.352723Z","shell.execute_reply.started":"2024-11-20T09:36:11.542553Z","shell.execute_reply":"2024-11-20T09:36:19.351805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:17:43.447068Z","iopub.execute_input":"2024-11-20T09:17:43.447427Z","iopub.status.idle":"2024-11-20T09:17:43.453527Z","shell.execute_reply.started":"2024-11-20T09:17:43.447391Z","shell.execute_reply":"2024-11-20T09:17:43.452491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:17:43.455205Z","iopub.execute_input":"2024-11-20T09:17:43.455808Z","iopub.status.idle":"2024-11-20T09:17:43.485722Z","shell.execute_reply.started":"2024-11-20T09:17:43.455775Z","shell.execute_reply":"2024-11-20T09:17:43.484719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:17:43.488482Z","iopub.execute_input":"2024-11-20T09:17:43.488908Z","iopub.status.idle":"2024-11-20T09:17:47.167119Z","shell.execute_reply.started":"2024-11-20T09:17:43.488862Z","shell.execute_reply":"2024-11-20T09:17:47.166119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndef parse_time_column(df, column_name, new_column_name=\"time_column\"):\n   \n    # Remove the word 'time' and strip extra spaces from the original column\n    df[column_name] = df[column_name].str.replace('time', '', regex=False).str.strip()\n\n    # Parse the cleaned-up time strings into datetime objects (only time)\n    df[new_column_name] = pd.to_datetime(df[column_name], format='%H:%M:%S', errors='coerce').dt.strftime('%H:%M:%S')\n    \n    # Drop the original time column\n    df.drop(columns=[column_name], inplace=True)\n\n    return df\n\n# Call the function\ntrain = parse_time_column(train, 'time')\n\n# Show the result\nprint(train)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:36:30.578927Z","iopub.execute_input":"2024-11-20T09:36:30.579241Z","iopub.status.idle":"2024-11-20T09:36:31.731639Z","shell.execute_reply.started":"2024-11-20T09:36:30.579215Z","shell.execute_reply":"2024-11-20T09:36:31.730763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:17:48.462458Z","iopub.execute_input":"2024-11-20T09:17:48.462806Z","iopub.status.idle":"2024-11-20T09:17:48.485836Z","shell.execute_reply.started":"2024-11-20T09:17:48.462770Z","shell.execute_reply":"2024-11-20T09:17:48.484598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train[\n    [\n        \"bg-2:25\", \"bg-2:20\", \"bg-2:15\", \"bg-2:10\", \"bg-2:05\", \"bg-2:00\", \"bg-1:55\",\n        \"bg-1:50\", \"bg-1:45\", \"bg-1:40\", \"bg-1:35\", \"bg-1:30\", \"bg-1:25\", \"bg-1:20\",\n        \"bg-1:15\", \"bg-1:10\", \"bg-1:05\", \"bg-1:00\", \"bg-0:55\", \"bg-0:50\", \"bg-0:45\",\n        \"bg-0:40\", \"bg-0:35\", \"bg-0:30\", \"bg-0:25\", \"bg-0:20\", \"bg-0:15\", \"bg-0:10\",\n        \"bg-0:05\", \"bg-0:00\", \"bg+1:00\", \"insulin-5:55\", \"insulin-5:50\", \"insulin-5:45\",\n        \"insulin-5:40\", \"insulin-5:35\", \"insulin-5:30\", \"insulin-5:25\", \"insulin-5:20\",\n        \"insulin-5:15\", \"insulin-5:10\", \"time_column\",\"p_num\"\n    ]\n]\n\n# Check the resulting DataFrame\nprint(train_data.head())\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:37:02.650760Z","iopub.execute_input":"2024-11-20T09:37:02.651609Z","iopub.status.idle":"2024-11-20T09:37:02.686435Z","shell.execute_reply.started":"2024-11-20T09:37:02.651576Z","shell.execute_reply":"2024-11-20T09:37:02.685523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data.drop(columns=[\"bg+1:00\"])  # Features\ny = train_data[\"bg+1:00\"] ","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:37:09.129970Z","iopub.execute_input":"2024-11-20T09:37:09.130817Z","iopub.status.idle":"2024-11-20T09:37:09.155261Z","shell.execute_reply.started":"2024-11-20T09:37:09.130783Z","shell.execute_reply":"2024-11-20T09:37:09.154536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import  SimpleImputer,KNNImputer\nfrom sklearn.compose import ColumnTransformer # This is used to apply transformers to specific columns\nfrom sklearn.pipeline import Pipeline # This is used to chain transformers and estimators together\nfrom sklearn.preprocessing import OrdinalEncoder \nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\n\n\n\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=42) # Adjust test_size and random_state as needed\n\n# Identify  numerical features\nnumerical_features = X_train.select_dtypes(exclude=['object']).columns\ncategorical_features = X_train.select_dtypes(include=['object']).columns\n\n# Optimized categorical transformer (OrdinalEncoder to reduce memory usage)\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),  # Impute missing categorical values\n    ('label', OrdinalEncoder ())  \n])\n# Numerical transformer pipeline: Include scaling\nnumerical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='mean',add_indicator=True)),  # Impute missing numerical values\n    ('scaler', StandardScaler())  # Scale numerical features\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:45:10.344851Z","iopub.execute_input":"2024-11-20T09:45:10.345497Z","iopub.status.idle":"2024-11-20T09:45:10.459275Z","shell.execute_reply.started":"2024-11-20T09:45:10.345461Z","shell.execute_reply":"2024-11-20T09:45:10.458290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocessor = ColumnTransformer(\n    transformers=[\n        ('num', numerical_transformer, numerical_features), \n        ('cat', categorical_transformer, categorical_features)  # Categorical preprocessing\n    ])\n\n# Preprocess training and testing data\nX_train_preprocessed = preprocessor.fit_transform(X_train)\nX_test_preprocessed = preprocessor.transform(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:45:14.164356Z","iopub.execute_input":"2024-11-20T09:45:14.164751Z","iopub.status.idle":"2024-11-20T09:45:14.777839Z","shell.execute_reply.started":"2024-11-20T09:45:14.164716Z","shell.execute_reply":"2024-11-20T09:45:14.776707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import  cross_val_score\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\nmodel = XGBRegressor()\n\n# Fit the model\nmodel.fit(X_train_preprocessed, y_train)\n\n# Predict and evaluate\ny_pred = model.predict(X_test_preprocessed)\n# Calculate performance metrics\nmae = mean_absolute_error(y_test, y_pred)\nmse = mean_squared_error(y_test, y_pred)\nrmse = mean_squared_error(y_test, y_pred, squared=False)  # RMSE is the square root of MSE\nr2 = r2_score(y_test, y_pred)\n\n# Print metrics\nprint(f\"Mean Absolute Error (MAE): {mae:.2f}\")\nprint(f\"Mean Squared Error (MSE): {mse:.2f}\")\nprint(f\"Root Mean Squared Error (RMSE): {rmse:.2f}\")\nprint(f\"R² Score: {r2:.2f}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:45:17.884098Z","iopub.execute_input":"2024-11-20T09:45:17.884980Z","iopub.status.idle":"2024-11-20T09:45:20.103546Z","shell.execute_reply.started":"2024-11-20T09:45:17.884944Z","shell.execute_reply":"2024-11-20T09:45:20.101806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install optuna","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom sklearn.model_selection import  cross_val_score\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\nimport optuna\n\n\n#  Define Optuna optimization function\ndef objective(trial):\n    # Hyperparameters to tune\n    params = {\n        'max_depth': trial.suggest_int('max_depth', 3, 10),\n        'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3),\n        'n_estimators': trial.suggest_int('n_estimators', 100, 1000),\n        'subsample': trial.suggest_float('subsample', 0.5, 1.0),\n        'colsample_bytree': trial.suggest_float('colsample_bytree', 0.5, 1.0),\n        'reg_alpha': trial.suggest_float('reg_alpha', 0, 1.0),  # L1 regularization\n        'reg_lambda': trial.suggest_float('reg_lambda', 0, 1.0),  # L2 regularization\n    }\n    \n    # Train the model\n    model = XGBRegressor(**params, random_state=42)\n    model.fit(X_train_preprocessed, y_train)\n    \n    # Cross-validation for robustness\n    scores = cross_val_score(model, X_train_preprocessed, y_train, cv=3, scoring='neg_mean_squared_error')\n    return -scores.mean()  # Optuna minimizes, so return negative MSE\n\n# Step 4: Optimize with Optuna\nstudy = optuna.create_study(direction=\"minimize\")\nstudy.optimize(objective, n_trials=10)\n\nprint(\"Best hyperparameters:\", study.best_params)\n\n# Step 5: Train the model with best hyperparameters\nbest_params = study.best_params\nmodel = XGBRegressor(**best_params, random_state=42)\nmodel.fit(X_train_preprocessed, y_train)\n\n# Step 6: Make predictions\npredictions = model.predict(X_test_preprocessed)\n\n# Step 7: Round predictions to integers\ninteger_predictions = [int(round(pred)) for pred in predictions]\n\n# Step 8: Evaluate the model\nmse = mean_squared_error(y_test, integer_predictions)\nmae = mean_absolute_error(y_test, integer_predictions)\nr2 = r2_score(y_test, integer_predictions)\n\nprint(\"\\nEvaluation Metrics:\")\nprint(f\"Mean Squared Error (rounded predictions): {mse:.4f}\")\nprint(f\"Mean Absolute Error (rounded predictions): {mae:.4f}\")\nprint(f\"R2 Score (rounded predictions): {r2:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T09:46:10.325115Z","iopub.execute_input":"2024-11-20T09:46:10.326066Z","iopub.status.idle":"2024-11-20T09:52:04.578242Z","shell.execute_reply.started":"2024-11-20T09:46:10.326031Z","shell.execute_reply":"2024-11-20T09:52:04.577349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rmse = np.sqrt(np.mean((y_test - integer_predictions) ** 2))\n\nprint(f\"RMSE: {rmse}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-20T10:10:13.636834Z","iopub.execute_input":"2024-11-20T10:10:13.637224Z","iopub.status.idle":"2024-11-20T10:10:13.645486Z","shell.execute_reply.started":"2024-11-20T10:10:13.637195Z","shell.execute_reply":"2024-11-20T10:10:13.644326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_target = y.mean()\naccuracy = (1 - (rmse / mean_target)) * 100\nprint(f\"Accuracy: {accuracy:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2024-11-20T10:10:53.543231Z","iopub.execute_input":"2024-11-20T10:10:53.543860Z","iopub.status.idle":"2024-11-20T10:10:53.549326Z","shell.execute_reply.started":"2024-11-20T10:10:53.543824Z","shell.execute_reply":"2024-11-20T10:10:53.548425Z"},"trusted":true},"execution_count":null,"outputs":[]}]}