{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\nimport glob\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\nfrom pathlib import Path\nfrom sklearn.cluster import KMeans\nfrom sklearn.feature_selection import mutual_info_classif\n\n\ndata_dir = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/'\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":"2023-06-09T10:57:51.115334Z","iopub.execute_input":"2023-06-09T10:57:51.115936Z","iopub.status.idle":"2023-06-09T10:57:51.125224Z","shell.execute_reply.started":"2023-06-09T10:57:51.115898Z","shell.execute_reply":"2023-06-09T10:57:51.124100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install fasteda","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:43:37.117245Z","iopub.execute_input":"2023-06-09T09:43:37.118160Z","iopub.status.idle":"2023-06-09T09:43:54.613111Z","shell.execute_reply.started":"2023-06-09T09:43:37.118125Z","shell.execute_reply":"2023-06-09T09:43:54.611916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_metadata_file = f'{data_dir}daily_metadata.csv'\ndefog_metadata_file = f'{data_dir}defog_metadata.csv'\ntdcsfog_metadata_file = f'{data_dir}tdcsfog_metadata.csv'\nevents_data_file = f'{data_dir}events.csv'\nsubjects_data_file = f'{data_dir}subjects.csv'\ntasks_data_file = f'{data_dir}tasks.csv'\n\n# Read the meta data\ndaily_metadata = pd.read_csv(daily_metadata_file)\ndefog_metadata = pd.read_csv(defog_metadata_file)\ntdcsfog_metadata = pd.read_csv(tdcsfog_metadata_file)\nfull_metadata = pd.concat([tdcsfog_metadata, defog_metadata])\n\nevents_data = pd.read_csv(events_data_file)\nsubjects_data = pd.read_csv(subjects_data_file)\ntasks_data = pd.read_csv(tasks_data_file)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:43:54.615466Z","iopub.execute_input":"2023-06-09T09:43:54.615940Z","iopub.status.idle":"2023-06-09T09:43:54.710316Z","shell.execute_reply.started":"2023-06-09T09:43:54.615890Z","shell.execute_reply":"2023-06-09T09:43:54.709157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the data and append the Id\ndef read_data(path):\n    df = pd.read_csv(path)\n    df['Id'] = path.split(\"/\")[-1].split(\".\")[0]\n    \n    return df\n\n\n# Read and concatenate all files of the train data from specified dataset\ndef create_full_data(dataset_name):\n    paths = glob.glob(data_dir + f'train/{dataset_name}/*')\n    final_df = pd.concat([read_data(p) for p in paths])\n    final_df['dataset'] = dataset_name\n    \n    return final_df","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:43:54.711721Z","iopub.execute_input":"2023-06-09T09:43:54.712083Z","iopub.status.idle":"2023-06-09T09:43:54.719813Z","shell.execute_reply.started":"2023-06-09T09:43:54.712053Z","shell.execute_reply":"2023-06-09T09:43:54.718412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tree -L 2 /kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:43:54.724448Z","iopub.execute_input":"2023-06-09T09:43:54.725170Z","iopub.status.idle":"2023-06-09T09:43:55.839993Z","shell.execute_reply.started":"2023-06-09T09:43:54.725130Z","shell.execute_reply":"2023-06-09T09:43:55.838535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dataframe with all tdcsfog data\ntdcsfog_data = create_full_data('tdcsfog')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:43:55.841740Z","iopub.execute_input":"2023-06-09T09:43:55.842846Z","iopub.status.idle":"2023-06-09T09:44:17.120937Z","shell.execute_reply.started":"2023-06-09T09:43:55.842805Z","shell.execute_reply":"2023-06-09T09:44:17.119693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Motion sensor data, possibly from a wearable device, with each row representing a particular moment in time (as indicated by the \"Time\" column) and containing measurements of acceleration in three dimensions (\"AccV\", \"AccML\", \"AccAP\"). The values in these columns indicate the acceleration of the device along the vertical, medial-lateral, and anterior-posterior axes, respectively.\n\nThe \"StartHesitation\", \"Turn\", and \"Walking\" columns appear to be binary variables indicating whether or not the corresponding activity is taking place at a given moment in time. However, in this table, all values in these columns are 0, so it is not possible to predict the status of these variables based on the data in this table alone","metadata":{}},{"cell_type":"code","source":"tdcsfog_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:17.122488Z","iopub.execute_input":"2023-06-09T09:44:17.122896Z","iopub.status.idle":"2023-06-09T09:44:17.156037Z","shell.execute_reply.started":"2023-06-09T09:44:17.122858Z","shell.execute_reply":"2023-06-09T09:44:17.155081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_data.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:17.157441Z","iopub.execute_input":"2023-06-09T09:44:17.157733Z","iopub.status.idle":"2023-06-09T09:44:18.938701Z","shell.execute_reply.started":"2023-06-09T09:44:17.157708Z","shell.execute_reply":"2023-06-09T09:44:18.937462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the mean value for \"AccV\" is -9.30, indicating that the device was tilted slightly downwards on average. The standard deviation of 1.39 for \"AccV\" suggests that the device orientation varied quite a bit over time. The maximum and minimum values for each variable provide an idea of the range of motion that was captured by the sensor over the course of the data collection period.\n\nHere, only Turn has the max value of 1. Thus, only a turn event occurred in this experiment.","metadata":{}},{"cell_type":"code","source":"# Create a dataframe with all defog data\ndefog_data = create_full_data('defog')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:18.939771Z","iopub.execute_input":"2023-06-09T09:44:18.940063Z","iopub.status.idle":"2023-06-09T09:44:45.489066Z","shell.execute_reply.started":"2023-06-09T09:44:18.940030Z","shell.execute_reply":"2023-06-09T09:44:45.487907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"defog_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:45.490436Z","iopub.execute_input":"2023-06-09T09:44:45.490764Z","iopub.status.idle":"2023-06-09T09:44:45.506357Z","shell.execute_reply.started":"2023-06-09T09:44:45.490737Z","shell.execute_reply":"2023-06-09T09:44:45.505543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_data_valid = defog_data.loc[ \\\n                        (defog_data.Valid == True) & (defog_data.Task == True)].copy()\n\ndefog_data_valid.reset_index(drop=True, inplace=True)\ndefog_data_valid.drop(['Valid', 'Task'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:45.507858Z","iopub.execute_input":"2023-06-09T09:44:45.508573Z","iopub.status.idle":"2023-06-09T09:44:47.266983Z","shell.execute_reply.started":"2023-06-09T09:44:45.508539Z","shell.execute_reply":"2023-06-09T09:44:47.265443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_data_valid","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:47.268785Z","iopub.execute_input":"2023-06-09T09:44:47.269255Z","iopub.status.idle":"2023-06-09T09:44:47.294585Z","shell.execute_reply.started":"2023-06-09T09:44:47.269215Z","shell.execute_reply":"2023-06-09T09:44:47.293259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data = pd.concat([tdcsfog_data, defog_data_valid])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:47.296474Z","iopub.execute_input":"2023-06-09T09:44:47.297117Z","iopub.status.idle":"2023-06-09T09:44:48.173137Z","shell.execute_reply.started":"2023-06-09T09:44:47.297081Z","shell.execute_reply":"2023-06-09T09:44:48.171833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:48.179070Z","iopub.execute_input":"2023-06-09T09:44:48.179466Z","iopub.status.idle":"2023-06-09T09:44:48.200138Z","shell.execute_reply.started":"2023-06-09T09:44:48.179431Z","shell.execute_reply":"2023-06-09T09:44:48.198604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA :: Exploratory Data Analysis  on the dataset","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n# Visualize \"AccV\", \"AccML\", and \"AccAP.\"\nsns.pairplot(full_data[['AccV', 'AccML', 'AccAP']])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:44:48.201695Z","iopub.execute_input":"2023-06-09T09:44:48.202459Z","iopub.status.idle":"2023-06-09T09:49:56.424055Z","shell.execute_reply.started":"2023-06-09T09:44:48.202404Z","shell.execute_reply":"2023-06-09T09:49:56.422772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# *full_data = full_data.merge(full_metadata, on='Id', how='inner') \\\n#                        .merge(tasks_data[['Id','t_kmeans']], how='left', on='Id').fillna(-1) \\\n#                        .merge(subjects_data.drop('Visit', axis=1),\n#                                       on='Subject', how='left').fillna(-1)\n# full_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:49:56.426300Z","iopub.execute_input":"2023-06-09T09:49:56.426733Z","iopub.status.idle":"2023-06-09T09:49:56.432098Z","shell.execute_reply.started":"2023-06-09T09:49:56.426698Z","shell.execute_reply":"2023-06-09T09:49:56.430772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the numbers by graph.\ndata = pd.DataFrame(\n    np.concatenate([\n        ['Total'] * len(full_data),\n        ['StartHesitation'] * int(np.ceil(len(full_data) / 2 * full_data['StartHesitation'].mean())),\n        ['Turn'] * int(np.ceil(len(full_data) / 2 * full_data['Turn'].mean())),\n        ['Walking'] * int(np.ceil(len(full_data) / 2 * full_data['Walking'].mean()))\n    ]),\n    columns = [\"The Number of 1\"]\n)\n\nsns.countplot(x = 'The Number of 1', data = data)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:49:56.434236Z","iopub.execute_input":"2023-06-09T09:49:56.435321Z","iopub.status.idle":"2023-06-09T09:50:18.163263Z","shell.execute_reply.started":"2023-06-09T09:49:56.435280Z","shell.execute_reply":"2023-06-09T09:50:18.162069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"######################################### TIME VS (StartHesitation,Turn,Walking) PLOT ###################\n# Create a figure and axis object.\nfig, ax = plt.subplots(figsize = (10, 6))\n\n# Plot the StartHesitation, Turn, and Walking columns against Time.\nax.plot(full_data['Time'], full_data['StartHesitation'], label = 'StartHesitation')\nax.plot(full_data['Time'], full_data['Turn'], label = 'Turn')\nax.plot(full_data['Time'], full_data['Walking'], label = 'Walking')\n\n# Add axis labels and title.\nax.set_xlabel('Time')\nax.set_ylabel('Binary Status')\nax.set_title('Relationship between Time and Movement Status')\n\n# Add a legend to the plot.\nax.legend()\n\n# Show the plot.\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:50:18.164763Z","iopub.execute_input":"2023-06-09T09:50:18.165105Z","iopub.status.idle":"2023-06-09T09:52:34.225832Z","shell.execute_reply.started":"2023-06-09T09:50:18.165074Z","shell.execute_reply":"2023-06-09T09:52:34.224566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the figure size.\nplt.figure(figsize = (12,6))\n\n# Calculates the mean values of all other columns for each unique value of Time.\n# Reset the index of the resulting dataframe.\nfull_data_means = full_data.groupby('Time').mean().reset_index()\n\n# Plot the mean StartHesitation values over time.\nplt.plot(full_data_means['Time'], full_data_means['StartHesitation'], label = 'StartHesitation')\n\n# Plot the mean Turn values over time.\nplt.plot(full_data_means['Time'], full_data_means['Turn'], label = 'Turn')\n\n# Plot the mean Walking values over time.\nplt.plot(full_data_means['Time'], full_data_means['Walking'], label = 'Walking')\n\n# Add a legend to the plot.\nplt.legend()\n\n# Set the x-label of the plot.\nplt.xlabel('Time')\n\n# Set the y-label of the plot.\nplt.ylabel('Mean Value')\n\n# Set the title of the plot.\nplt.title('Mean Values of StartHesitation, Turn, and Walking over Time')\n\n# Display the plot.\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:34.227293Z","iopub.execute_input":"2023-06-09T09:52:34.227652Z","iopub.status.idle":"2023-06-09T09:52:36.404062Z","shell.execute_reply.started":"2023-06-09T09:52:34.227621Z","shell.execute_reply":"2023-06-09T09:52:36.402784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Events data\nEvents data has 5 features and 3544 instances\n\n1). Id - 535 unique ids.\n\n2). Init- Time (s) the event began.\n\n3). Completion Time- (s) the event ended.\n\n4). Type- Three types: StartHesitation, Turn, or Walking.\n\n5). Kinetic- Whether the event was kinetic (1) and involved movement, or akinetic (0) and static.\n\nThe Fast_eda analysis of events data have the following finding:\n\nThe number of FoG events of 'Turn' type is higher than other types.\nThe number of 'Kinetic' events involving movement out number 'STATIC'.\nMissing values 30% (1045/3544) in both types and Kinetics columns","metadata":{}},{"cell_type":"code","source":"events_data.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:36.406195Z","iopub.execute_input":"2023-06-09T09:52:36.406637Z","iopub.status.idle":"2023-06-09T09:52:36.422645Z","shell.execute_reply.started":"2023-06-09T09:52:36.406597Z","shell.execute_reply":"2023-06-09T09:52:36.421029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fasteda import fast_eda\nfast_eda(events_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:36.424356Z","iopub.execute_input":"2023-06-09T09:52:36.425065Z","iopub.status.idle":"2023-06-09T09:52:40.822214Z","shell.execute_reply.started":"2023-06-09T09:52:36.425019Z","shell.execute_reply":"2023-06-09T09:52:40.821099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from phik.phik import phik_matrix\nfrom phik.report import plot_correlation_matrix","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:40.824042Z","iopub.execute_input":"2023-06-09T09:52:40.824750Z","iopub.status.idle":"2023-06-09T09:52:41.177838Z","shell.execute_reply.started":"2023-06-09T09:52:40.824701Z","shell.execute_reply":"2023-06-09T09:52:41.176464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects_data.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:41.179468Z","iopub.execute_input":"2023-06-09T09:52:41.184190Z","iopub.status.idle":"2023-06-09T09:52:41.194608Z","shell.execute_reply.started":"2023-06-09T09:52:41.184142Z","shell.execute_reply":"2023-06-09T09:52:41.193417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_eda(subjects_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:41.196510Z","iopub.execute_input":"2023-06-09T09:52:41.196869Z","iopub.status.idle":"2023-06-09T09:52:53.864653Z","shell.execute_reply.started":"2023-06-09T09:52:41.196838Z","shell.execute_reply":"2023-06-09T09:52:53.863400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tasks_data.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:53.866705Z","iopub.execute_input":"2023-06-09T09:52:53.867109Z","iopub.status.idle":"2023-06-09T09:52:53.875822Z","shell.execute_reply.started":"2023-06-09T09:52:53.867075Z","shell.execute_reply":"2023-06-09T09:52:53.874279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tasks_data.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:53.877163Z","iopub.execute_input":"2023-06-09T09:52:53.877536Z","iopub.status.idle":"2023-06-09T09:52:53.896745Z","shell.execute_reply.started":"2023-06-09T09:52:53.877506Z","shell.execute_reply":"2023-06-09T09:52:53.895614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_eda(tasks_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:53.898969Z","iopub.execute_input":"2023-06-09T09:52:53.899504Z","iopub.status.idle":"2023-06-09T09:52:56.465485Z","shell.execute_reply.started":"2023-06-09T09:52:53.899453Z","shell.execute_reply":"2023-06-09T09:52:56.464247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_eda(full_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T09:52:56.467339Z","iopub.execute_input":"2023-06-09T09:52:56.467806Z","iopub.status.idle":"2023-06-09T10:16:32.442079Z","shell.execute_reply.started":"2023-06-09T09:52:56.467765Z","shell.execute_reply":"2023-06-09T10:16:32.440142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Read 500 files of the tdcsfog metadata (the total number of files is 833)\ntdcsfog_files = [f'{data_dir}train/tdcsfog/{id}.csv' for id in \\\n                                         tdcsfog_metadata.Id.to_list()[:501]]\n\ntdcsfog_data = pd.concat([pd.read_csv(file) for file in tdcsfog_files])","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:16:32.444712Z","iopub.execute_input":"2023-06-09T10:16:32.445139Z","iopub.status.idle":"2023-06-09T10:16:43.283772Z","shell.execute_reply.started":"2023-06-09T10:16:32.445105Z","shell.execute_reply":"2023-06-09T10:16:43.282210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_data.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:16:43.286303Z","iopub.execute_input":"2023-06-09T10:16:43.286898Z","iopub.status.idle":"2023-06-09T10:16:46.731539Z","shell.execute_reply.started":"2023-06-09T10:16:43.286846Z","shell.execute_reply":"2023-06-09T10:16:46.730276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_eda(tdcsfog_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:25:28.611147Z","iopub.execute_input":"2023-06-09T10:25:28.611594Z","iopub.status.idle":"2023-06-09T10:34:08.249793Z","shell.execute_reply.started":"2023-06-09T10:25:28.611560Z","shell.execute_reply":"2023-06-09T10:34:08.248488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **MODEL CREATION**","metadata":{}},{"cell_type":"markdown","source":"*SPLITING THE FEATURE VARIABLE AND THE TARGET VARIABLE *\n*FEATURE VARIABLE i.e. \"Time\", \"AccV\", \"AccML\", and \"AccAP\" AND TARGET VARIABLE  i.e. \"StartHesitation\", \"Turn\", and \"Walking\")*","metadata":{}},{"cell_type":"code","source":"!pip install tsfresh","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:34:08.251989Z","iopub.execute_input":"2023-06-09T10:34:08.252331Z","iopub.status.idle":"2023-06-09T10:34:24.105359Z","shell.execute_reply.started":"2023-06-09T10:34:08.252298Z","shell.execute_reply":"2023-06-09T10:34:24.103288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pylab as plt\n\nfrom tsfresh import extract_features, extract_relevant_features, select_features\nfrom tsfresh.utilities.dataframe_functions import impute\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:34:30.085441Z","iopub.execute_input":"2023-06-09T10:34:30.086021Z","iopub.status.idle":"2023-06-09T10:34:30.102035Z","shell.execute_reply.started":"2023-06-09T10:34:30.085981Z","shell.execute_reply":"2023-06-09T10:34:30.100765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:34:30.103946Z","iopub.execute_input":"2023-06-09T10:34:30.104406Z","iopub.status.idle":"2023-06-09T10:34:30.115054Z","shell.execute_reply.started":"2023-06-09T10:34:30.104346Z","shell.execute_reply":"2023-06-09T10:34:30.114252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data","metadata":{"execution":{"iopub.status.busy":"2023-06-09T11:14:13.410234Z","iopub.execute_input":"2023-06-09T11:14:13.410839Z","iopub.status.idle":"2023-06-09T11:14:13.446831Z","shell.execute_reply.started":"2023-06-09T11:14:13.410804Z","shell.execute_reply":"2023-06-09T11:14:13.445854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.title('accelerometer reading')\nplt.plot(full_data.set_index(\"Time\").AccV)\nplt.plot(full_data.set_index(\"Time\").AccML)\nplt.plot(full_data.set_index(\"Time\").AccAP)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T11:14:16.438617Z","iopub.execute_input":"2023-06-09T11:14:16.439968Z","iopub.status.idle":"2023-06-09T11:14:25.685629Z","shell.execute_reply.started":"2023-06-09T11:14:16.439923Z","shell.execute_reply":"2023-06-09T11:14:25.684159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train and evaluate classifier\nFor later comparison, we train a decision tree on all features (without selection):","metadata":{}},{"cell_type":"markdown","source":"# DecisionTreeClassifier","metadata":{}},{"cell_type":"code","source":"X = tdcsfog_data[['AccV', 'AccML', 'AccAP']]\nstart = tdcsfog_data.StartHesitation\nturn = tdcsfog_data.Turn\nwalk = tdcsfog_data.Walking","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:34:30.116559Z","iopub.execute_input":"2023-06-09T10:34:30.117210Z","iopub.status.idle":"2023-06-09T10:34:30.154622Z","shell.execute_reply.started":"2023-06-09T10:34:30.117172Z","shell.execute_reply":"2023-06-09T10:34:30.153439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, start, stratify=start,\n                                                      test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:38:01.748730Z","iopub.execute_input":"2023-06-09T10:38:01.749235Z","iopub.status.idle":"2023-06-09T10:38:04.333850Z","shell.execute_reply.started":"2023-06-09T10:38:01.749200Z","shell.execute_reply":"2023-06-09T10:38:04.332316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier_full = DecisionTreeClassifier()\nclassifier_full.fit(X_train, y_train)\nprint(classification_report(y_test, classifier_full.predict(X_test)))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:38:07.583090Z","iopub.execute_input":"2023-06-09T10:38:07.583505Z","iopub.status.idle":"2023-06-09T10:39:10.847542Z","shell.execute_reply.started":"2023-06-09T10:38:07.583474Z","shell.execute_reply":"2023-06-09T10:39:10.845254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_filtered_multi = select_features(X_train, y_train,\n                                         multiclass=True,\n                                         n_significant=2\n                                         )\nX_train_filtered_multi.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:44:16.010637Z","iopub.execute_input":"2023-06-09T10:44:16.012350Z","iopub.status.idle":"2023-06-09T10:45:13.518250Z","shell.execute_reply.started":"2023-06-09T10:44:16.012272Z","shell.execute_reply":"2023-06-09T10:45:13.516634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier_selected_multi = DecisionTreeClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:48:42.247680Z","iopub.execute_input":"2023-06-09T10:48:42.249638Z","iopub.status.idle":"2023-06-09T10:48:42.255412Z","shell.execute_reply.started":"2023-06-09T10:48:42.249564Z","shell.execute_reply":"2023-06-09T10:48:42.254141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier_selected_multi.fit(X_train_filtered_multi, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:48:56.590460Z","iopub.execute_input":"2023-06-09T10:48:56.591000Z","iopub.status.idle":"2023-06-09T10:50:01.511013Z","shell.execute_reply.started":"2023-06-09T10:48:56.590961Z","shell.execute_reply":"2023-06-09T10:50:01.509642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_filtered_multi = X_test[X_train_filtered_multi.columns]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:50:01.513987Z","iopub.execute_input":"2023-06-09T10:50:01.514484Z","iopub.status.idle":"2023-06-09T10:50:01.529829Z","shell.execute_reply.started":"2023-06-09T10:50:01.514443Z","shell.execute_reply":"2023-06-09T10:50:01.528532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, classifier_selected_multi.predict(X_test_filtered_multi)))","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:50:01.531252Z","iopub.execute_input":"2023-06-09T10:50:01.531600Z","iopub.status.idle":"2023-06-09T10:50:03.142933Z","shell.execute_reply.started":"2023-06-09T10:50:01.531570Z","shell.execute_reply":"2023-06-09T10:50:03.141420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T10:59:15.569710Z","iopub.execute_input":"2023-06-09T10:59:15.570420Z","iopub.status.idle":"2023-06-09T10:59:17.481050Z","shell.execute_reply.started":"2023-06-09T10:59:15.570372Z","shell.execute_reply":"2023-06-09T10:59:17.479328Z"},"trusted":true},"execution_count":null,"outputs":[]}]}