{"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":30775,"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\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-08T09:47:37.961449Z","iopub.execute_input":"2024-11-08T09:47:37.962001Z","iopub.status.idle":"2024-11-08T09:47:37.968938Z","shell.execute_reply.started":"2024-11-08T09:47:37.961955Z","shell.execute_reply":"2024-11-08T09:47:37.967099Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Importing the training data\ntrain= pd.read_csv('/kaggle/input/brist1d/train.csv')\ntest= pd.read_csv('/kaggle/input/brist1d/test.csv')\ntrain.head()\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:38.061688Z","iopub.execute_input":"2024-11-08T09:47:38.063032Z","iopub.status.idle":"2024-11-08T09:47:47.859888Z","shell.execute_reply.started":"2024-11-08T09:47:38.062958Z","shell.execute_reply":"2024-11-08T09:47:47.858673Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:47.862117Z","iopub.execute_input":"2024-11-08T09:47:47.862469Z","iopub.status.idle":"2024-11-08T09:47:47.896834Z","shell.execute_reply.started":"2024-11-08T09:47:47.862431Z","shell.execute_reply":"2024-11-08T09:47:47.895607Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:47.898477Z","iopub.execute_input":"2024-11-08T09:47:47.899034Z","iopub.status.idle":"2024-11-08T09:47:52.136541Z","shell.execute_reply.started":"2024-11-08T09:47:47.898994Z","shell.execute_reply":"2024-11-08T09:47:52.135377Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()\n# Let us see what columns have missing values\n# Total number of entries (rows X columns) in the dataset\ntotal= train.size\n#Number of missing values per column\nmissingCount = train.isnull().sum()\n#Total number of missing values\nmissing_tot = missingCount.sum()\n# Calculate percentage of missing values\nprint(\"The dataset contains\", round(((missing_tot/total) * 100), 2), \"%\", \"missing values\")\n\n# keeping only the columns with missing values>0 \nmissing = missingCount[missingCount > 0] \nprint(missing)","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:52.138219Z","iopub.execute_input":"2024-11-08T09:47:52.138554Z","iopub.status.idle":"2024-11-08T09:47:53.148019Z","shell.execute_reply.started":"2024-11-08T09:47:52.138519Z","shell.execute_reply":"2024-11-08T09:47:53.146700Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_na=train.isnull().sum()\npercentage = train.isnull().mean() * 100\nmissing = pd.DataFrame({'Missing Values(train)':train_na, 'Percentage':percentage})\nmissing\n\n# Filter only the columns with more than 80% missing values\nmissing_above_80 = missing[missing['Percentage'] > 80]\nmissing_above_80.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:53.151683Z","iopub.execute_input":"2024-11-08T09:47:53.152178Z","iopub.status.idle":"2024-11-08T09:47:54.175786Z","shell.execute_reply.started":"2024-11-08T09:47:53.152136Z","shell.execute_reply":"2024-11-08T09:47:54.174482Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract numerical features\nnum = train.select_dtypes(include=np.number).columns.tolist()\nnum","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:54.177198Z","iopub.execute_input":"2024-11-08T09:47:54.177527Z","iopub.status.idle":"2024-11-08T09:47:54.433132Z","shell.execute_reply.started":"2024-11-08T09:47:54.177493Z","shell.execute_reply":"2024-11-08T09:47:54.431853Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Histogram of blood glucose in mmol/L\nplt.hist(train['bg+1:00'])\nplt.title('Distribution of blood glucose an hour in the future ')\nplt.xlabel('Blood glucose in mmol/L')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:54.435153Z","iopub.execute_input":"2024-11-08T09:47:54.435523Z","iopub.status.idle":"2024-11-08T09:47:54.717401Z","shell.execute_reply.started":"2024-11-08T09:47:54.435483Z","shell.execute_reply":"2024-11-08T09:47:54.716123Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_var = train[['bg-5:55','bg-5:50','bg-2:30','bg-0:15','bg-0:00',\n 'insulin-5:55','insulin-5:50', 'insulin-2:30', 'insulin-0:00', 'insulin-0:15', 'insulin-0:00', 'cals-5:50', \n                 'cals-0:00','steps-5:55' ,'steps-0:05','steps-0:00','hr-5:55','hr-0:05',\n 'hr-0:00','carbs-5:55','carbs-0:05',\n 'carbs-0:00',\n 'bg+1:00']]","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:54.718537Z","iopub.execute_input":"2024-11-08T09:47:54.718913Z","iopub.status.idle":"2024-11-08T09:47:54.731540Z","shell.execute_reply.started":"2024-11-08T09:47:54.718875Z","shell.execute_reply":"2024-11-08T09:47:54.730191Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Find the pearson correlations matrix\ncorr = num_var.corr(method = 'pearson')\ncorr\n\nplt.figure(figsize=(15, 10))\n\n# Using Seaborn to create a heatmap\nsns.heatmap(corr, annot=True, fmt='.2f', cmap='Pastel2', linewidths=2)\n\nplt.title('Correlation Heatmap')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:54.733327Z","iopub.execute_input":"2024-11-08T09:47:54.733843Z","iopub.status.idle":"2024-11-08T09:47:56.671312Z","shell.execute_reply.started":"2024-11-08T09:47:54.733801Z","shell.execute_reply":"2024-11-08T09:47:56.670074Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.drop(train.loc[:,'carbs-5:55' : 'activity-0:00'].columns, axis=1)\ntrain ","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:56.673199Z","iopub.execute_input":"2024-11-08T09:47:56.673673Z","iopub.status.idle":"2024-11-08T09:47:57.189982Z","shell.execute_reply.started":"2024-11-08T09:47:56.673626Z","shell.execute_reply":"2024-11-08T09:47:57.188647Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert 'time' column to datetime and extract features\ntrain['time'] = pd.to_datetime(train['time'], format='%H:%M:%S')\ntest['time'] = pd.to_datetime(test['time'], format='%H:%M:%S')\n\n# Extract hour and minute as numerical features\ntrain['hour'] = train['time'].dt.hour\ntrain['minute'] = train['time'].dt.minute\ntest['hour'] = test['time'].dt.hour\ntest['minute'] = test['time'].dt.minute\n\n# Drop 'time' column\ntrain.drop('time', axis=1, inplace=True)\ntest.drop('time', axis=1, inplace=True)\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:57.191307Z","iopub.execute_input":"2024-11-08T09:47:57.191697Z","iopub.status.idle":"2024-11-08T09:47:57.407178Z","shell.execute_reply.started":"2024-11-08T09:47:57.191657Z","shell.execute_reply":"2024-11-08T09:47:57.405943Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.drop(train.loc[:,'id' : 'p_num'].columns, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:57.408773Z","iopub.execute_input":"2024-11-08T09:47:57.409159Z","iopub.status.idle":"2024-11-08T09:47:57.491440Z","shell.execute_reply.started":"2024-11-08T09:47:57.409120Z","shell.execute_reply":"2024-11-08T09:47:57.490230Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train= train.rename(columns={'bg+1:00': 'bg+1hour'}, inplace=False)\n\nimport re\n\n# Function to clean feature names\ndef clean_column_names(train):\n    train.columns = [re.sub(r'[^\\w\\s]', '_', col) for col in train.columns]  # Replace special characters with underscores\n    return train\ntrain = clean_column_names (train)\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:57.492979Z","iopub.execute_input":"2024-11-08T09:47:57.493332Z","iopub.status.idle":"2024-11-08T09:47:57.635973Z","shell.execute_reply.started":"2024-11-08T09:47:57.493296Z","shell.execute_reply":"2024-11-08T09:47:57.634803Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Extract feature and target arrays\nX= train.drop('bg_1hour', axis=1)\ny= train[['bg_1hour']]\n# Extract text features\ncats = X.select_dtypes(exclude=np.number).columns.tolist()\n\n# Convert to pd.Categorical\nfor col in cats:\n   X[col] = X[col].astype('category')","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:57.639844Z","iopub.execute_input":"2024-11-08T09:47:57.640258Z","iopub.status.idle":"2024-11-08T09:47:57.714272Z","shell.execute_reply.started":"2024-11-08T09:47:57.640218Z","shell.execute_reply":"2024-11-08T09:47:57.713153Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:57.715773Z","iopub.execute_input":"2024-11-08T09:47:57.716207Z","iopub.status.idle":"2024-11-08T09:47:57.890164Z","shell.execute_reply.started":"2024-11-08T09:47:57.716158Z","shell.execute_reply":"2024-11-08T09:47:57.888905Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Training with LightGBM model \nimport lightgbm as lgbm\nparams = { 'objective': 'regression',  # not needed\n    'boosting_type': 'gbdt',  # default\n    'num_leaves': 31,  # default\n    'learning_rate': 0.05,  # default = 0.10\n    'feature_fraction': 1.0,  # default\n    'min_data_in_leaf': 2,  # default = 20\n    'random_state': 0,\n    'verbosity': -1}\n\nmodel = lgbm.LGBMRegressor(**params)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:47:57.891688Z","iopub.execute_input":"2024-11-08T09:47:57.892073Z","iopub.status.idle":"2024-11-08T09:48:29.944526Z","shell.execute_reply.started":"2024-11-08T09:47:57.892034Z","shell.execute_reply":"2024-11-08T09:48:29.943343Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Predictions\ny_pred = model.predict(X_test)\nfrom sklearn.metrics import mean_squared_error\n# printing the mean squared error between real value and predicted value\nprint(mean_squared_error(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:48:29.946144Z","iopub.execute_input":"2024-11-08T09:48:29.946563Z","iopub.status.idle":"2024-11-08T09:48:30.306243Z","shell.execute_reply.started":"2024-11-08T09:48:29.946518Z","shell.execute_reply":"2024-11-08T09:48:30.304943Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:48:30.307884Z","iopub.execute_input":"2024-11-08T09:48:30.308346Z","iopub.status.idle":"2024-11-08T09:48:30.344776Z","shell.execute_reply.started":"2024-11-08T09:48:30.308293Z","shell.execute_reply":"2024-11-08T09:48:30.343535Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntest = test.drop(test.loc[:,'id' : 'p_num'].columns, axis=1)\ntest = test.drop(test.loc[:,'carbs-5:55' : 'activity-0:00'].columns, axis=1)\n\n# Function to clean feature names\ndef clean_column_names(test):\n    test.columns = [re.sub(r'[^\\w\\s]', '_', col) for col in test.columns]  # Replace special characters with underscores\n    return test\ntest = clean_column_names (test)\n# Extract text features\ncats = test.select_dtypes(exclude=np.number).columns.tolist()\n\n# Convert to pd.Categorical\nfor col in cats:\n   test[col] = test[col].astype('category')\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:48:30.346025Z","iopub.execute_input":"2024-11-08T09:48:30.346349Z","iopub.status.idle":"2024-11-08T09:48:30.370470Z","shell.execute_reply.started":"2024-11-08T09:48:30.346315Z","shell.execute_reply":"2024-11-08T09:48:30.369289Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_LightGBM=model.predict(test)\npred_LightGBM","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:48:30.372319Z","iopub.execute_input":"2024-11-08T09:48:30.373248Z","iopub.status.idle":"2024-11-08T09:48:30.419236Z","shell.execute_reply.started":"2024-11-08T09:48:30.373191Z","shell.execute_reply":"2024-11-08T09:48:30.417925Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/brist1d/sample_submission.csv')\nsub['bg+1:00'] = pred_LightGBM\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:48:30.420640Z","iopub.execute_input":"2024-11-08T09:48:30.421103Z","iopub.status.idle":"2024-11-08T09:48:30.443336Z","shell.execute_reply.started":"2024-11-08T09:48:30.421054Z","shell.execute_reply":"2024-11-08T09:48:30.442060Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-11-08T09:48:30.444877Z","iopub.execute_input":"2024-11-08T09:48:30.445341Z","iopub.status.idle":"2024-11-08T09:48:30.458117Z","shell.execute_reply.started":"2024-11-08T09:48:30.445290Z","shell.execute_reply":"2024-11-08T09:48:30.456956Z"},"trusted":true},"outputs":[],"execution_count":null}]}