{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":41880,"databundleVersionId":5677426,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# importing all the libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport warnings\nimport os\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\n\n#to remove warnings\nwarnings.filterwarnings(action = \"ignore\", category = DeprecationWarning ) \nwarnings.filterwarnings(action = \"ignore\", category = FutureWarning ) \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport seaborn as sns\nimport warnings\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nfrom sklearn.metrics import accuracy_score, classification_report\n\nimport lightgbm as lgb\n\n!pip install xgboost\nimport xgboost as xgb\n\nwarnings.filterwarnings(action = \"ignore\", category = DeprecationWarning ) \nwarnings.filterwarnings(action = \"ignore\", category = FutureWarning ) ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-24T08:08:59.724967Z","iopub.execute_input":"2024-08-24T08:08:59.726290Z","iopub.status.idle":"2024-08-24T08:09:38.925287Z","shell.execute_reply.started":"2024-08-24T08:08:59.726234Z","shell.execute_reply":"2024-08-24T08:09:38.923511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog'\ntdcsfog_list = []\n\n# Iterate over each file in the directory\nfor file_name in os.listdir(tdcsfog_path):\n    if file_name.endswith('.csv') and file_name != '003f117e14.csv':  # Exclude the specific file\n        file_path = os.path.join(tdcsfog_path, file_name)\n        df = pd.read_csv(file_path)\n        \n        # Add a new column with the file name without the .csv extension\n        df['file_name'] = file_name[:-4]\n        \n        tdcsfog_list.append(df)\n\n# Concatenate all DataFrames in the list into a single DataFrame\ntdcsfog = pd.concat(tdcsfog_list, ignore_index=True)\n\n# Create the 'IsFOG' column based on any non-zero value in 'StartHesitation', 'Walking', 'Turn' columns\ntdcsfog['IsFOG'] = tdcsfog[['StartHesitation', 'Walking', 'Turn']].any(axis='columns')\n\n# Display the first few rows of the DataFrame\ntdcsfog.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:09:38.927924Z","iopub.execute_input":"2024-08-24T08:09:38.928794Z","iopub.status.idle":"2024-08-24T08:09:58.203102Z","shell.execute_reply.started":"2024-08-24T08:09:38.928753Z","shell.execute_reply":"2024-08-24T08:09:58.201971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_metadata = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tdcsfog_metadata.csv')\n# Merge the dataframes based on matching 'file_name' in tdcsfog and 'id' in tdcsfog_metadata\n# Assuming 'file_name' and 'id' are the column names in the respective dataframes\n# and that the 'id' in tdcsfog_metadata corresponds to 'file_name' in tdcsfog\ntdcsfog = tdcsfog.merge(tdcsfog_metadata[['Id', 'Subject']], left_on='file_name', right_on='Id', how='left')\n\n# Rename the 'Subject' column from tdcsfog_metadata to 'subject' in tdcsfog\ntdcsfog = tdcsfog.rename(columns={'Subject': 'subject'})\n# Drop the now unnecessary 'id' column from the merge\ntdcsfog = tdcsfog.drop(columns=['Id'])\n\nprint(tdcsfog.head())\n# Count the number of unique values in each column\nunique_counts = tdcsfog.nunique()\n\n# Display the number of unique values in each column\nprint(unique_counts)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:09:58.204862Z","iopub.execute_input":"2024-08-24T08:09:58.205687Z","iopub.status.idle":"2024-08-24T08:10:09.091289Z","shell.execute_reply.started":"2024-08-24T08:09:58.205590Z","shell.execute_reply":"2024-08-24T08:10:09.089882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog'\ndefog_list = []\n\n# Iterate over each file in the directory\nfor file_name in os.listdir(defog_path):\n    if file_name.endswith('.csv') and file_name != '003f117e14.csv':  # Exclude the specific file\n        file_path = os.path.join(defog_path, file_name)\n        df = pd.read_csv(file_path)\n        \n        # Add a new column with the file name without the .csv extension\n        df['file_name'] = file_name[:-4]\n        \n        defog_list.append(df)\n\n# Concatenate all DataFrames in the list into a single DataFrame\ndefog = pd.concat(defog_list, ignore_index=True)\n\n# Create the 'IsFOG' column based on any non-zero value in 'StartHesitation', 'Walking', 'Turn' columns\ndefog['IsFOG'] = defog[['StartHesitation', 'Walking', 'Turn']].any(axis='columns')\n\ndefog = defog.drop(columns=['Valid', 'Task'])\n\n# Display the first few rows of the DataFrame\ndefog.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:10:09.094250Z","iopub.execute_input":"2024-08-24T08:10:09.094608Z","iopub.status.idle":"2024-08-24T08:10:36.973802Z","shell.execute_reply.started":"2024-08-24T08:10:09.094576Z","shell.execute_reply":"2024-08-24T08:10:36.972739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_metadata = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/defog_metadata.csv')\n# Merge the dataframes based on matching 'file_name' in tdcsfog and 'id' in tdcsfog_metadata\n# Assuming 'file_name' and 'id' are the column names in the respective dataframes\n# and that the 'id' in tdcsfog_metadata corresponds to 'file_name' in tdcsfog\ndefog = defog.merge(defog_metadata[['Id', 'Subject']], left_on='file_name', right_on='Id', how='left')\n\n# Rename the 'Subject' column from tdcsfog_metadata to 'subject' in tdcsfog\ndefog = defog.rename(columns={'Subject': 'subject'})\n# Drop the now unnecessary 'id' column from the merge\ndefog = defog.drop(columns=['Id'])\n\nprint(tdcsfog.head())\n# Count the number of unique values in each column\nunique_counts = defog.nunique()\n\n# Display the number of unique values in each column\nprint(unique_counts)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:10:36.975484Z","iopub.execute_input":"2024-08-24T08:10:36.975945Z","iopub.status.idle":"2024-08-24T08:10:54.645102Z","shell.execute_reply.started":"2024-08-24T08:10:36.975905Z","shell.execute_reply":"2024-08-24T08:10:54.643275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_rolling_window_features(data, window_size=200, feature_columns=['AccV', 'AccML', 'AccAP']):\n    \"\"\"\n    Add rolling window features to the dataset.\n\n    Parameters:\n    - data: The pandas DataFrame to which the features will be added.\n    - window_size: The size of the rolling window. 2 seconds window for 100Hz sampling rate\n    - feature_columns: The columns to calculate the rolling features for.\n\n    Returns:\n    - The pandas DataFrame with the new rolling window features added.\n    \"\"\"\n    for axis in feature_columns:\n        data[f'{axis}_rolling_mean'] = data[axis].rolling(window=window_size, min_periods=1).mean()\n        data[f'{axis}_rolling_std'] = data[axis].rolling(window=window_size, min_periods=1).std()\n        data[f'{axis}_rolling_max'] = data[axis].rolling(window=window_size, min_periods=1).max()\n        data[f'{axis}_rolling_min'] = data[axis].rolling(window=window_size, min_periods=1).min()\n    \n    data.dropna(inplace=True)\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:10:54.646903Z","iopub.execute_input":"2024-08-24T08:10:54.647337Z","iopub.status.idle":"2024-08-24T08:10:54.658666Z","shell.execute_reply.started":"2024-08-24T08:10:54.647296Z","shell.execute_reply":"2024-08-24T08:10:54.657281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog = add_rolling_window_features(tdcsfog)\ndefog = add_rolling_window_features(defog)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:10:54.660130Z","iopub.execute_input":"2024-08-24T08:10:54.660530Z","iopub.status.idle":"2024-08-24T08:11:13.285779Z","shell.execute_reply.started":"2024-08-24T08:10:54.660494Z","shell.execute_reply":"2024-08-24T08:11:13.284567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract unique file names\nunique_files_tdcsfog = tdcsfog['subject'].unique()\n\n# Calculate the split index\nsplit_index_tdcsfog  = int(0.8 * len(unique_files_tdcsfog))\n\n# Split the file names into training and test sets\ntrain_files_tdcsfog = unique_files_tdcsfog[:split_index_tdcsfog]\ntest_files_tdcsfog = unique_files_tdcsfog[split_index_tdcsfog:]\n\n# Filter the DataFrame for training and test sets\ntrain_data_tdcsfog = tdcsfog[tdcsfog['subject'].isin(train_files_tdcsfog)]\ntest_data_tdcsfog = tdcsfog[tdcsfog['subject'].isin(test_files_tdcsfog)]\n\n# Print the number of rows in each set to verify\nprint(f'Train data rows: {len(train_data_tdcsfog)}')\nprint(f'Test data rows: {len(test_data_tdcsfog)}')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:11:13.287783Z","iopub.execute_input":"2024-08-24T08:11:13.288295Z","iopub.status.idle":"2024-08-24T08:11:15.662172Z","shell.execute_reply.started":"2024-08-24T08:11:13.288247Z","shell.execute_reply":"2024-08-24T08:11:15.660813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract unique file names\nunique_files_defog = defog['subject'].unique()\n\n# Calculate the split index\nsplit_index_defog  = int(0.8 * len(unique_files_defog))\n\n# Split the file names into training and test sets\ntrain_files_defog = unique_files_defog[:split_index_defog]\ntest_files_defog = unique_files_defog[split_index_defog:]\n\n# Filter the DataFrame for training and test sets\ntrain_data_defog = defog[defog['subject'].isin(train_files_defog)]\ntest_data_defog = defog[defog['subject'].isin(test_files_defog)]\n\n# Print the number of rows in each set to verify\nprint(f'Train data rows: {len(train_data_defog)}')\nprint(f'Test data rows: {len(test_data_defog)}')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:11:15.664221Z","iopub.execute_input":"2024-08-24T08:11:15.664720Z","iopub.status.idle":"2024-08-24T08:11:20.124424Z","shell.execute_reply.started":"2024-08-24T08:11:15.664670Z","shell.execute_reply":"2024-08-24T08:11:20.122998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming 'train_data' and 'test_data' are pandas DataFrames\ntrain_features_tdcsfog = train_data_tdcsfog[['Time','AccV', 'AccML', 'AccAP', \n                   'AccV_rolling_mean', 'AccV_rolling_std', 'AccV_rolling_max', 'AccV_rolling_min',\n                   'AccML_rolling_mean', 'AccML_rolling_std', 'AccML_rolling_max', 'AccML_rolling_min',\n                   'AccAP_rolling_mean', 'AccAP_rolling_std', 'AccAP_rolling_max', 'AccAP_rolling_min']]\ntest_features_tdcsfog = test_data_tdcsfog[['Time','AccV', 'AccML', 'AccAP', \n                   'AccV_rolling_mean', 'AccV_rolling_std', 'AccV_rolling_max', 'AccV_rolling_min',\n                   'AccML_rolling_mean', 'AccML_rolling_std', 'AccML_rolling_max', 'AccML_rolling_min',\n                   'AccAP_rolling_mean', 'AccAP_rolling_std', 'AccAP_rolling_max', 'AccAP_rolling_min']]\n\n# Select the label column\ntrain_labels_tdcsfog = train_data_tdcsfog['IsFOG']\ntest_labels_tdcsfog = test_data_tdcsfog['IsFOG']\n\n# Create LightGBM datasets\ntrain_dataset_tdcsfog = lgb.Dataset(train_features_tdcsfog, label=train_labels_tdcsfog)\ntest_dataset_tdcsfog = lgb.Dataset(test_features_tdcsfog, label=test_labels_tdcsfog)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:11:20.128993Z","iopub.execute_input":"2024-08-24T08:11:20.129430Z","iopub.status.idle":"2024-08-24T08:11:20.698863Z","shell.execute_reply.started":"2024-08-24T08:11:20.129384Z","shell.execute_reply":"2024-08-24T08:11:20.697760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming 'train_data' and 'test_data' are pandas DataFrames\ntrain_features_defog = train_data_defog[['Time','AccV', 'AccML', 'AccAP', \n                   'AccV_rolling_mean', 'AccV_rolling_std', 'AccV_rolling_max', 'AccV_rolling_min',\n                   'AccML_rolling_mean', 'AccML_rolling_std', 'AccML_rolling_max', 'AccML_rolling_min',\n                   'AccAP_rolling_mean', 'AccAP_rolling_std', 'AccAP_rolling_max', 'AccAP_rolling_min']]\ntest_features_defog = test_data_defog[['Time','AccV', 'AccML', 'AccAP', \n                   'AccV_rolling_mean', 'AccV_rolling_std', 'AccV_rolling_max', 'AccV_rolling_min',\n                   'AccML_rolling_mean', 'AccML_rolling_std', 'AccML_rolling_max', 'AccML_rolling_min',\n                   'AccAP_rolling_mean', 'AccAP_rolling_std', 'AccAP_rolling_max', 'AccAP_rolling_min']]\n\n# Select the label column\ntrain_labels_defog = train_data_defog['IsFOG']\ntest_labels_defog = test_data_defog['IsFOG']\n\n# Create LightGBM datasets\ntrain_dataset_defog = lgb.Dataset(train_features_defog, label=train_labels_defog)\ntest_dataset_defog = lgb.Dataset(test_features_defog, label=test_labels_defog)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:11:20.700108Z","iopub.execute_input":"2024-08-24T08:11:20.700423Z","iopub.status.idle":"2024-08-24T08:11:21.830899Z","shell.execute_reply.started":"2024-08-24T08:11:20.700396Z","shell.execute_reply":"2024-08-24T08:11:21.829419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog_params={\n    'objective': 'binary', #binary target feature\n    'metric': 'average_precision', \n    'boosting_type': 'gbdt',  #GradientBoostingDecisionTree\n    'learning_rate': 0.18,\n    'verbose': 1,\n    'max_depth': 10,\n    'num_leaves': 80,\n    'is_unbalance':True\n}\n\n# Train the LightGBM model\nnum_round = 200  \n\n# Train the model\nfog_model_tdcsfog = lgb.train(fog_params, train_dataset_tdcsfog, num_round, valid_sets=[test_dataset_tdcsfog])\n\n# Make predictions\ny_pred_tdcsfog = fog_model_tdcsfog.predict(test_features_tdcsfog, num_iteration=fog_model_tdcsfog.best_iteration)\n\n# Convert probabilities to binary predictions\ny_pred_binary_tdcsfog = (y_pred_tdcsfog > 0.5).astype(int)\n\n# Evaluate the model\naccuracy = metrics.accuracy_score(test_labels_tdcsfog, y_pred_binary_tdcsfog)\nprint(f\"Accuracy: {accuracy}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:11:21.832583Z","iopub.execute_input":"2024-08-24T08:11:21.832999Z","iopub.status.idle":"2024-08-24T08:13:16.915920Z","shell.execute_reply.started":"2024-08-24T08:11:21.832961Z","shell.execute_reply":"2024-08-24T08:13:16.913753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog_params={\n    'objective': 'binary', #binary target feature\n    'metric': 'average_precision', \n    'boosting_type': 'gbdt',  #GradientBoostingDecisionTree\n    'learning_rate': 0.18,\n    'verbose': 1,\n    'max_depth': 10,\n    'num_leaves': 80,\n    'is_unbalance':True\n}\n# Train the LightGBM model\nnum_round = 200  \n\n# Train the model\nfog_model_defog = lgb.train(fog_params, train_dataset_defog, num_round, valid_sets=[test_dataset_defog])\n\n# Make predictions\ny_pred_defog = fog_model_defog.predict(test_features_defog, num_iteration=fog_model_defog.best_iteration)\n\n# Convert probabilities to binary predictions\ny_pred_binary_defog = (y_pred_defog > 0.5).astype(int)\n\n# Evaluate the model\naccuracy_defog = metrics.accuracy_score(test_labels_defog, y_pred_binary_defog)\nprint(f\"Accuracy: {accuracy_defog}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:13:16.918442Z","iopub.execute_input":"2024-08-24T08:13:16.918991Z","iopub.status.idle":"2024-08-24T08:16:38.546771Z","shell.execute_reply.started":"2024-08-24T08:13:16.918936Z","shell.execute_reply":"2024-08-24T08:16:38.545433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = [\"StartHesitation\", \"Turn\", 'Walking', 'IsFOG', 'file_name','subject']","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:16:38.548188Z","iopub.execute_input":"2024-08-24T08:16:38.548823Z","iopub.status.idle":"2024-08-24T08:16:38.555576Z","shell.execute_reply.started":"2024-08-24T08:16:38.548774Z","shell.execute_reply":"2024-08-24T08:16:38.554134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"CLASSIFICATION","metadata":{}},{"cell_type":"code","source":"train_data_tdcsfog_cl = train_data_tdcsfog[train_data_tdcsfog['IsFOG'] == True]\ntest_data_tdcsfog_cl = test_data_tdcsfog[test_data_tdcsfog['IsFOG'] == True]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:16:38.557566Z","iopub.execute_input":"2024-08-24T08:16:38.558037Z","iopub.status.idle":"2024-08-24T08:16:38.919863Z","shell.execute_reply.started":"2024-08-24T08:16:38.558004Z","shell.execute_reply":"2024-08-24T08:16:38.918298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_defog_cl = train_data_defog[train_data_defog['IsFOG'] == True]\ntest_data_defog_cl = test_data_defog[test_data_defog['IsFOG'] == True]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:16:38.921786Z","iopub.execute_input":"2024-08-24T08:16:38.922203Z","iopub.status.idle":"2024-08-24T08:16:39.056019Z","shell.execute_reply.started":"2024-08-24T08:16:38.922167Z","shell.execute_reply":"2024-08-24T08:16:39.054681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X values for on which the model is trained\nX_train_tdcsfog = train_data_tdcsfog_cl.drop(targets, axis=1)  # features\n\n# split data into the three \ny1_train_tdcsfog = train_data_tdcsfog_cl[['StartHesitation', 'Turn','Walking']]\n# X values for on which the model is trained\nX_test_tdcsfog = test_data_tdcsfog_cl.drop(targets, axis=1)  # features\n\n# split data into the three \ny1_test_tdcsfog = test_data_tdcsfog_cl[['StartHesitation', 'Turn','Walking']]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:16:39.058389Z","iopub.execute_input":"2024-08-24T08:16:39.059236Z","iopub.status.idle":"2024-08-24T08:16:39.258814Z","shell.execute_reply.started":"2024-08-24T08:16:39.059177Z","shell.execute_reply":"2024-08-24T08:16:39.257514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X values for on which the model is trained\nX_train_defog = train_data_defog_cl.drop(targets, axis=1)  # features\n\n# split data into the three \ny1_train_defog = train_data_defog_cl[['StartHesitation', 'Turn','Walking']]\n# X values for on which the model is trained\nX_test_defog = test_data_defog_cl.drop(targets, axis=1)  # features\n\n# split data into the three \ny1_test_defog = test_data_defog_cl[['StartHesitation', 'Turn','Walking']]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:16:39.260532Z","iopub.execute_input":"2024-08-24T08:16:39.260954Z","iopub.status.idle":"2024-08-24T08:16:39.344534Z","shell.execute_reply.started":"2024-08-24T08:16:39.260919Z","shell.execute_reply":"2024-08-24T08:16:39.343323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1_train_tdcsfog = pd.DataFrame(y1_train_tdcsfog)\ny1_test_tdcsfog = pd.DataFrame(y1_test_tdcsfog)\ny1_train_defog = pd.DataFrame(y1_train_defog)\ny1_test_defog = pd.DataFrame(y1_test_defog)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:16:39.346352Z","iopub.execute_input":"2024-08-24T08:16:39.346870Z","iopub.status.idle":"2024-08-24T08:16:39.354314Z","shell.execute_reply.started":"2024-08-24T08:16:39.346815Z","shell.execute_reply":"2024-08-24T08:16:39.352956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1_train_tdcsfog","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:18:20.303412Z","iopub.execute_input":"2024-08-24T08:18:20.304342Z","iopub.status.idle":"2024-08-24T08:18:20.317289Z","shell.execute_reply.started":"2024-08-24T08:18:20.304301Z","shell.execute_reply":"2024-08-24T08:18:20.315884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_StartHesitation_tdcsfog_train = y1_train_tdcsfog['StartHesitation']\ny_Turn_tdcsfog_train = y1_train_tdcsfog['Turn']\ny_Walking_tdcsfog_train = y1_train_tdcsfog['Walking']\n\ny_StartHesitation_tdcsfog_test = y1_test_tdcsfog['StartHesitation']\ny_Turn_tdcsfog_test = y1_test_tdcsfog['Turn']\ny_Walking_tdcsfog_test = y1_test_tdcsfog['Walking']","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:18:38.235738Z","iopub.execute_input":"2024-08-24T08:18:38.236143Z","iopub.status.idle":"2024-08-24T08:18:38.248973Z","shell.execute_reply.started":"2024-08-24T08:18:38.236112Z","shell.execute_reply":"2024-08-24T08:18:38.247040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_StartHesitation_defog_train = y1_train_defog['StartHesitation']\ny_Turn_defog_train = y1_train_defog['Turn']\ny_Walking_defog_train = y1_train_defog['Walking']\n\ny_StartHesitation_defog_test = y1_test_defog['StartHesitation']\ny_Turn_defog_test = y1_test_defog['Turn']\ny_Walking_defog_test = y1_test_defog['Walking']","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:19:04.776269Z","iopub.execute_input":"2024-08-24T08:19:04.776704Z","iopub.status.idle":"2024-08-24T08:19:04.789107Z","shell.execute_reply.started":"2024-08-24T08:19:04.776670Z","shell.execute_reply":"2024-08-24T08:19:04.787743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define hyperparameters and objective for LightGBM\nparams = {\n    'objective': 'binary',\n    'metric': 'average_precision',\n    'boosting_type': 'gbdt',\n    'learning_rate': 0.03,\n    'verbose': 1,\n    'max_depth': 6,\n    'num_leaves': 50\n}","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:19:06.720486Z","iopub.execute_input":"2024-08-24T08:19:06.721422Z","iopub.status.idle":"2024-08-24T08:19:06.727290Z","shell.execute_reply.started":"2024-08-24T08:19:06.721382Z","shell.execute_reply":"2024-08-24T08:19:06.726063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create LightGBM datasets\ntrain_dataset_defog_SH = lgb.Dataset(X_train_defog, label=y_StartHesitation_defog_train)\ntest_dataset_defog_SH = lgb.Dataset(X_test_defog, label=y_StartHesitation_defog_test, reference=train_dataset_defog_SH)\n\nnum_round = 300\n# Training a LightGBM model for StartHesitation\nmodel_SH_defog= lgb.train(params, train_dataset_defog_SH, num_round, valid_sets=[test_dataset_defog_SH])","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:19:09.689129Z","iopub.execute_input":"2024-08-24T08:19:09.689547Z","iopub.status.idle":"2024-08-24T08:19:24.880748Z","shell.execute_reply.started":"2024-08-24T08:19:09.689513Z","shell.execute_reply":"2024-08-24T08:19:24.879469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create LightGBM datasets\ntrain_dataset_defog_T = lgb.Dataset(X_train_defog, label=y_Turn_defog_train)\ntest_dataset_defog_T = lgb.Dataset(X_test_defog, label=y_Turn_defog_test, reference=train_dataset_defog_T)\nnum_round = 300\n# Training a LightGBM model for StartHesitation\nmodel_T_defog= lgb.train(params, train_dataset_defog_T, num_round, valid_sets=[test_dataset_defog_T])\n","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:20:50.769308Z","iopub.execute_input":"2024-08-24T08:20:50.769883Z","iopub.status.idle":"2024-08-24T08:21:08.862301Z","shell.execute_reply.started":"2024-08-24T08:20:50.769830Z","shell.execute_reply":"2024-08-24T08:21:08.861171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create LightGBM datasets\ntrain_dataset_defog_W = lgb.Dataset(X_train_defog, label=y_Walking_defog_train)\ntest_dataset_defog_W = lgb.Dataset(X_test_defog, label=y_Walking_defog_test,reference=train_dataset_defog_W)\n\nnum_round = 300\nmodel_W_defog= lgb.train(params, train_dataset_defog_W, num_round, valid_sets=[test_dataset_defog_W])","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:21:57.802494Z","iopub.execute_input":"2024-08-24T08:21:57.803597Z","iopub.status.idle":"2024-08-24T08:22:16.747732Z","shell.execute_reply.started":"2024-08-24T08:21:57.803548Z","shell.execute_reply":"2024-08-24T08:22:16.746560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create LightGBM datasets\ntrain_dataset_tdcsfog_SH = lgb.Dataset(X_train_tdcsfog, label=y_StartHesitation_tdcsfog_train)\ntest_dataset_tdcsfog_SH = lgb.Dataset(X_test_tdcsfog, label=y_StartHesitation_tdcsfog_test, reference=train_dataset_tdcsfog_SH)\n\nnum_round = 300\nmodel_SH_tdcsfog= lgb.train(params, train_dataset_tdcsfog_SH, num_round, valid_sets=[test_dataset_tdcsfog_SH])","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:22:28.917692Z","iopub.execute_input":"2024-08-24T08:22:28.918773Z","iopub.status.idle":"2024-08-24T08:23:13.925710Z","shell.execute_reply.started":"2024-08-24T08:22:28.918719Z","shell.execute_reply":"2024-08-24T08:23:13.924619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create LightGBM datasets\ntrain_dataset_tdcsfog_T = lgb.Dataset(X_train_tdcsfog, label=y_Turn_tdcsfog_train)\ntest_dataset_tdcsfog_T = lgb.Dataset(X_test_tdcsfog, label=y_Turn_tdcsfog_test, reference=train_dataset_tdcsfog_T)\nnum_round = 300\nmodel_T_tdcsfog= lgb.train(params, train_dataset_tdcsfog_T, num_round, valid_sets=[test_dataset_tdcsfog_T])","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:26:09.247389Z","iopub.execute_input":"2024-08-24T08:26:09.248503Z","iopub.status.idle":"2024-08-24T08:26:58.510626Z","shell.execute_reply.started":"2024-08-24T08:26:09.248425Z","shell.execute_reply":"2024-08-24T08:26:58.509411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create LightGBM datasets\ntrain_dataset_tdcsfog_W = lgb.Dataset(X_train_tdcsfog, label=y_Walking_tdcsfog_train)\ntest_dataset_tdcsfog_W = lgb.Dataset(X_test_tdcsfog, label=y_Walking_tdcsfog_test, reference=train_dataset_tdcsfog_W)\nnum_round = 300\nmodel_W_tdcsfog= lgb.train(params, train_dataset_tdcsfog_W, num_round, valid_sets=[test_dataset_tdcsfog_W])","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:27:02.302492Z","iopub.execute_input":"2024-08-24T08:27:02.302948Z","iopub.status.idle":"2024-08-24T08:27:46.460390Z","shell.execute_reply.started":"2024-08-24T08:27:02.302914Z","shell.execute_reply":"2024-08-24T08:27:46.459219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"SUBMISSION","metadata":{}},{"cell_type":"code","source":"tdcsfog_test_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/tdcsfog'\n        \ntdcsfog_test_list = [\n    pd.read_csv(os.path.join(tdcsfog_test_path, file_name)).assign(Id=lambda df: file_name[:-4] + '_' + df['Time'].astype(str))\n    for file_name in os.listdir(tdcsfog_test_path)\n    if file_name.endswith('.csv')\n]        \n\ntdcsfog_test = pd.concat(tdcsfog_test_list, axis = 0)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:07.316136Z","iopub.execute_input":"2024-08-24T08:25:07.316661Z","iopub.status.idle":"2024-08-24T08:25:07.356688Z","shell.execute_reply.started":"2024-08-24T08:25:07.316602Z","shell.execute_reply":"2024-08-24T08:25:07.355518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_test_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/defog'\n\ndefog_test_list = [\n    pd.read_csv(os.path.join(defog_test_path, file_name)).assign(Id=lambda df: file_name[:-4] + '_' + df['Time'].astype(str))\n    for file_name in os.listdir(defog_test_path)\n    if file_name.endswith('.csv')\n]        \n\ndefog_test = pd.concat(defog_test_list, axis = 0)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:10.467129Z","iopub.execute_input":"2024-08-24T08:25:10.467523Z","iopub.status.idle":"2024-08-24T08:25:11.148888Z","shell.execute_reply.started":"2024-08-24T08:25:10.467492Z","shell.execute_reply":"2024-08-24T08:25:11.147692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"window_size = 200  # 2 seconds window for 100Hz sampling rate\n\n# Calculating rolling window features for each acceleration axis\nfor axis in ['AccV', 'AccML', 'AccAP']:\n    defog_test[f'{axis}_rolling_mean'] = defog_test[axis].rolling(window=window_size, min_periods=1).mean()\n    defog_test[f'{axis}_rolling_std'] = defog_test[axis].rolling(window=window_size, min_periods=1).std()\n    defog_test[f'{axis}_rolling_max'] = defog_test[axis].rolling(window=window_size, min_periods=1).max()\n    defog_test[f'{axis}_rolling_min'] = defog_test[axis].rolling(window=window_size, min_periods=1).min()","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:13.562750Z","iopub.execute_input":"2024-08-24T08:25:13.563250Z","iopub.status.idle":"2024-08-24T08:25:13.700408Z","shell.execute_reply.started":"2024-08-24T08:25:13.563214Z","shell.execute_reply":"2024-08-24T08:25:13.699168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculating rolling window features for each acceleration axis\nfor axis in ['AccV', 'AccML', 'AccAP']:\n    tdcsfog_test[f'{axis}_rolling_mean'] = tdcsfog_test[axis].rolling(window=window_size, min_periods=1).mean()\n    tdcsfog_test[f'{axis}_rolling_std'] = tdcsfog_test[axis].rolling(window=window_size, min_periods=1).std()\n    tdcsfog_test[f'{axis}_rolling_max'] = tdcsfog_test[axis].rolling(window=window_size, min_periods=1).max()\n    tdcsfog_test[f'{axis}_rolling_min'] = tdcsfog_test[axis].rolling(window=window_size, min_periods=1).min()","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:15.732256Z","iopub.execute_input":"2024-08-24T08:25:15.732694Z","iopub.status.idle":"2024-08-24T08:25:15.752318Z","shell.execute_reply.started":"2024-08-24T08:25:15.732659Z","shell.execute_reply":"2024-08-24T08:25:15.751180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Updating feature selection with rolling window features\nfeature_columns = ['Time','AccV', 'AccML', 'AccAP', \n                   'AccV_rolling_mean', 'AccV_rolling_std', 'AccV_rolling_max', 'AccV_rolling_min',\n                   'AccML_rolling_mean', 'AccML_rolling_std', 'AccML_rolling_max', 'AccML_rolling_min',\n                   'AccAP_rolling_mean', 'AccAP_rolling_std', 'AccAP_rolling_max', 'AccAP_rolling_min']\n\nX_defog = defog_test[feature_columns]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:19.294984Z","iopub.execute_input":"2024-08-24T08:25:19.295434Z","iopub.status.idle":"2024-08-24T08:25:19.317273Z","shell.execute_reply.started":"2024-08-24T08:25:19.295400Z","shell.execute_reply":"2024-08-24T08:25:19.316162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Updating feature selection with rolling window features\nfeature_columns = ['Time','AccV', 'AccML', 'AccAP', \n                   'AccV_rolling_mean', 'AccV_rolling_std', 'AccV_rolling_max', 'AccV_rolling_min',\n                   'AccML_rolling_mean', 'AccML_rolling_std', 'AccML_rolling_max', 'AccML_rolling_min',\n                   'AccAP_rolling_mean', 'AccAP_rolling_std', 'AccAP_rolling_max', 'AccAP_rolling_min']\n\nX_tdcsfog= tdcsfog_test[feature_columns]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:21.756906Z","iopub.execute_input":"2024-08-24T08:25:21.757327Z","iopub.status.idle":"2024-08-24T08:25:21.767440Z","shell.execute_reply.started":"2024-08-24T08:25:21.757293Z","shell.execute_reply":"2024-08-24T08:25:21.766291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred_defog = fog_model_defog.predict(X_defog)\ntest_pred_tdcsfog = fog_model_tdcsfog.predict(X_tdcsfog)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:24.386024Z","iopub.execute_input":"2024-08-24T08:25:24.386461Z","iopub.status.idle":"2024-08-24T08:25:25.465759Z","shell.execute_reply.started":"2024-08-24T08:25:24.386427Z","shell.execute_reply":"2024-08-24T08:25:25.464088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Add momentum to defog identification; FOG event continuity\nde_len = len(test_pred_defog)\ndefog_pred = np.insert(test_pred_defog, 0, 0)\ndefog_pred_fog = [(defog_pred[i]/8) + defog_pred[i+1] for i in range(de_len)]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:31.023279Z","iopub.execute_input":"2024-08-24T08:25:31.023744Z","iopub.status.idle":"2024-08-24T08:25:31.268511Z","shell.execute_reply.started":"2024-08-24T08:25:31.023708Z","shell.execute_reply":"2024-08-24T08:25:31.267018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Add momentum to defog identification; FOG event continuity\ntdcsfog_len = len(test_pred_tdcsfog)\ntdcsfog_pred = np.insert(test_pred_tdcsfog, 0, 0)\ntdcsfog_pred_fog = [(tdcsfog_pred[i]/8) + tdcsfog_pred[i+1] for i in range(tdcsfog_len)]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:33.509553Z","iopub.execute_input":"2024-08-24T08:25:33.510033Z","iopub.status.idle":"2024-08-24T08:25:33.522053Z","shell.execute_reply.started":"2024-08-24T08:25:33.509998Z","shell.execute_reply":"2024-08-24T08:25:33.520616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_test['FogProb'] = tdcsfog_pred_fog","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:35.877962Z","iopub.execute_input":"2024-08-24T08:25:35.879122Z","iopub.status.idle":"2024-08-24T08:25:35.887586Z","shell.execute_reply.started":"2024-08-24T08:25:35.879080Z","shell.execute_reply":"2024-08-24T08:25:35.886308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_test['FogProb'] = defog_pred_fog","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:37.827761Z","iopub.execute_input":"2024-08-24T08:25:37.829029Z","iopub.status.idle":"2024-08-24T08:25:37.934477Z","shell.execute_reply.started":"2024-08-24T08:25:37.828989Z","shell.execute_reply":"2024-08-24T08:25:37.933296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_defog_SH_pred = model_SH_defog.predict(X_defog, predict_disable_shape_check='TRUE')\ntest_defog_T_pred = model_T_defog.predict(X_defog, predict_disable_shape_check='TRUE')\ntest_defog_W_pred = model_W_defog.predict(X_defog, predict_disable_shape_check='TRUE')","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:25:40.579810Z","iopub.execute_input":"2024-08-24T08:25:40.580677Z","iopub.status.idle":"2024-08-24T08:25:44.697169Z","shell.execute_reply.started":"2024-08-24T08:25:40.580611Z","shell.execute_reply":"2024-08-24T08:25:44.696088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_tdcsfog_SH_pred = model_SH_tdcsfog.predict(X_tdcsfog)\ntest_tdcsfog_T_pred = model_T_tdcsfog.predict(X_tdcsfog)\ntest_tdcsfog_W_pred = model_W_tdcsfog.predict(X_tdcsfog)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:27:58.851047Z","iopub.execute_input":"2024-08-24T08:27:58.851481Z","iopub.status.idle":"2024-08-24T08:27:58.940508Z","shell.execute_reply.started":"2024-08-24T08:27:58.851448Z","shell.execute_reply":"2024-08-24T08:27:58.939295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_test['StartHesitation'] = np.sqrt(test_defog_SH_pred * defog_pred_fog)\ndefog_test['Turn'] = np.sqrt(test_defog_T_pred * defog_pred_fog)\ndefog_test['Walking'] = np.sqrt(test_defog_W_pred * defog_pred_fog)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:01.468611Z","iopub.execute_input":"2024-08-24T08:28:01.469189Z","iopub.status.idle":"2024-08-24T08:28:01.555922Z","shell.execute_reply.started":"2024-08-24T08:28:01.469146Z","shell.execute_reply":"2024-08-24T08:28:01.554676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntdcsfog_test['StartHesitation'] = np.sqrt(test_tdcsfog_SH_pred * tdcsfog_pred_fog)\ntdcsfog_test['Turn'] = np.sqrt(test_tdcsfog_T_pred * tdcsfog_pred_fog)\ntdcsfog_test['Walking'] = np.sqrt(test_tdcsfog_W_pred * tdcsfog_pred_fog)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:03.710166Z","iopub.execute_input":"2024-08-24T08:28:03.710675Z","iopub.status.idle":"2024-08-24T08:28:03.721773Z","shell.execute_reply.started":"2024-08-24T08:28:03.710622Z","shell.execute_reply":"2024-08-24T08:28:03.720340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_defog = defog_test[['Id','StartHesitation','Turn','Walking']]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:19.162993Z","iopub.execute_input":"2024-08-24T08:28:19.163516Z","iopub.status.idle":"2024-08-24T08:28:19.185622Z","shell.execute_reply.started":"2024-08-24T08:28:19.163471Z","shell.execute_reply":"2024-08-24T08:28:19.184356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_tdcsfog = tdcsfog_test[['Id','StartHesitation','Turn','Walking']]","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:20.825476Z","iopub.execute_input":"2024-08-24T08:28:20.826455Z","iopub.status.idle":"2024-08-24T08:28:20.845779Z","shell.execute_reply.started":"2024-08-24T08:28:20.826407Z","shell.execute_reply":"2024-08-24T08:28:20.834723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([submission_defog, submission_tdcsfog], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:23.831139Z","iopub.execute_input":"2024-08-24T08:28:23.831721Z","iopub.status.idle":"2024-08-24T08:28:23.852234Z","shell.execute_reply.started":"2024-08-24T08:28:23.831672Z","shell.execute_reply":"2024-08-24T08:28:23.850846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:25.468280Z","iopub.execute_input":"2024-08-24T08:28:25.469020Z","iopub.status.idle":"2024-08-24T08:28:28.161658Z","shell.execute_reply.started":"2024-08-24T08:28:25.468972Z","shell.execute_reply":"2024-08-24T08:28:28.160430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-08-24T08:28:31.006358Z","iopub.execute_input":"2024-08-24T08:28:31.006797Z","iopub.status.idle":"2024-08-24T08:28:31.024840Z","shell.execute_reply.started":"2024-08-24T08:28:31.006761Z","shell.execute_reply":"2024-08-24T08:28:31.023657Z"},"trusted":true},"execution_count":null,"outputs":[]}]}