{"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 \nimport pandas as pd \nimport os\nimport glob\nimport gc\nfrom tqdm.auto import tqdm \nfrom sklearn import *\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-16T05:51:45.064166Z","iopub.execute_input":"2023-04-16T05:51:45.064594Z","iopub.status.idle":"2023-04-16T05:51:45.101881Z","shell.execute_reply.started":"2023-04-16T05:51:45.064554Z","shell.execute_reply":"2023-04-16T05:51:45.099883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#understanding metadata\nmetadata_defog = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/defog_metadata.csv\")\nmetadata_defog.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:21:35.546657Z","iopub.execute_input":"2023-04-16T03:21:35.547106Z","iopub.status.idle":"2023-04-16T03:21:35.562615Z","shell.execute_reply.started":"2023-04-16T03:21:35.547067Z","shell.execute_reply":"2023-04-16T03:21:35.561615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_defog.describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:21:49.174796Z","iopub.execute_input":"2023-04-16T03:21:49.175811Z","iopub.status.idle":"2023-04-16T03:21:49.207109Z","shell.execute_reply.started":"2023-04-16T03:21:49.175766Z","shell.execute_reply":"2023-04-16T03:21:49.206019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check defog timeseries for 1 subject out of these 45 subjects\ndf_list=[]\npath='/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/'\nfilenames = glob.glob(os.path.join(path , \"*.csv\"))\nfor filename in filenames[0:1]:\n    print(filename)\n    for filename in filenames:\n        df= pd.read_csv(filename)\n        df_list.append(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:25:58.462650Z","iopub.execute_input":"2023-04-16T03:25:58.463083Z","iopub.status.idle":"2023-04-16T03:26:23.826409Z","shell.execute_reply.started":"2023-04-16T03:25:58.463044Z","shell.execute_reply":"2023-04-16T03:26:23.825330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_be9d33541d = df_list[0]","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:26:43.003125Z","iopub.execute_input":"2023-04-16T03:26:43.003528Z","iopub.status.idle":"2023-04-16T03:26:43.009151Z","shell.execute_reply.started":"2023-04-16T03:26:43.003490Z","shell.execute_reply":"2023-04-16T03:26:43.007856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##df_be9d33541d= pd.concat(df_list , ignore_index = True, axis =0)\ndf_be9d33541d.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:27:07.992590Z","iopub.execute_input":"2023-04-16T03:27:07.993017Z","iopub.status.idle":"2023-04-16T03:27:08.010372Z","shell.execute_reply.started":"2023-04-16T03:27:07.992978Z","shell.execute_reply":"2023-04-16T03:27:08.009314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_be9d33541d.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:27:26.847372Z","iopub.execute_input":"2023-04-16T03:27:26.848077Z","iopub.status.idle":"2023-04-16T03:27:26.855324Z","shell.execute_reply.started":"2023-04-16T03:27:26.848034Z","shell.execute_reply":"2023-04-16T03:27:26.854112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##ignore invalid records since these are not annotated\n\ndf_be9d33541d  = df_be9d33541d[(df_be9d33541d[\"Task\"] == True) & (df_be9d33541d[\"Valid\"] == True)]\ndf_be9d33541d.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:27:43.529124Z","iopub.execute_input":"2023-04-16T03:27:43.529514Z","iopub.status.idle":"2023-04-16T03:27:43.542435Z","shell.execute_reply.started":"2023-04-16T03:27:43.529480Z","shell.execute_reply":"2023-04-16T03:27:43.541267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_be9d33541d","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:27:58.104697Z","iopub.execute_input":"2023-04-16T03:27:58.105108Z","iopub.status.idle":"2023-04-16T03:27:58.124147Z","shell.execute_reply.started":"2023-04-16T03:27:58.105070Z","shell.execute_reply":"2023-04-16T03:27:58.123036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Turn --------> {df_be9d33541d[df_be9d33541d.Turn == 1].count()}\")    \nprint(f\"Walking --------> {df_be9d33541d[df_be9d33541d.Walking == 1].count()}\") \nprint(f\"StartHesitation --------> {df_be9d33541d[df_be9d33541d.StartHesitation == 1].count()}\") ","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:28:35.254041Z","iopub.execute_input":"2023-04-16T03:28:35.254442Z","iopub.status.idle":"2023-04-16T03:28:35.268623Z","shell.execute_reply.started":"2023-04-16T03:28:35.254408Z","shell.execute_reply":"2023-04-16T03:28:35.267342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_be9d33541d.describe()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:28:50.659202Z","iopub.execute_input":"2023-04-16T03:28:50.659630Z","iopub.status.idle":"2023-04-16T03:28:50.700519Z","shell.execute_reply.started":"2023-04-16T03:28:50.659594Z","shell.execute_reply":"2023-04-16T03:28:50.699185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Analyzing the 3 target classes \n\ndf_be9d33541d[['StartHesitation','Turn','Walking']].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:29:10.067391Z","iopub.execute_input":"2023-04-16T03:29:10.067833Z","iopub.status.idle":"2023-04-16T03:29:10.082525Z","shell.execute_reply.started":"2023-04-16T03:29:10.067787Z","shell.execute_reply":"2023-04-16T03:29:10.081396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#read events data for timeseries\n\ndf_events = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/events.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:29:37.307028Z","iopub.execute_input":"2023-04-16T03:29:37.308227Z","iopub.status.idle":"2023-04-16T03:29:37.324290Z","shell.execute_reply.started":"2023-04-16T03:29:37.308185Z","shell.execute_reply":"2023-04-16T03:29:37.323431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_events[df_events.Id =='be9d33541d']","metadata":{"execution":{"iopub.status.busy":"2023-04-16T03:29:54.932451Z","iopub.execute_input":"2023-04-16T03:29:54.932884Z","iopub.status.idle":"2023-04-16T03:29:54.951553Z","shell.execute_reply.started":"2023-04-16T03:29:54.932848Z","shell.execute_reply":"2023-04-16T03:29:54.950711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_example_df = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog/003f117e14.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:53:11.986563Z","iopub.execute_input":"2023-04-16T05:53:11.987108Z","iopub.status.idle":"2023-04-16T05:53:12.040411Z","shell.execute_reply.started":"2023-04-16T05:53:11.987054Z","shell.execute_reply":"2023-04-16T05:53:12.039342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_example_df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:53:27.553810Z","iopub.execute_input":"2023-04-16T05:53:27.555478Z","iopub.status.idle":"2023-04-16T05:53:27.606464Z","shell.execute_reply.started":"2023-04-16T05:53:27.555415Z","shell.execute_reply":"2023-04-16T05:53:27.605001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_example_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:54:27.935361Z","iopub.execute_input":"2023-04-16T05:54:27.935818Z","iopub.status.idle":"2023-04-16T05:54:27.964023Z","shell.execute_reply.started":"2023-04-16T05:54:27.935767Z","shell.execute_reply":"2023-04-16T05:54:27.962778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_example_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:54:53.076316Z","iopub.execute_input":"2023-04-16T05:54:53.076732Z","iopub.status.idle":"2023-04-16T05:54:53.118380Z","shell.execute_reply.started":"2023-04-16T05:54:53.076690Z","shell.execute_reply":"2023-04-16T05:54:53.117171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in ['AccV','AccML', 'AccAP']:\n    plt.plot(tdcsfog_example_df['Time'], tdcsfog_example_df[column], color='blue',)\n    plt.title(f\"{column} Time Series\", fontsize=12)\n    plt.xlabel('Time', fontsize=12)\n    plt.ylabel(f'{column}', fontsize=12)\n    plt.grid(True)\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:55:54.926652Z","iopub.execute_input":"2023-04-16T05:55:54.927106Z","iopub.status.idle":"2023-04-16T05:55:55.639793Z","shell.execute_reply.started":"2023-04-16T05:55:54.927063Z","shell.execute_reply":"2023-04-16T05:55:55.638195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_meta_df = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tdcsfog_metadata.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:56:40.131833Z","iopub.execute_input":"2023-04-16T05:56:40.132293Z","iopub.status.idle":"2023-04-16T05:56:40.148566Z","shell.execute_reply.started":"2023-04-16T05:56:40.132251Z","shell.execute_reply":"2023-04-16T05:56:40.147288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_meta_df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:56:56.841477Z","iopub.execute_input":"2023-04-16T05:56:56.841893Z","iopub.status.idle":"2023-04-16T05:56:56.864474Z","shell.execute_reply.started":"2023-04-16T05:56:56.841858Z","shell.execute_reply":"2023-04-16T05:56:56.862914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_meta_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:57:22.370604Z","iopub.execute_input":"2023-04-16T05:57:22.370999Z","iopub.status.idle":"2023-04-16T05:57:22.396616Z","shell.execute_reply.started":"2023-04-16T05:57:22.370964Z","shell.execute_reply":"2023-04-16T05:57:22.395561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcsfog_meta_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:57:41.370903Z","iopub.execute_input":"2023-04-16T05:57:41.372269Z","iopub.status.idle":"2023-04-16T05:57:41.387711Z","shell.execute_reply.started":"2023-04-16T05:57:41.372214Z","shell.execute_reply":"2023-04-16T05:57:41.385949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data = tdcsfog_meta_df, x = \"Test\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:58:22.684411Z","iopub.execute_input":"2023-04-16T05:58:22.684856Z","iopub.status.idle":"2023-04-16T05:58:22.932965Z","shell.execute_reply.started":"2023-04-16T05:58:22.684808Z","shell.execute_reply":"2023-04-16T05:58:22.931076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_metadata_df = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/daily_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:58:58.678216Z","iopub.execute_input":"2023-04-16T05:58:58.678627Z","iopub.status.idle":"2023-04-16T05:58:58.688924Z","shell.execute_reply.started":"2023-04-16T05:58:58.678590Z","shell.execute_reply":"2023-04-16T05:58:58.687683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_metadata_df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T05:59:07.569809Z","iopub.execute_input":"2023-04-16T05:59:07.570222Z","iopub.status.idle":"2023-04-16T05:59:07.589498Z","shell.execute_reply.started":"2023-04-16T05:59:07.570185Z","shell.execute_reply":"2023-04-16T05:59:07.587892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_df = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/events.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:11:18.990960Z","iopub.execute_input":"2023-04-16T06:11:18.992465Z","iopub.status.idle":"2023-04-16T06:11:19.006558Z","shell.execute_reply.started":"2023-04-16T06:11:18.992406Z","shell.execute_reply":"2023-04-16T06:11:19.004855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:11:34.826357Z","iopub.execute_input":"2023-04-16T06:11:34.827316Z","iopub.status.idle":"2023-04-16T06:11:34.850005Z","shell.execute_reply.started":"2023-04-16T06:11:34.827200Z","shell.execute_reply":"2023-04-16T06:11:34.848649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/subjects.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:12:19.057680Z","iopub.execute_input":"2023-04-16T06:12:19.058393Z","iopub.status.idle":"2023-04-16T06:12:19.074546Z","shell.execute_reply.started":"2023-04-16T06:12:19.058344Z","shell.execute_reply":"2023-04-16T06:12:19.073365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:12:42.231332Z","iopub.execute_input":"2023-04-16T06:12:42.231760Z","iopub.status.idle":"2023-04-16T06:12:42.258650Z","shell.execute_reply.started":"2023-04-16T06:12:42.231723Z","shell.execute_reply":"2023-04-16T06:12:42.257025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_metadata_df = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/defog_metadata.csv\")\n\ndefog_metadata_df['Type'] = \"defog\"\ntdcsfog_meta_df['Type'] = \"tdcsfog\"\n\nmetadata=pd.concat([defog_metadata_df,tdcsfog_meta_df])","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:13:47.131174Z","iopub.execute_input":"2023-04-16T06:13:47.131612Z","iopub.status.idle":"2023-04-16T06:13:47.149010Z","shell.execute_reply.started":"2023-04-16T06:13:47.131573Z","shell.execute_reply":"2023-04-16T06:13:47.147708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata['Medication'] = np.where(metadata['Medication'] != 'on', 0, 1)\nmetadata_new=metadata.merge(sub,how='left',on='Subject').copy()\nmetadata_new.fillna(0,inplace=True)\nmetadata_new['Sex'] = np.where(metadata_new['Sex'] != 'M', 0, 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:14:33.930026Z","iopub.execute_input":"2023-04-16T06:14:33.930433Z","iopub.status.idle":"2023-04-16T06:14:33.950382Z","shell.execute_reply.started":"2023-04-16T06:14:33.930399Z","shell.execute_reply":"2023-04-16T06:14:33.949296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bins = [0,10,20,30,40,50,60,70,80,90,100]\nlabels = [0,1,2,3,4,5,6,7,8,9]\nmetadata_new['Age_Category'] = pd.cut(metadata_new['Age'],bins,labels=labels)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:14:58.077025Z","iopub.execute_input":"2023-04-16T06:14:58.077568Z","iopub.status.idle":"2023-04-16T06:14:58.088301Z","shell.execute_reply.started":"2023-04-16T06:14:58.077524Z","shell.execute_reply":"2023-04-16T06:14:58.087139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bins = [0,5,10,15,20,25,30,35]\nlabels = [0,1,2,3,4,5,6]\nmetadata_new['YearsSinceDx_Category'] = pd.cut(metadata_new['YearsSinceDx'],bins,labels=labels)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:15:20.289823Z","iopub.execute_input":"2023-04-16T06:15:20.290300Z","iopub.status.idle":"2023-04-16T06:15:20.300260Z","shell.execute_reply.started":"2023-04-16T06:15:20.290257Z","shell.execute_reply":"2023-04-16T06:15:20.298512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bins = [0,5,10,15,20,25,30,35]\nlabels = [0,1,2,3,4,5,6]\nmetadata_new['YearsSinceDx_Category'] = pd.cut(metadata_new['YearsSinceDx'],bins,labels=labels)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:15:36.215421Z","iopub.execute_input":"2023-04-16T06:15:36.215856Z","iopub.status.idle":"2023-04-16T06:15:36.225485Z","shell.execute_reply.started":"2023-04-16T06:15:36.215815Z","shell.execute_reply":"2023-04-16T06:15:36.224109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_new = metadata_new.drop_duplicates()\nmetadata_new = metadata_new.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:15:56.600444Z","iopub.execute_input":"2023-04-16T06:15:56.600874Z","iopub.status.idle":"2023-04-16T06:15:56.618084Z","shell.execute_reply.started":"2023-04-16T06:15:56.600837Z","shell.execute_reply":"2023-04-16T06:15:56.616623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/\"\n\ntrain_defog = glob.glob(path+'train/defog/**')\ntrain_tdcsfog = glob.glob(path+'train/tdcsfog/**')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:16:14.978107Z","iopub.execute_input":"2023-04-16T06:16:14.979021Z","iopub.status.idle":"2023-04-16T06:16:14.993812Z","shell.execute_reply.started":"2023-04-16T06:16:14.978956Z","shell.execute_reply":"2023-04-16T06:16:14.991981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_data(f):\n    df = pd.read_csv(f)\n    df['Id'] = f.split('/')[-1].split('.')[0]\n    df['data_type'] = f.split('/')[-2]\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:16:27.740770Z","iopub.execute_input":"2023-04-16T06:16:27.741195Z","iopub.status.idle":"2023-04-16T06:16:27.748813Z","shell.execute_reply.started":"2023-04-16T06:16:27.741158Z","shell.execute_reply":"2023-04-16T06:16:27.747086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_defog = pd.concat([get_data(f) for f in train_defog])\ndf_train_tdcsfog = pd.concat([get_data(f) for f in train_tdcsfog])","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:16:46.277166Z","iopub.execute_input":"2023-04-16T06:16:46.277580Z","iopub.status.idle":"2023-04-16T06:17:34.553339Z","shell.execute_reply.started":"2023-04-16T06:16:46.277544Z","shell.execute_reply":"2023-04-16T06:17:34.551985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_time(df, freq):\n    if freq == 100:\n        df['Time'] /= 100\n        df['Time'] = round(df['Time'], 3)\n    elif freq == 128:\n        df['Time'] = round(df['Time'] / 128, )\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:18:14.322947Z","iopub.execute_input":"2023-04-16T06:18:14.323439Z","iopub.status.idle":"2023-04-16T06:18:14.331855Z","shell.execute_reply.started":"2023-04-16T06:18:14.323394Z","shell.execute_reply":"2023-04-16T06:18:14.330039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_defog = convert_time(df_train_defog, 100)\ndf_train_tdcsfog = convert_time(df_train_tdcsfog, 128)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:18:29.997894Z","iopub.execute_input":"2023-04-16T06:18:29.998478Z","iopub.status.idle":"2023-04-16T06:18:30.288806Z","shell.execute_reply.started":"2023-04-16T06:18:29.998429Z","shell.execute_reply":"2023-04-16T06:18:30.287152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_acc(df):\n    df['AccV'] *= 9.80665\n    df['AccML'] *= 9.80665\n    df['AccAP'] *= 9.80665\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:18:45.823904Z","iopub.execute_input":"2023-04-16T06:18:45.824378Z","iopub.status.idle":"2023-04-16T06:18:45.831660Z","shell.execute_reply.started":"2023-04-16T06:18:45.824332Z","shell.execute_reply":"2023-04-16T06:18:45.829888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_defog = convert_acc(df_train_defog)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:19:01.856641Z","iopub.execute_input":"2023-04-16T06:19:01.857044Z","iopub.status.idle":"2023-04-16T06:19:02.121967Z","shell.execute_reply.started":"2023-04-16T06:19:01.857007Z","shell.execute_reply":"2023-04-16T06:19:02.120467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.concat([df_train_defog,df_train_tdcsfog])","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:19:21.010166Z","iopub.execute_input":"2023-04-16T06:19:21.010589Z","iopub.status.idle":"2023-04-16T06:19:23.761209Z","shell.execute_reply.started":"2023-04-16T06:19:21.010554Z","shell.execute_reply":"2023-04-16T06:19:23.759894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:19:37.142603Z","iopub.execute_input":"2023-04-16T06:19:37.143012Z","iopub.status.idle":"2023-04-16T06:19:51.147984Z","shell.execute_reply.started":"2023-04-16T06:19:37.142976Z","shell.execute_reply":"2023-04-16T06:19:51.146573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_df = df.merge(metadata_new,how='left',on='Id').copy()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:20:14.091321Z","iopub.execute_input":"2023-04-16T06:20:14.091754Z","iopub.status.idle":"2023-04-16T06:20:44.496960Z","shell.execute_reply.started":"2023-04-16T06:20:14.091714Z","shell.execute_reply":"2023-04-16T06:20:44.495389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_df['data_type'] = np.where(combined_df['data_type'] != 'defog', 0, 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:21:04.577532Z","iopub.execute_input":"2023-04-16T06:21:04.578021Z","iopub.status.idle":"2023-04-16T06:21:04.836367Z","shell.execute_reply.started":"2023-04-16T06:21:04.577968Z","shell.execute_reply":"2023-04-16T06:21:04.834655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=['Time', 'AccV', 'AccML', 'AccAP', 'Age_Category', 'Sex', 'Medication', 'YearsSinceDx_Category', 'data_type']\nTargets=['StartHesitation', 'Turn' , 'Walking']","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:21:11.652051Z","iopub.execute_input":"2023-04-16T06:21:11.653200Z","iopub.status.idle":"2023-04-16T06:21:11.658527Z","shell.execute_reply.started":"2023-04-16T06:21:11.653154Z","shell.execute_reply":"2023-04-16T06:21:11.657332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = model_selection.train_test_split(combined_df[features], combined_df[Targets], test_size=.30, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:21:34.217267Z","iopub.execute_input":"2023-04-16T06:21:34.217713Z","iopub.status.idle":"2023-04-16T06:21:46.496259Z","shell.execute_reply.started":"2023-04-16T06:21:34.217674Z","shell.execute_reply":"2023-04-16T06:21:46.495009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf = ensemble.RandomForestRegressor(max_depth= 10, min_samples_leaf=1, min_samples_split=2, n_estimators=125, max_features='sqrt', random_state=42, n_jobs=-1)\nrf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T06:21:59.839964Z","iopub.execute_input":"2023-04-16T06:21:59.841226Z","iopub.status.idle":"2023-04-16T07:07:57.741581Z","shell.execute_reply.started":"2023-04-16T06:21:59.841169Z","shell.execute_reply":"2023-04-16T07:07:57.737977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importances = rf.feature_importances_\n\n# Plot feature importances\nplt.bar(X_train.columns, importances)\nplt.xticks(rotation=90)\nplt.ylabel('Importance')\nplt.xlabel('Feature')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:08:07.070488Z","iopub.execute_input":"2023-04-16T07:08:07.071109Z","iopub.status.idle":"2023-04-16T07:08:07.494268Z","shell.execute_reply.started":"2023-04-16T07:08:07.071040Z","shell.execute_reply":"2023-04-16T07:08:07.492745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = rf.predict(X_valid)\nprint(metrics.average_precision_score(y_valid, y_pred.clip(0.0,1.0)))","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:08:33.585698Z","iopub.execute_input":"2023-04-16T07:08:33.586112Z","iopub.status.idle":"2023-04-16T07:09:41.934143Z","shell.execute_reply.started":"2023-04-16T07:08:33.586076Z","shell.execute_reply":"2023-04-16T07:09:41.932760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df\ndel combined_df\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:11:20.141083Z","iopub.execute_input":"2023-04-16T07:11:20.141578Z","iopub.status.idle":"2023-04-16T07:11:21.760074Z","shell.execute_reply.started":"2023-04-16T07:11:20.141538Z","shell.execute_reply":"2023-04-16T07:11:21.758005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = glob.glob(path + 'test/**/**')\nsubmissions = []\nfor file in test_files:\n    test_df = pd.read_csv(file)\n    org_time = test_df[['Time']]\n    test_df = test_df.assign(Id=file.split('/')[-1].split('.')[0])\n    if 'defog' in file:\n        test_df = test_df.assign(data_type=1)\n        test_df = convert_time(test_df, 100)\n        test_df = convert_acc(test_df)\n    else:\n        test_df = test_df.assign(data_type=0)\n        test_df = convert_time(test_df, 128)\n    df_test = test_df.merge(metadata_new, how='left', on='Id').copy()\n    results = pd.DataFrame(np.round(rf.predict(df_test[features]),3), columns=Targets)\n    df_temp = df_test[['Id']]\n    org_time = org_time.reset_index(drop=True)\n    results = results.reset_index(drop=True)\n    sub = pd.concat([org_time, results], axis=1)\n    sub = pd.concat([df_temp, sub], axis=1)\n    sub['Id'] = sub['Id'].astype(str) + '_' + sub['Time'].astype(str)\n    submissions.append(sub[['Id','StartHesitation', 'Turn' , 'Walking']])\n    \nsubmission = pd.concat(submissions)\nsubmission[['Id','StartHesitation', 'Turn' , 'Walking']].to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:14:25.756194Z","iopub.execute_input":"2023-04-16T07:14:25.756685Z","iopub.status.idle":"2023-04-16T07:14:30.475296Z","shell.execute_reply.started":"2023-04-16T07:14:25.756643Z","shell.execute_reply":"2023-04-16T07:14:30.474281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:14:50.922108Z","iopub.execute_input":"2023-04-16T07:14:50.922523Z","iopub.status.idle":"2023-04-16T07:14:50.940947Z","shell.execute_reply.started":"2023-04-16T07:14:50.922487Z","shell.execute_reply":"2023-04-16T07:14:50.939603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/working/submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:15:08.412224Z","iopub.execute_input":"2023-04-16T07:15:08.412649Z","iopub.status.idle":"2023-04-16T07:15:08.765626Z","shell.execute_reply.started":"2023-04-16T07:15:08.412613Z","shell.execute_reply":"2023-04-16T07:15:08.764182Z"},"trusted":true},"execution_count":null,"outputs":[]}]}