{"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":"none","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\ntest  = pd.read_csv('/kaggle/input/brist1d/test.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:14:22.904226Z","iopub.execute_input":"2024-11-20T16:14:22.904967Z","iopub.status.idle":"2024-11-20T16:14:26.019721Z","shell.execute_reply.started":"2024-11-20T16:14:22.904932Z","shell.execute_reply":"2024-11-20T16:14:26.018754Z"},"trusted":true},"outputs":[],"execution_count":null},{"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')\ntest = parse_time_column(test,'time')\n\n# Show the result\nprint(train)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:25:57.032713Z","iopub.execute_input":"2024-11-20T16:25:57.033013Z","iopub.status.idle":"2024-11-20T16:25:58.176095Z","shell.execute_reply.started":"2024-11-20T16:25:57.032985Z","shell.execute_reply":"2024-11-20T16:25:58.175024Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = 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\"\n    ]\n]\ntest=test [\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\", \"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\"\n    ]\n\n]\n# Check the resulting DataFrame\nprint(train.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T16:25:58.177778Z","iopub.execute_input":"2024-11-20T16:25:58.178565Z","iopub.status.idle":"2024-11-20T16:25:58.254433Z","shell.execute_reply.started":"2024-11-20T16:25:58.178522Z","shell.execute_reply":"2024-11-20T16:25:58.253594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X= train.drop(['bg+1:00'],axis=1)\ny= train.drop(X,axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:26:01.271191Z","iopub.execute_input":"2024-11-20T16:26:01.271873Z","iopub.status.idle":"2024-11-20T16:26:01.300952Z","shell.execute_reply.started":"2024-11-20T16:26:01.271838Z","shell.execute_reply":"2024-11-20T16:26:01.299995Z"},"trusted":true},"outputs":[],"execution_count":null},{"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\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)\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-20T16:26:12.181135Z","iopub.execute_input":"2024-11-20T16:26:12.181963Z","iopub.status.idle":"2024-11-20T16:26:12.274725Z","shell.execute_reply.started":"2024-11-20T16:26:12.181926Z","shell.execute_reply":"2024-11-20T16:26:12.273923Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":" ","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:00:50.867296Z","iopub.execute_input":"2024-11-20T16:00:50.867645Z","iopub.status.idle":"2024-11-20T16:00:50.913337Z","shell.execute_reply.started":"2024-11-20T16:00:50.867609Z","shell.execute_reply":"2024-11-20T16:00:50.912655Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:26:19.181768Z","iopub.execute_input":"2024-11-20T16:26:19.182108Z","iopub.status.idle":"2024-11-20T16:26:19.714873Z","shell.execute_reply.started":"2024-11-20T16:26:19.182080Z","shell.execute_reply":"2024-11-20T16:26:19.714122Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preprocessed = preprocessor.fit_transform(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T16:26:21.932772Z","iopub.execute_input":"2024-11-20T16:26:21.933118Z","iopub.status.idle":"2024-11-20T16:26:21.954912Z","shell.execute_reply.started":"2024-11-20T16:26:21.933089Z","shell.execute_reply":"2024-11-20T16:26:21.954219Z"}},"outputs":[],"execution_count":null},{"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-20T16:26:58.548305Z","iopub.execute_input":"2024-11-20T16:26:58.549208Z","iopub.status.idle":"2024-11-20T16:27:00.572243Z","shell.execute_reply.started":"2024-11-20T16:26:58.549172Z","shell.execute_reply":"2024-11-20T16:27:00.571542Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install optuna","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T16:27:05.894159Z","iopub.execute_input":"2024-11-20T16:27:05.894549Z","iopub.status.idle":"2024-11-20T16:27:14.102643Z","shell.execute_reply.started":"2024-11-20T16:27:05.894517Z","shell.execute_reply":"2024-11-20T16:27:14.101465Z"}},"outputs":[],"execution_count":null},{"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-20T16:27:19.823423Z","iopub.execute_input":"2024-11-20T16:27:19.824164Z","iopub.status.idle":"2024-11-20T16:39:35.937302Z","shell.execute_reply.started":"2024-11-20T16:27:19.824132Z","shell.execute_reply":"2024-11-20T16:39:35.936421Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bg = model.predict(test_preprocessed)\nprint(len(bg))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T16:43:22.782589Z","iopub.execute_input":"2024-11-20T16:43:22.782924Z","iopub.status.idle":"2024-11-20T16:43:22.850743Z","shell.execute_reply.started":"2024-11-20T16:43:22.782899Z","shell.execute_reply":"2024-11-20T16:43:22.849849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.DataFrame()\nt = pd.read_csv('/kaggle/input/brist1d/test.csv')\ndf['id'] = t['id']\ndf['bg+1:00'] = bg\ndf.to_csv('submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T16:43:30.350776Z","iopub.execute_input":"2024-11-20T16:43:30.351601Z","iopub.status.idle":"2024-11-20T16:43:30.503649Z","shell.execute_reply.started":"2024-11-20T16:43:30.351552Z","shell.execute_reply":"2024-11-20T16:43:30.502638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T17:18:09.595875Z","iopub.execute_input":"2024-11-20T17:18:09.596222Z","iopub.status.idle":"2024-11-20T17:18:09.607876Z","shell.execute_reply.started":"2024-11-20T17:18:09.596193Z","shell.execute_reply":"2024-11-20T17:18:09.606726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T17:22:09.651931Z","iopub.execute_input":"2024-11-20T17:22:09.652280Z","iopub.status.idle":"2024-11-20T17:22:17.890730Z","shell.execute_reply.started":"2024-11-20T17:22:09.652249Z","shell.execute_reply":"2024-11-20T17:22:17.889543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T17:23:44.539465Z","iopub.execute_input":"2024-11-20T17:23:44.540193Z","iopub.status.idle":"2024-11-20T17:23:45.598651Z","shell.execute_reply.started":"2024-11-20T17:23:44.540161Z","shell.execute_reply":"2024-11-20T17:23:45.597729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T17:24:39.722346Z","iopub.execute_input":"2024-11-20T17:24:39.722867Z","iopub.status.idle":"2024-11-20T17:24:40.049939Z","shell.execute_reply.started":"2024-11-20T17:24:39.722831Z","shell.execute_reply":"2024-11-20T17:24:40.049024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}