{"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\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-06-09T03:12:47.145359Z","iopub.execute_input":"2023-06-09T03:12:47.145749Z","iopub.status.idle":"2023-06-09T03:12:47.413998Z","shell.execute_reply.started":"2023-06-09T03:12:47.145722Z","shell.execute_reply":"2023-06-09T03:12:47.413226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**IMPORTING THE LIBRARIES**","metadata":{"execution":{"iopub.status.busy":"2023-06-27T05:48:41.305122Z","iopub.execute_input":"2023-06-27T05:48:41.305514Z","iopub.status.idle":"2023-06-27T05:48:41.310652Z","shell.execute_reply.started":"2023-06-27T05:48:41.305485Z","shell.execute_reply":"2023-06-27T05:48:41.309415Z"}}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport re\nimport warnings\nimport os\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:47.415989Z","iopub.execute_input":"2023-06-09T03:12:47.417244Z","iopub.status.idle":"2023-06-09T03:12:48.086282Z","shell.execute_reply.started":"2023-06-09T03:12:47.417213Z","shell.execute_reply":"2023-06-09T03:12:48.085609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ss=pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/sample_submission.csv')\n#ss","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.087263Z","iopub.execute_input":"2023-06-09T03:12:48.087633Z","iopub.status.idle":"2023-06-09T03:12:48.090582Z","shell.execute_reply.started":"2023-06-09T03:12:48.087610Z","shell.execute_reply":"2023-06-09T03:12:48.089723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/sample_submission.csv\n#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/subjects.csv\n#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tasks.csv\n#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/defog_metadata.csv\n#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/daily_metadata.csv\n#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/events.csv\n#/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tdcsfog_metadata.csv","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.091530Z","iopub.execute_input":"2023-06-09T03:12:48.091779Z","iopub.status.idle":"2023-06-09T03:12:48.102987Z","shell.execute_reply.started":"2023-06-09T03:12:48.091760Z","shell.execute_reply":"2023-06-09T03:12:48.102313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DATA PRE-PROCESSING**","metadata":{}},{"cell_type":"code","source":"subjects = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/subjects.csv')\nsubjects","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.105143Z","iopub.execute_input":"2023-06-09T03:12:48.106012Z","iopub.status.idle":"2023-06-09T03:12:48.163213Z","shell.execute_reply.started":"2023-06-09T03:12:48.105986Z","shell.execute_reply":"2023-06-09T03:12:48.162477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(subjects).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.164537Z","iopub.execute_input":"2023-06-09T03:12:48.164803Z","iopub.status.idle":"2023-06-09T03:12:48.172858Z","shell.execute_reply.started":"2023-06-09T03:12:48.164781Z","shell.execute_reply":"2023-06-09T03:12:48.171894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_upoff = subjects['UPDRSIII_Off'].mean()\nmean_upoff","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.174175Z","iopub.execute_input":"2023-06-09T03:12:48.174561Z","iopub.status.idle":"2023-06-09T03:12:48.184039Z","shell.execute_reply.started":"2023-06-09T03:12:48.174540Z","shell.execute_reply":"2023-06-09T03:12:48.183052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_visit = subjects['Visit'].mean()\nmean_visit","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.185777Z","iopub.execute_input":"2023-06-09T03:12:48.186143Z","iopub.status.idle":"2023-06-09T03:12:48.195924Z","shell.execute_reply.started":"2023-06-09T03:12:48.186113Z","shell.execute_reply":"2023-06-09T03:12:48.194644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_upon = subjects['UPDRSIII_On'].mean()\nmean_upon","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.197591Z","iopub.execute_input":"2023-06-09T03:12:48.197927Z","iopub.status.idle":"2023-06-09T03:12:48.209281Z","shell.execute_reply.started":"2023-06-09T03:12:48.197900Z","shell.execute_reply":"2023-06-09T03:12:48.207779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects['UPDRSIII_Off']=subjects['UPDRSIII_Off'].fillna(mean_upoff)\nsubjects['Visit']=subjects['Visit'].fillna(mean_visit)\nsubjects['UPDRSIII_On']=subjects['UPDRSIII_On'].fillna(mean_upon)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.210775Z","iopub.execute_input":"2023-06-09T03:12:48.211081Z","iopub.status.idle":"2023-06-09T03:12:48.222802Z","shell.execute_reply.started":"2023-06-09T03:12:48.211057Z","shell.execute_reply":"2023-06-09T03:12:48.221531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(subjects).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.224068Z","iopub.execute_input":"2023-06-09T03:12:48.224407Z","iopub.status.idle":"2023-06-09T03:12:48.234780Z","shell.execute_reply.started":"2023-06-09T03:12:48.224382Z","shell.execute_reply":"2023-06-09T03:12:48.233916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.236304Z","iopub.execute_input":"2023-06-09T03:12:48.236592Z","iopub.status.idle":"2023-06-09T03:12:48.255697Z","shell.execute_reply.started":"2023-06-09T03:12:48.236567Z","shell.execute_reply":"2023-06-09T03:12:48.254526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tasks = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tasks.csv')\ntasks","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.258711Z","iopub.execute_input":"2023-06-09T03:12:48.258999Z","iopub.status.idle":"2023-06-09T03:12:48.284981Z","shell.execute_reply.started":"2023-06-09T03:12:48.258975Z","shell.execute_reply":"2023-06-09T03:12:48.283861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(tasks).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.291935Z","iopub.execute_input":"2023-06-09T03:12:48.292289Z","iopub.status.idle":"2023-06-09T03:12:48.301907Z","shell.execute_reply.started":"2023-06-09T03:12:48.292242Z","shell.execute_reply":"2023-06-09T03:12:48.300899Z"},"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')\ndefog_metadata","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.303531Z","iopub.execute_input":"2023-06-09T03:12:48.303805Z","iopub.status.idle":"2023-06-09T03:12:48.326573Z","shell.execute_reply.started":"2023-06-09T03:12:48.303782Z","shell.execute_reply":"2023-06-09T03:12:48.325368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(defog_metadata).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.327793Z","iopub.execute_input":"2023-06-09T03:12:48.328076Z","iopub.status.idle":"2023-06-09T03:12:48.337017Z","shell.execute_reply.started":"2023-06-09T03:12:48.328053Z","shell.execute_reply":"2023-06-09T03:12:48.336114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_metadata = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/daily_metadata.csv')\ndaily_metadata","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.338437Z","iopub.execute_input":"2023-06-09T03:12:48.338792Z","iopub.status.idle":"2023-06-09T03:12:48.361575Z","shell.execute_reply.started":"2023-06-09T03:12:48.338759Z","shell.execute_reply":"2023-06-09T03:12:48.360750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(daily_metadata).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.362741Z","iopub.execute_input":"2023-06-09T03:12:48.362996Z","iopub.status.idle":"2023-06-09T03:12:48.371928Z","shell.execute_reply.started":"2023-06-09T03:12:48.362971Z","shell.execute_reply":"2023-06-09T03:12:48.370756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/events.csv')\nevents","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.373043Z","iopub.execute_input":"2023-06-09T03:12:48.373407Z","iopub.status.idle":"2023-06-09T03:12:48.403048Z","shell.execute_reply.started":"2023-06-09T03:12:48.373383Z","shell.execute_reply":"2023-06-09T03:12:48.401804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(events).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.404789Z","iopub.execute_input":"2023-06-09T03:12:48.405085Z","iopub.status.idle":"2023-06-09T03:12:48.415659Z","shell.execute_reply.started":"2023-06-09T03:12:48.405054Z","shell.execute_reply":"2023-06-09T03:12:48.414630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.417119Z","iopub.execute_input":"2023-06-09T03:12:48.417627Z","iopub.status.idle":"2023-06-09T03:12:48.436595Z","shell.execute_reply.started":"2023-06-09T03:12:48.417589Z","shell.execute_reply":"2023-06-09T03:12:48.435213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events.values","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.438223Z","iopub.execute_input":"2023-06-09T03:12:48.438958Z","iopub.status.idle":"2023-06-09T03:12:48.449850Z","shell.execute_reply.started":"2023-06-09T03:12:48.438910Z","shell.execute_reply":"2023-06-09T03:12:48.448700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_fixed=pd.get_dummies(events['Type'])\nevents_fixed","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.450731Z","iopub.execute_input":"2023-06-09T03:12:48.451033Z","iopub.status.idle":"2023-06-09T03:12:48.467453Z","shell.execute_reply.started":"2023-06-09T03:12:48.451007Z","shell.execute_reply":"2023-06-09T03:12:48.466478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#events_fixeds = pd.get_dummies(events['Kinetic'])\n#events_fixeds\nkmean = events['Kinetic'].mean()\nkmean\nevents['Kinetic']=events['Kinetic'].fillna(kmean)\nevents","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.468597Z","iopub.execute_input":"2023-06-09T03:12:48.468897Z","iopub.status.idle":"2023-06-09T03:12:48.483795Z","shell.execute_reply.started":"2023-06-09T03:12:48.468875Z","shell.execute_reply":"2023-06-09T03:12:48.482371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events = events.drop(['Type'], axis=1)\nevents","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.485034Z","iopub.execute_input":"2023-06-09T03:12:48.485367Z","iopub.status.idle":"2023-06-09T03:12:48.504137Z","shell.execute_reply.started":"2023-06-09T03:12:48.485339Z","shell.execute_reply":"2023-06-09T03:12:48.502996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"events_actual = pd.concat((events,events_fixed), axis=1)\nevents_actual","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.505517Z","iopub.execute_input":"2023-06-09T03:12:48.505925Z","iopub.status.idle":"2023-06-09T03:12:48.528857Z","shell.execute_reply.started":"2023-06-09T03:12:48.505890Z","shell.execute_reply":"2023-06-09T03:12:48.527888Z"},"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')\ntdcsfog_metadata","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.529917Z","iopub.execute_input":"2023-06-09T03:12:48.530719Z","iopub.status.idle":"2023-06-09T03:12:48.555917Z","shell.execute_reply.started":"2023-06-09T03:12:48.530693Z","shell.execute_reply":"2023-06-09T03:12:48.554974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(tdcsfog_metadata).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.556901Z","iopub.execute_input":"2023-06-09T03:12:48.557227Z","iopub.status.idle":"2023-06-09T03:12:48.567460Z","shell.execute_reply.started":"2023-06-09T03:12:48.557198Z","shell.execute_reply":"2023-06-09T03:12:48.566329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.568800Z","iopub.execute_input":"2023-06-09T03:12:48.569092Z","iopub.status.idle":"2023-06-09T03:12:48.576726Z","shell.execute_reply.started":"2023-06-09T03:12:48.569066Z","shell.execute_reply":"2023-06-09T03:12:48.575536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/be9d33541d.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.578790Z","iopub.execute_input":"2023-06-09T03:12:48.579237Z","iopub.status.idle":"2023-06-09T03:12:48.750973Z","shell.execute_reply.started":"2023-06-09T03:12:48.579208Z","shell.execute_reply":"2023-06-09T03:12:48.749365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.754734Z","iopub.execute_input":"2023-06-09T03:12:48.755071Z","iopub.status.idle":"2023-06-09T03:12:48.773023Z","shell.execute_reply.started":"2023-06-09T03:12:48.755046Z","shell.execute_reply":"2023-06-09T03:12:48.771974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" train['Valid'].info()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.774910Z","iopub.execute_input":"2023-06-09T03:12:48.775230Z","iopub.status.idle":"2023-06-09T03:12:48.790783Z","shell.execute_reply.started":"2023-06-09T03:12:48.775200Z","shell.execute_reply":"2023-06-09T03:12:48.789724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(train).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.791741Z","iopub.execute_input":"2023-06-09T03:12:48.792042Z","iopub.status.idle":"2023-06-09T03:12:48.802018Z","shell.execute_reply.started":"2023-06-09T03:12:48.792014Z","shell.execute_reply":"2023-06-09T03:12:48.801283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**PREPARING THE DATASET TO TRAIN THE MODEL**","metadata":{}},{"cell_type":"code","source":"#Set the directory path to the folder containing the CSV files.\ntdcsfog_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog'\n\ntdcsfog_list = []\n\nfor file_name in os.listdir(tdcsfog_path):\n    if file_name.endswith('.csv'):\n        file_path = os.path.join(tdcsfog_path, file_name)\n        file = pd.read_csv(file_path)\n        tdcsfog_list.append(file)\n\n\ntdcsfog = pd.concat(tdcsfog_list, axis = 0)\n\n# Show the concatenated dataframe.\ntdcsfog = tdcsfog.reset_index(drop=True)\ntdcsfog","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:12:48.803280Z","iopub.execute_input":"2023-06-09T03:12:48.804284Z","iopub.status.idle":"2023-06-09T03:13:02.398107Z","shell.execute_reply.started":"2023-06-09T03:12:48.804225Z","shell.execute_reply":"2023-06-09T03:13:02.396731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog'\n\n# Initialize an empty list to store the dataframes.\ndefog_list = []\n\nfor file_name in os.listdir(defog_path):\n    if file_name.endswith('.csv'):\n        file_path = os.path.join(defog_path, file_name)\n        file = pd.read_csv(file_path)\n        defog_list.append(file)\n\ndefog = pd.concat(defog_list, axis = 0)\n\ndefog = defog.reset_index(drop=True)\ndefog","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:02.399429Z","iopub.execute_input":"2023-06-09T03:13:02.399786Z","iopub.status.idle":"2023-06-09T03:13:19.983063Z","shell.execute_reply.started":"2023-06-09T03:13:02.399754Z","shell.execute_reply":"2023-06-09T03:13:19.982179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog = defog[(defog['Task'] == 1) & (defog['Valid'] == 1)]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:19.984078Z","iopub.execute_input":"2023-06-09T03:13:19.984332Z","iopub.status.idle":"2023-06-09T03:13:20.157841Z","shell.execute_reply.started":"2023-06-09T03:13:19.984312Z","shell.execute_reply":"2023-06-09T03:13:20.157127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog = defog.iloc[:, :7]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.158890Z","iopub.execute_input":"2023-06-09T03:13:20.159167Z","iopub.status.idle":"2023-06-09T03:13:20.200229Z","shell.execute_reply.started":"2023-06-09T03:13:20.159146Z","shell.execute_reply":"2023-06-09T03:13:20.199285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"defog.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.201185Z","iopub.execute_input":"2023-06-09T03:13:20.201458Z","iopub.status.idle":"2023-06-09T03:13:20.712969Z","shell.execute_reply.started":"2023-06-09T03:13:20.201438Z","shell.execute_reply":"2023-06-09T03:13:20.711549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tdcsfog.reset_index(drop=True, inplace=True)\n#defog.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.714179Z","iopub.execute_input":"2023-06-09T03:13:20.714472Z","iopub.status.idle":"2023-06-09T03:13:20.721510Z","shell.execute_reply.started":"2023-06-09T03:13:20.714449Z","shell.execute_reply":"2023-06-09T03:13:20.720351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_actual = pd.concat([tdcsfog, defog], axis=0)\n#train_actual.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.723703Z","iopub.execute_input":"2023-06-09T03:13:20.724037Z","iopub.status.idle":"2023-06-09T03:13:20.893985Z","shell.execute_reply.started":"2023-06-09T03:13:20.724010Z","shell.execute_reply":"2023-06-09T03:13:20.893016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_actual = train_actual.drop(['Valid','Task'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.900528Z","iopub.execute_input":"2023-06-09T03:13:20.900894Z","iopub.status.idle":"2023-06-09T03:13:20.905007Z","shell.execute_reply.started":"2023-06-09T03:13:20.900864Z","shell.execute_reply":"2023-06-09T03:13:20.904161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.isnull(train_actual).sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.906335Z","iopub.execute_input":"2023-06-09T03:13:20.906865Z","iopub.status.idle":"2023-06-09T03:13:20.968543Z","shell.execute_reply.started":"2023-06-09T03:13:20.906826Z","shell.execute_reply":"2023-06-09T03:13:20.967752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_actual","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.969669Z","iopub.execute_input":"2023-06-09T03:13:20.970146Z","iopub.status.idle":"2023-06-09T03:13:20.983557Z","shell.execute_reply.started":"2023-06-09T03:13:20.970120Z","shell.execute_reply":"2023-06-09T03:13:20.982552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training=train_actual[:1000001]\ntraining","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:20.984759Z","iopub.execute_input":"2023-06-09T03:13:20.984973Z","iopub.status.idle":"2023-06-09T03:13:21.003771Z","shell.execute_reply.started":"2023-06-09T03:13:20.984953Z","shell.execute_reply":"2023-06-09T03:13:21.002710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_merged = train_actual.iloc[:, 0:4]  # input features\ny1 = training['StartHesitation']  # target variable for StartHesitation\ny2 = training['Turn']  # target variable for Turn\ny3 = training['Walking']  # target variable for Walking","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:21.004954Z","iopub.execute_input":"2023-06-09T03:13:21.005244Z","iopub.status.idle":"2023-06-09T03:13:21.091155Z","shell.execute_reply.started":"2023-06-09T03:13:21.005225Z","shell.execute_reply":"2023-06-09T03:13:21.089805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the positions of y1 where it equals 0.\ny1_zeros = np.where(y1 == 0)[0]\ny1_ones = np.where(y1 == 1)[0]\n\n# Choose the same number of samples with y1 == 1 as there are with y1 == 0.\nnum1_ones = (y1 == 1).sum()\nnp.random.seed(42)\ny1_zeros = np.random.choice(np.where(y1 == 0)[0], size = num1_ones, replace = False)\n\n# Combine the positions of y1 == 0 and y1 == 1.\ny1_balanced_idxs = np.sort(np.concatenate([y1_zeros, y1_ones]))\n\n# Use the balanced indices to get the corresponding rows of X and y1.\nX1_balanced = X_merged.iloc[y1_balanced_idxs, :]\ny1_balanced = y1.iloc[y1_balanced_idxs]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:21.093329Z","iopub.execute_input":"2023-06-09T03:13:21.094563Z","iopub.status.idle":"2023-06-09T03:13:21.735357Z","shell.execute_reply.started":"2023-06-09T03:13:21.094495Z","shell.execute_reply":"2023-06-09T03:13:21.734275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the positions of y2 where it equals 0.\ny2_zeros = np.where(y2 == 0)[0]\ny2_ones = np.where(y2 == 1)[0]\n\n# Choose the same number of samples with y2 == 1 as there are with y2 == 0.\nnum2_ones = (y2 == 1).sum()\nnp.random.seed(42)\ny2_zeros = np.random.choice(np.where(y2 == 0)[0], size = num2_ones, replace = False)\n\n# Combine the positions of y2 == 0 and y2 == 1.\ny2_bal = np.sort(np.concatenate([y2_zeros, y2_ones]))\n\n# Use the balanced indices to get the corresponding rows of X and y1.\nX2_balanced = X_merged.iloc[y2_bal, :]\ny2_balanced = y2.iloc[y2_bal]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:21.737140Z","iopub.execute_input":"2023-06-09T03:13:21.737491Z","iopub.status.idle":"2023-06-09T03:13:22.633505Z","shell.execute_reply.started":"2023-06-09T03:13:21.737463Z","shell.execute_reply":"2023-06-09T03:13:22.632182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the positions of y3 where it equals 0.\ny3_zeros = np.where(y3 == 0)[0]\ny3_ones = np.where(y3 == 1)[0]\n\n# Choose the same number of samples with y3 == 1 as there are with y3 == 0.\nnum3_ones = (y3 == 1).sum()\nnp.random.seed(42)\ny3_zeros = np.random.choice(np.where(y3 == 0)[0], size = num3_ones, replace = False)\n\n# Combine the positions of y3 == 0 and y3 == 1.\ny3_bal = np.sort(np.concatenate([y3_zeros, y3_ones]))\n\n# Use the balanced indices to get the corresponding rows of X and y3.\nX3_balanced = X_merged.iloc[y3_bal, :]\ny3_balanced = y3.iloc[y3_bal]","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:22.635158Z","iopub.execute_input":"2023-06-09T03:13:22.635955Z","iopub.status.idle":"2023-06-09T03:13:23.265077Z","shell.execute_reply.started":"2023-06-09T03:13:22.635917Z","shell.execute_reply":"2023-06-09T03:13:23.263812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX1_train, X1_test, y1_train, y1_test = train_test_split(X1_balanced, y1_balanced, test_size = 0.25, random_state = 42)\nX2_train, X2_test, y2_train, y2_test = train_test_split(X2_balanced, y2_balanced, test_size = 0.25, random_state = 42)\nX3_train, X3_test, y3_train, y3_test = train_test_split(X3_balanced, y3_balanced, test_size = 0.25, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:23.267303Z","iopub.execute_input":"2023-06-09T03:13:23.267955Z","iopub.status.idle":"2023-06-09T03:13:24.400572Z","shell.execute_reply.started":"2023-06-09T03:13:23.267923Z","shell.execute_reply":"2023-06-09T03:13:24.399914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**HYPERPARAMETER TUNING USING GRIDSEARCHCV**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\nfrom sklearn.ensemble import RandomForestRegressor","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Defining the parameter grid for hyperparameter tuning.\nparam_grid = {\n    'n_estimators': [100, 200, 300],\n    'max_depth': [8],\n    'n_jobs': [-1],\n    'random_state': [42]\n}\n# Create three separate Random Forest Regressor models.\nmodel1 = GridSearchCV(RandomForestRegressor(), param_grid, cv = 5)\nmodel2 = GridSearchCV(RandomForestRegressor(), param_grid, cv = 5)\nmodel3 = GridSearchCV(RandomForestRegressor(), param_grid, cv = 5)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-09T03:13:24.401816Z","iopub.execute_input":"2023-06-09T03:13:24.402224Z","iopub.status.idle":"2023-06-09T07:15:41.298460Z","shell.execute_reply.started":"2023-06-09T03:13:24.402201Z","shell.execute_reply":"2023-06-09T07:15:41.297315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the models on the training data.\nmodel1.fit(X1_train, y1_train)\nmodel2.fit(X2_train, y2_train)\nmodel3.fit(X3_train, y3_train)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**GETTING THE ACCURACY SCORES**","metadata":{}},{"cell_type":"code","source":"# Evaluate the models on the test data.\nprint('R2 for StartHesitation:', model1.best_score_)\nprint('R2 for Turn:', model2.best_score_)\nprint('R2 for Walking:', model3.best_score_)\n\n# Print the best parameters for each model\nprint('Best parameters for StartHesitation:', model1.best_params_)\nprint('Best parameters for Turn:', model2.best_params_)\nprint('Best parameters for Walking:', model3.best_params_)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**PREPARING THE TEST DATASET**","metadata":{}},{"cell_type":"code","source":"tdcsfog_test_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/tdcsfog'\ntdcsfog_test_list = []\n\nfor file_name in os.listdir(tdcsfog_test_path):\n    if file_name.endswith('.csv'):\n        file_path = os.path.join(tdcsfog_test_path, file_name)\n        file = pd.read_csv(file_path)\n        file['Id'] = file_name[:-4] + '_' + file['Time'].apply(str)\n        file.Time = file.Time / (len(file) - 1)\n        tdcsfog_test_list.append(file)\n\ntdcsfog_test = pd.concat(tdcsfog_test_list, axis = 0)\n\ntdcsfog_test","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:41.299893Z","iopub.execute_input":"2023-06-09T07:15:41.300198Z","iopub.status.idle":"2023-06-09T07:15:41.338701Z","shell.execute_reply.started":"2023-06-09T07:15:41.300163Z","shell.execute_reply":"2023-06-09T07:15:41.338021Z"},"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\nfor file_name in os.listdir(defog_test_path):\n    if file_name.endswith('.csv'):\n        file_path = os.path.join(defog_test_path, file_name)\n        file = pd.read_csv(file_path)\n        file['Id'] = file_name[:-4] + '_' + file['Time'].apply(str)\n        file.Time = file.Time / (len(file) - 1)\n        defog_test_list.append(file)\n\ndefog_test = pd.concat(defog_test_list, axis = 0)\ndefog_test = pd.concat(defog_test_list, axis = 0)\n\ndefog_test","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:41.339738Z","iopub.execute_input":"2023-06-09T07:15:41.340139Z","iopub.status.idle":"2023-06-09T07:15:41.697539Z","shell.execute_reply.started":"2023-06-09T07:15:41.340117Z","shell.execute_reply":"2023-06-09T07:15:41.696500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_actual = pd.concat([tdcsfog_test, defog_test], axis = 0).reset_index(drop = True)\ntest_actual","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:41.698851Z","iopub.execute_input":"2023-06-09T07:15:41.700437Z","iopub.status.idle":"2023-06-09T07:15:41.732407Z","shell.execute_reply.started":"2023-06-09T07:15:41.700409Z","shell.execute_reply":"2023-06-09T07:15:41.731217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**STANDARDIZATION & GETTING THE PREDICTIONS**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:41.735016Z","iopub.execute_input":"2023-06-09T07:15:41.735449Z","iopub.status.idle":"2023-06-09T07:15:41.741597Z","shell.execute_reply.started":"2023-06-09T07:15:41.735421Z","shell.execute_reply":"2023-06-09T07:15:41.740327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = test_actual.iloc[:, 0:4]\n\n# Standardize the independent variables by a new scaler.\nscaler = StandardScaler()\ntest_X = scaler.fit_transform(test_X)\n\n# Get the predictions for the three models on the test data.\npred_y1 = model1.predict(test_X)\npred_y2 = model2.predict(test_X)\npred_y3 = model3.predict(test_X)\n\ntest_actual['StartHesitation'] = pred_y1 # target variable for StartHesitation\ntest_actual['Turn'] = pred_y2 # target variable for Turn\ntest_actual['Walking'] = pred_y3 # target variable for Walking\n\ntest_actual","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:41.743879Z","iopub.execute_input":"2023-06-09T07:15:41.744290Z","iopub.status.idle":"2023-06-09T07:15:42.785716Z","shell.execute_reply.started":"2023-06-09T07:15:41.744238Z","shell.execute_reply":"2023-06-09T07:15:42.784903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = test_actual.iloc[:, 4:].fillna(0.0)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:42.786893Z","iopub.execute_input":"2023-06-09T07:15:42.787169Z","iopub.status.idle":"2023-06-09T07:15:42.854473Z","shell.execute_reply.started":"2023-06-09T07:15:42.787143Z","shell.execute_reply":"2023-06-09T07:15:42.853549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:42.855648Z","iopub.execute_input":"2023-06-09T07:15:42.855875Z","iopub.status.idle":"2023-06-09T07:15:43.940494Z","shell.execute_reply.started":"2023-06-09T07:15:42.855856Z","shell.execute_reply":"2023-06-09T07:15:43.939672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.tree import DecisionTreeClassifier\n#rom sklearn.ensemble import RandomForestClassifier\n#from sklearn.model_selection import train_test_split\n#from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n#from sklearn.pipeline import Pipeline\n#from sklearn.compose import ColumnTransformer\n#from sklearn.impute import SimpleImputer\n#from sklearn.preprocessing import StandardScaler, OneHotEncoder\n#from xgboost import XGBClassifier","metadata":{"execution":{"iopub.status.busy":"2023-06-09T07:15:43.941342Z","iopub.execute_input":"2023-06-09T07:15:43.941556Z","iopub.status.idle":"2023-06-09T07:15:43.947425Z","shell.execute_reply.started":"2023-06-09T07:15:43.941537Z","shell.execute_reply":"2023-06-09T07:15:43.946009Z"},"trusted":true},"execution_count":null,"outputs":[]}]}