{"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":"import numpy as np\nimport pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:11:22.834818Z","iopub.execute_input":"2024-11-29T00:11:22.835244Z","iopub.status.idle":"2024-11-29T00:11:24.138037Z","shell.execute_reply.started":"2024-11-29T00:11:22.835189Z","shell.execute_reply":"2024-11-29T00:11:24.136807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train=pd.read_csv('/kaggle/input/brist1d/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:12:27.686569Z","iopub.execute_input":"2024-11-29T00:12:27.687247Z","iopub.status.idle":"2024-11-29T00:12:40.587430Z","shell.execute_reply.started":"2024-11-29T00:12:27.687205Z","shell.execute_reply":"2024-11-29T00:12:40.586224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T06:29:03.759391Z","iopub.execute_input":"2024-11-28T06:29:03.759790Z","iopub.status.idle":"2024-11-28T06:29:03.768299Z","shell.execute_reply.started":"2024-11-28T06:29:03.759755Z","shell.execute_reply":"2024-11-28T06:29:03.767234Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Encoding and New Feature","metadata":{}},{"cell_type":"code","source":"df_activities=pd.DataFrame({'Activities':['Indoor climbing', 'Run', 'Strength training',\n                                      'Swim', 'Bike', 'Dancing', 'Stairclimber',\n                                      'Spinning', 'Walking', 'HIIT', 'Outdoor Bike',\n                                      'Walk', 'Aerobic Workout', 'Tennis', 'Workout',\n                                      'Hike', 'Zumba', 'Sport', 'Yoga',\n                                      'Swimming', 'Weights', 'Running','None']})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:12:48.395036Z","iopub.execute_input":"2024-11-29T00:12:48.395443Z","iopub.status.idle":"2024-11-29T00:12:48.401700Z","shell.execute_reply.started":"2024-11-29T00:12:48.395403Z","shell.execute_reply":"2024-11-29T00:12:48.400500Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_activities.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:12:50.552376Z","iopub.execute_input":"2024-11-29T00:12:50.552836Z","iopub.status.idle":"2024-11-29T00:12:50.572337Z","shell.execute_reply.started":"2024-11-29T00:12:50.552795Z","shell.execute_reply":"2024-11-29T00:12:50.571076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nact_encoder=LabelEncoder()\nact_encoder.fit(df_activities['Activities'])\n#get all activity columns\nact_cols = [col for col in data_train.columns if 'activity' in col]\n#\ndata_train[act_cols] = data_train[act_cols].fillna(\"None\")\nfor col in act_cols:\n    data_train[col] = act_encoder.fit_transform(data_train[col].values.ravel())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:12:53.240733Z","iopub.execute_input":"2024-11-29T00:12:53.241175Z","iopub.status.idle":"2024-11-29T00:12:57.005651Z","shell.execute_reply.started":"2024-11-29T00:12:53.241128Z","shell.execute_reply":"2024-11-29T00:12:57.004308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#cylindric time\ndef add_feature_cylindric_time(data):\n    data['time'] = pd.to_datetime(data['time'], format='%H:%M:%S')\n    data['hour'] = data['time'].dt.hour\n    data['hour sin'] = np.sin(2 * np.pi * data['hour'] / 24)\n    data['hour cos'] = np.cos(2 * np.pi * data['hour'] / 24)\n    data.drop('hour', axis=1, inplace=True)\n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:12:57.431013Z","iopub.execute_input":"2024-11-29T00:12:57.431532Z","iopub.status.idle":"2024-11-29T00:12:57.439058Z","shell.execute_reply.started":"2024-11-29T00:12:57.431495Z","shell.execute_reply":"2024-11-29T00:12:57.437597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train = add_feature_cylindric_time(data_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:12:59.624236Z","iopub.execute_input":"2024-11-29T00:12:59.624588Z","iopub.status.idle":"2024-11-29T00:12:59.978850Z","shell.execute_reply.started":"2024-11-29T00:12:59.624556Z","shell.execute_reply":"2024-11-29T00:12:59.977622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:13:02.768774Z","iopub.execute_input":"2024-11-29T00:13:02.769330Z","iopub.status.idle":"2024-11-29T00:13:02.796582Z","shell.execute_reply.started":"2024-11-29T00:13:02.769285Z","shell.execute_reply":"2024-11-29T00:13:02.795346Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ny = data_train['bg+1:00']\nX = data_train.drop(columns=['id','p_num','time', 'bg+1:00'], axis=1)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:13:09.055761Z","iopub.execute_input":"2024-11-29T00:13:09.056156Z","iopub.status.idle":"2024-11-29T00:13:10.282413Z","shell.execute_reply.started":"2024-11-29T00:13:09.056121Z","shell.execute_reply":"2024-11-29T00:13:10.281299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import optuna\nfrom sklearn.model_selection import KFold\nfrom catboost import CatBoostRegressor\nfrom sklearn.metrics import mean_squared_error","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:13:16.368822Z","iopub.execute_input":"2024-11-29T00:13:16.369239Z","iopub.status.idle":"2024-11-29T00:13:17.039699Z","shell.execute_reply.started":"2024-11-29T00:13:16.369200Z","shell.execute_reply":"2024-11-29T00:13:17.038606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T08:24:32.276025Z","iopub.execute_input":"2024-11-28T08:24:32.276451Z","iopub.status.idle":"2024-11-28T08:24:32.338081Z","shell.execute_reply.started":"2024-11-28T08:24:32.276410Z","shell.execute_reply":"2024-11-28T08:24:32.336875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import xgboost as xgb\n# def objective(trial):\n#     param = {\n#         'objective': 'reg:squarederror',\n#         'booster': 'gbtree',\n#         'n_estimators': trial.suggest_int('n_estimators', 100, 500),\n#         'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3),\n#         'max_depth': trial.suggest_int('max_depth', 3, 12),\n#         'min_child_weight': trial.suggest_int('min_child_weight', 1, 10),\n#         'subsample': trial.suggest_float('subsample', 0.6, 1.0),\n#         'colsample_bytree': trial.suggest_float('colsample_bytree', 0.6, 1.0),\n#         'gamma': trial.suggest_float('gamma', 0, 0.5),\n#         'reg_lambda': trial.suggest_float('reg_lambda', 0.01, 1.0),\n#         'reg_alpha': trial.suggest_float('reg_alpha', 0.01, 1.0),\n#         'tree_method': 'auto',\n#         'random_state': 42\n#     }\n#     model = xgb.XGBRegressor(**param)\n#     model.fit(X_train, y_train)\n#     y_pred = model.predict(X_test)\n#     mse = mean_squared_error(y_test, y_pred)\n#     return mse\n\n# study = optuna.create_study(direction='minimize')\n# study.optimize(objective, n_trials=50)\n\n# print(\"Best hyperparameters: \", study.best_params)\n# print(\"Best MSE: \", study.best_value)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T08:46:05.968921Z","iopub.execute_input":"2024-11-28T08:46:05.970181Z","iopub.status.idle":"2024-11-28T12:40:16.147226Z","shell.execute_reply.started":"2024-11-28T08:46:05.970115Z","shell.execute_reply":"2024-11-28T12:40:16.144684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nparam={'n_estimators': 438, 'learning_rate': 0.08494023518101854, 'max_depth': 12, 'min_child_weight': 8, 'subsample': 0.9187340170043836, 'colsample_bytree': 0.910333866185326, 'gamma': 0.4001057195214107, 'reg_lambda': 0.31232950155604705, 'reg_alpha': 0.6008813684177805}\n#Best MSE:  2.0807342120390326\nmodel = xgb.XGBRegressor(\n    objective = 'reg:squarederror',\n    booster = 'gbtree',\n    random_state = 42,\n    **param\n)\n\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:17:04.533526Z","iopub.execute_input":"2024-11-29T00:17:04.533925Z","iopub.status.idle":"2024-11-29T00:24:49.487381Z","shell.execute_reply.started":"2024-11-29T00:17:04.533890Z","shell.execute_reply":"2024-11-29T00:24:49.485974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)\nmse = mean_squared_error(y_test, y_pred)\nprint(f\"Mean Squared Error: {mse}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:26:14.030726Z","iopub.execute_input":"2024-11-29T00:26:14.031135Z","iopub.status.idle":"2024-11-29T00:26:14.919288Z","shell.execute_reply.started":"2024-11-29T00:26:14.031097Z","shell.execute_reply":"2024-11-29T00:26:14.917946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#train the whole thing\nmodel = xgb.XGBRegressor(\n    objective = 'reg:squarederror',\n    booster = 'gbtree',\n    random_state = 42,\n    **param\n)\nmodel.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:27:15.435167Z","iopub.execute_input":"2024-11-29T00:27:15.435572Z","iopub.status.idle":"2024-11-29T00:36:13.704031Z","shell.execute_reply.started":"2024-11-29T00:27:15.435523Z","shell.execute_reply":"2024-11-29T00:36:13.702920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#load test data\ndata_test = pd.read_csv('/kaggle/input/brist1d/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:37:26.281206Z","iopub.execute_input":"2024-11-29T00:37:26.281592Z","iopub.status.idle":"2024-11-29T00:37:26.525773Z","shell.execute_reply.started":"2024-11-29T00:37:26.281553Z","shell.execute_reply":"2024-11-29T00:37:26.524583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#data transformation\ndata_test[act_cols] = data_test[act_cols].fillna(\"None\")\nfor col in act_cols:\n    data_test[col] = act_encoder.fit_transform(data_test[col].values.ravel())\ndata_test = add_feature_cylindric_time(data_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:38:55.735777Z","iopub.execute_input":"2024-11-29T00:38:55.736222Z","iopub.status.idle":"2024-11-29T00:38:55.867723Z","shell.execute_reply.started":"2024-11-29T00:38:55.736180Z","shell.execute_reply":"2024-11-29T00:38:55.866452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = data_test.drop(columns=['id','p_num','time'], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:39:59.904751Z","iopub.execute_input":"2024-11-29T00:39:59.905185Z","iopub.status.idle":"2024-11-29T00:39:59.921978Z","shell.execute_reply.started":"2024-11-29T00:39:59.905146Z","shell.execute_reply":"2024-11-29T00:39:59.920005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#predict the outcome\ny_pred = model.predict(X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:46:23.593354Z","iopub.execute_input":"2024-11-29T00:46:23.593861Z","iopub.status.idle":"2024-11-29T00:46:23.776420Z","shell.execute_reply.started":"2024-11-29T00:46:23.593813Z","shell.execute_reply":"2024-11-29T00:46:23.775240Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_res=pd.DataFrame({'id':data_test['id'], 'bg+1:00':y_pred})\ndf_res.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:49:05.678045Z","iopub.execute_input":"2024-11-29T00:49:05.678475Z","iopub.status.idle":"2024-11-29T00:49:05.694855Z","shell.execute_reply.started":"2024-11-29T00:49:05.678436Z","shell.execute_reply":"2024-11-29T00:49:05.693417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_res.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T00:49:07.681362Z","iopub.execute_input":"2024-11-29T00:49:07.681781Z","iopub.status.idle":"2024-11-29T00:49:07.693892Z","shell.execute_reply.started":"2024-11-29T00:49:07.681738Z","shell.execute_reply":"2024-11-29T00:49:07.692782Z"}},"outputs":[],"execution_count":null}]}