{"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":30786,"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-11-03T11:42:56.370453Z","iopub.execute_input":"2024-11-03T11:42:56.371573Z","iopub.status.idle":"2024-11-03T11:42:56.382973Z","shell.execute_reply.started":"2024-11-03T11:42:56.371507Z","shell.execute_reply":"2024-11-03T11:42:56.381546Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\nfrom sklearn.ensemble import HistGradientBoostingRegressor\nfrom sklearn.preprocessing import StandardScaler\nfrom datetime import datetime\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:42:56.385685Z","iopub.execute_input":"2024-11-03T11:42:56.386171Z","iopub.status.idle":"2024-11-03T11:42:56.393371Z","shell.execute_reply.started":"2024-11-03T11:42:56.386126Z","shell.execute_reply":"2024-11-03T11:42:56.392096Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/brist1d/train.csv')\ntest=pd.read_csv('/kaggle/input/brist1d/test.csv')\ntrain.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:42:56.395582Z","iopub.execute_input":"2024-11-03T11:42:56.396158Z","iopub.status.idle":"2024-11-03T11:43:04.743863Z","shell.execute_reply.started":"2024-11-03T11:42:56.396102Z","shell.execute_reply":"2024-11-03T11:43:04.742908Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:04.745915Z","iopub.execute_input":"2024-11-03T11:43:04.746289Z","iopub.status.idle":"2024-11-03T11:43:04.774320Z","shell.execute_reply.started":"2024-11-03T11:43:04.746251Z","shell.execute_reply":"2024-11-03T11:43:04.773212Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:04.775688Z","iopub.execute_input":"2024-11-03T11:43:04.776288Z","iopub.status.idle":"2024-11-03T11:43:04.785243Z","shell.execute_reply.started":"2024-11-03T11:43:04.776242Z","shell.execute_reply":"2024-11-03T11:43:04.784273Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:04.786406Z","iopub.execute_input":"2024-11-03T11:43:04.786770Z","iopub.status.idle":"2024-11-03T11:43:04.819665Z","shell.execute_reply.started":"2024-11-03T11:43:04.786733Z","shell.execute_reply":"2024-11-03T11:43:04.818471Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:04.821140Z","iopub.execute_input":"2024-11-03T11:43:04.821586Z","iopub.status.idle":"2024-11-03T11:43:08.715023Z","shell.execute_reply.started":"2024-11-03T11:43:04.821538Z","shell.execute_reply":"2024-11-03T11:43:08.714022Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:08.718125Z","iopub.execute_input":"2024-11-03T11:43:08.718559Z","iopub.status.idle":"2024-11-03T11:43:09.203858Z","shell.execute_reply.started":"2024-11-03T11:43:08.718519Z","shell.execute_reply":"2024-11-03T11:43:09.202625Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a mask of columns to drop in the training set\ntrain_missing = train.isnull().mean()*100\n\n# Identify columns with more than 20% missing data in train\ncolumns_to_drop = train_missing[train_missing > 25].index","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:09.205397Z","iopub.execute_input":"2024-11-03T11:43:09.205757Z","iopub.status.idle":"2024-11-03T11:43:09.723094Z","shell.execute_reply.started":"2024-11-03T11:43:09.205710Z","shell.execute_reply":"2024-11-03T11:43:09.722124Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"columns_to_drop.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:09.724291Z","iopub.execute_input":"2024-11-03T11:43:09.724639Z","iopub.status.idle":"2024-11-03T11:43:09.731654Z","shell.execute_reply.started":"2024-11-03T11:43:09.724603Z","shell.execute_reply":"2024-11-03T11:43:09.730555Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop the identified columns from both df_train and df_test\ntrain_cleaned = train.drop(columns=columns_to_drop)\ntest_cleaned = test.drop(columns=columns_to_drop)\n\n\ntrain_cleaned.shape,test_cleaned.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:09.733193Z","iopub.execute_input":"2024-11-03T11:43:09.734135Z","iopub.status.idle":"2024-11-03T11:43:09.801999Z","shell.execute_reply.started":"2024-11-03T11:43:09.734081Z","shell.execute_reply":"2024-11-03T11:43:09.800887Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check the result\nprint(f\"Columns dropped: {columns_to_drop}\")\nprint(\"Updated df_train:\",train_cleaned.shape)\nprint(\"Updated df_test:\",test_cleaned.shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:09.803331Z","iopub.execute_input":"2024-11-03T11:43:09.803691Z","iopub.status.idle":"2024-11-03T11:43:09.810299Z","shell.execute_reply.started":"2024-11-03T11:43:09.803651Z","shell.execute_reply":"2024-11-03T11:43:09.809021Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop the 'id' column from both datasets\ntrain_cleaned = train_cleaned.drop('id', axis=1)\ntest_cleaned = test_cleaned.drop('id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:09.811671Z","iopub.execute_input":"2024-11-03T11:43:09.812042Z","iopub.status.idle":"2024-11-03T11:43:09.899570Z","shell.execute_reply.started":"2024-11-03T11:43:09.812003Z","shell.execute_reply":"2024-11-03T11:43:09.898557Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert time columns to hours, minutes, and seconds\ndef convert_time_columns(train_cleaned):\n    for col in train_cleaned.columns:\n        if pd.api.types.is_datetime64_any_dtype(train_cleaned[col]) or 'time' in col.lower():\n            train_cleaned[col] = pd.to_datetime(train_cleaned[col], errors='coerce')  # Convert to datetime\n            train_cleaned[col+'_hour'] = train_cleaned[col].dt.hour\n            train_cleaned[col+'_minute'] = train_cleaned[col].dt.minute\n#             train_cleaned[col+'_second'] = train_cleaned[col].dt.second\n            train_cleaned.drop(col, axis=1, inplace=True)  # Drop original time column after conversion\n# Apply time conversion to both train and test datasets\nconvert_time_columns(train_cleaned)\nconvert_time_columns(test_cleaned)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:09.900893Z","iopub.execute_input":"2024-11-03T11:43:09.901345Z","iopub.status.idle":"2024-11-03T11:43:10.040449Z","shell.execute_reply.started":"2024-11-03T11:43:09.901297Z","shell.execute_reply":"2024-11-03T11:43:10.039344Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Handle 'p_num' in the test set: find unique values and replace them with numbers\nunique_pnums = test_cleaned['p_num'].unique()\npnum_mapping = {value: idx for idx, value in enumerate(unique_pnums)}\ntest_cleaned['p_num'] = test_cleaned['p_num'].map(pnum_mapping)\n\n# Also ensure the same mapping is applied to 'p_num' in the train set, if it exists\nif 'p_num' in train_cleaned.columns:\n    train_cleaned['p_num'] = train_cleaned['p_num'].map(pnum_mapping)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:10.041910Z","iopub.execute_input":"2024-11-03T11:43:10.042369Z","iopub.status.idle":"2024-11-03T11:43:10.062679Z","shell.execute_reply.started":"2024-11-03T11:43:10.042319Z","shell.execute_reply":"2024-11-03T11:43:10.061538Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_cleaned.shape)\nprint(test_cleaned.shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:10.064318Z","iopub.execute_input":"2024-11-03T11:43:10.065103Z","iopub.status.idle":"2024-11-03T11:43:10.072987Z","shell.execute_reply.started":"2024-11-03T11:43:10.065030Z","shell.execute_reply":"2024-11-03T11:43:10.071928Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\n# Use mean (you can also try mean or most_frequent based on your data)\nimputer = SimpleImputer(strategy='mean')\n\n# Fit on df_train and transform both df_train and df_test\ntrain_imputed = imputer.fit_transform(train_cleaned)\ntest_imputed = imputer.fit_transform(test_cleaned)\n\n# Convert the NumPy arrays back to DataFrames and retain the original column names\ntrain_cleaned = pd.DataFrame(train_imputed, columns=train_cleaned.columns)\ntest_cleaned = pd.DataFrame(test_imputed, columns=test_cleaned.columns)\n\n\ntrain_float_columns = train_cleaned.select_dtypes(include=['float64', 'float32']).columns.tolist()\ntest_float_columns = test_cleaned.select_dtypes(include=['float64', 'float32']).columns.tolist()\n\ntrain_cleaned[train_float_columns] = train_cleaned[train_float_columns].astype(float)\ntest_cleaned[test_float_columns] = test_cleaned[test_float_columns].astype(float)\n\nprint(train_cleaned.dtypes)\nprint(train_cleaned.columns)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:10.074401Z","iopub.execute_input":"2024-11-03T11:43:10.075088Z","iopub.status.idle":"2024-11-03T11:43:11.232360Z","shell.execute_reply.started":"2024-11-03T11:43:10.075015Z","shell.execute_reply":"2024-11-03T11:43:11.231125Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_cleaned.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:11.233596Z","iopub.execute_input":"2024-11-03T11:43:11.233941Z","iopub.status.idle":"2024-11-03T11:43:11.295739Z","shell.execute_reply.started":"2024-11-03T11:43:11.233905Z","shell.execute_reply":"2024-11-03T11:43:11.294638Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_cleaned.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:11.297099Z","iopub.execute_input":"2024-11-03T11:43:11.297444Z","iopub.status.idle":"2024-11-03T11:43:11.314909Z","shell.execute_reply.started":"2024-11-03T11:43:11.297408Z","shell.execute_reply":"2024-11-03T11:43:11.313828Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare Features and Target Variable for Training Data\n\nx_train = train_cleaned.drop('bg+1:00', axis=1)\ny_train = train_cleaned['bg+1:00']","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:11.318698Z","iopub.execute_input":"2024-11-03T11:43:11.319087Z","iopub.status.idle":"2024-11-03T11:43:11.427830Z","shell.execute_reply.started":"2024-11-03T11:43:11.319030Z","shell.execute_reply":"2024-11-03T11:43:11.426815Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_split, x_val, y_train_split, y_val = train_test_split(x_train, y_train, test_size = 0.2 , random_state = 42)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:51:28.448285Z","iopub.execute_input":"2024-11-03T11:51:28.448786Z","iopub.status.idle":"2024-11-03T11:51:28.933574Z","shell.execute_reply.started":"2024-11-03T11:51:28.448743Z","shell.execute_reply":"2024-11-03T11:51:28.932131Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.experimental import enable_hist_gradient_boosting  # noqa\nfrom sklearn.inspection import permutation_importance\n\n# Initialize and fit the HistGradientBoostingRegressor\nmodel = HistGradientBoostingRegressor()\nmodel.fit(x_train_split, y_train_split)\n\n# Compute permutation importance\nresult = permutation_importance(model, x_val, y_val, n_repeats=30, random_state=42)\n\n# Create a DataFrame to hold feature names and their importances\nfeature_importances = pd.DataFrame({\n    'Feature': x_train.columns,\n    'Importance': result.importances_mean\n})\n\n# Set a threshold for feature selection\nthreshold = 0.000409 # Adjust this value as needed\n\n# Select features based on the threshold\nselected_features = feature_importances[feature_importances['Importance'] > threshold]\n\n# Sort the selected features by importance\nselected_features = selected_features.sort_values(by='Importance', ascending=False)\n\n# Display the selected features and their importances\nprint(\"Selected Features from Permutation Importance:\")\nprint(selected_features)\nprint(feature_importances.describe())\n\n\n# Create a mask to filter the selected features from the training and test sets\nselected_indices = [x_train.columns.tolist().index(f) for f in selected_features['Feature']]\nx_train_split = x_train_split.iloc[:, selected_indices]\nx_val = x_val.iloc[:, selected_indices]\n\n# Standardize features\nscaler = StandardScaler()\nx_train_split = scaler.fit_transform(x_train_split)\nx_val = scaler.transform(x_val)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T12:01:29.702384Z","iopub.execute_input":"2024-11-03T12:01:29.703621Z","iopub.status.idle":"2024-11-03T12:17:38.215129Z","shell.execute_reply.started":"2024-11-03T12:01:29.703566Z","shell.execute_reply":"2024-11-03T12:17:38.212885Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the model with parameters\nmodel = HistGradientBoostingRegressor(\n    max_iter=310,               # Number of boosting iterations\n    learning_rate=0.05,         # Learning rate\n    max_leaf_nodes=50,          # Maximum number of leaf nodes\n    max_depth=6,                # Reasonable depth (can tune further based on performance)\n    min_samples_leaf=30,        # Minimum samples per leaf (helps prevent overfitting)\n    max_bins=128,               # Number of bins for feature binning\n    l2_regularization=0.9636088870146975,      # Regularization term (helps with overfitting)\n    scoring = \"neg_root_mean_squared_error\",  # Model Scoreing\n    random_state=42             # Randomness\n)\n\n\n# Train the model on the training data\nmodel.fit(x_train_split, y_train_split)\n\n\n# Predict on the test set\ny_pred = model.predict(x_val)\n\n# Evaluate the model\nmae = mean_absolute_error(y_val, y_pred)\nmse = mean_squared_error(y_val, y_pred)\n\n# Calculate R2 score and RMSE\nr2 = r2_score(y_val, y_pred)\nrmse = np.sqrt(mse)\n\nprint(f'Mean Absolute Error: {mae:.5f}')\nprint(f'Mean Squared Error: {mse:.5f}')\nprint(f\"R2 Score: {r2:.5f}\")\nprint(f\"RMSE: {rmse:.5f}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-29T06:31:17.570309Z","iopub.execute_input":"2024-11-29T06:31:17.570961Z","iopub.status.idle":"2024-11-29T06:31:17.605277Z","shell.execute_reply.started":"2024-11-29T06:31:17.570907Z","shell.execute_reply":"2024-11-29T06:31:17.603099Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Visualize Predictions\nplt.figure(figsize=(12, 6))\nplt.plot(y_val.values, label='Actual Glucose Levels')\nplt.plot(y_pred, label='Predicted Glucose Levels')\nplt.xlabel('Samples')\nplt.ylabel('Glucose Level')\nplt.title('Predicted vs Actual Glucose Levels')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:21.316181Z","iopub.status.idle":"2024-11-03T11:43:21.316640Z","shell.execute_reply.started":"2024-11-03T11:43:21.316421Z","shell.execute_reply":"2024-11-03T11:43:21.316443Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test = test_cleaned.drop('bg+1:00', axis=1, errors='ignore')\nx_test = x_test.iloc[:, selected_indices]\nx_test = scaler.transform(x_test)\ntest_predictions = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:21.318239Z","iopub.status.idle":"2024-11-03T11:43:21.318686Z","shell.execute_reply.started":"2024-11-03T11:43:21.318490Z","shell.execute_reply":"2024-11-03T11:43:21.318512Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save predictions to CSV\nsubmission = pd.DataFrame({'id': test['id'], 'bg+1:00': test_predictions})\nsubmission.to_csv(f'/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:21.319941Z","iopub.status.idle":"2024-11-03T11:43:21.320367Z","shell.execute_reply.started":"2024-11-03T11:43:21.320166Z","shell.execute_reply":"2024-11-03T11:43:21.320189Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Model training, evaluation, and test predictions completed.\")","metadata":{"execution":{"iopub.status.busy":"2024-11-03T11:43:21.321865Z","iopub.status.idle":"2024-11-03T11:43:21.322290Z","shell.execute_reply.started":"2024-11-03T11:43:21.322087Z","shell.execute_reply":"2024-11-03T11:43:21.322114Z"},"trusted":true},"outputs":[],"execution_count":null}]}