{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":41880,"databundleVersionId":5677426,"sourceType":"competition"}],"dockerImageVersionId":30823,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true,"execution":{"iopub.status.busy":"2024-12-29T12:04:07.658143Z","iopub.execute_input":"2024-12-29T12:04:07.658518Z","iopub.status.idle":"2024-12-29T12:04:09.415960Z","shell.execute_reply.started":"2024-12-29T12:04:07.658487Z","shell.execute_reply":"2024-12-29T12:04:09.410591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport cv2\nimport pandas as pd\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:36:52.351116Z","iopub.execute_input":"2024-12-29T13:36:52.351568Z","iopub.status.idle":"2024-12-29T13:36:52.635523Z","shell.execute_reply.started":"2024-12-29T13:36:52.351525Z","shell.execute_reply":"2024-12-29T13:36:52.634845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reduce Memory Usage\n# reference : https://www.kaggle.com/code/arjanso/reducing-dataframe-memory-size-by-65 @ARJANGROEN\n\ndef reduce_memory_usage(df):\n    \n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype.name\n        if ((col_type != 'datetime64[ns]') & (col_type != 'category')):\n            if (col_type != 'object'):\n                c_min = df[col].min()\n                c_max = df[col].max()\n                if str(col_type)[:3] == 'int':\n                    if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                        df[col] = df[col].astype(np.int8)\n                    elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                        df[col] = df[col].astype(np.int16)\n                    elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                        df[col] = df[col].astype(np.int32)\n                    elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                        df[col] = df[col].astype(np.int64)\n\n                else:\n                    if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                        df[col] = df[col].astype(np.float16)\n                    elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                        df[col] = df[col].astype(np.float32)\n                    else:\n                        pass\n            else:\n                df[col] = df[col].astype('category')\n    mem_usg = df.memory_usage().sum() / 1024**2 \n    print(\"Memory usage became: \",mem_usg,\" MB\")\n    \n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:36:54.100856Z","iopub.execute_input":"2024-12-29T13:36:54.101184Z","iopub.status.idle":"2024-12-29T13:36:54.109097Z","shell.execute_reply.started":"2024-12-29T13:36:54.101153Z","shell.execute_reply":"2024-12-29T13:36:54.108208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#reference: https://www.kaggle.com/code/ghrangel/read-data-and-merge\n\nDATA_ROOT_DEFOG = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/'\ndefog = pd.DataFrame()\nfor root, dirs, files in os.walk(DATA_ROOT_DEFOG):\n    for name in files:       \n        f = os.path.join(root, name)\n        df_list= pd.read_csv(f)\n        words = name.split('.')[0]\n        df_list['file']= name.split('.')[0]\n        defog = pd.concat([defog, df_list], axis=0)\n\ndefog","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:36:56.614769Z","iopub.execute_input":"2024-12-29T13:36:56.615061Z","iopub.status.idle":"2024-12-29T13:37:42.474852Z","shell.execute_reply.started":"2024-12-29T13:36:56.615039Z","shell.execute_reply":"2024-12-29T13:37:42.473816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"defog = reduce_memory_usage(defog)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:37:47.801347Z","iopub.execute_input":"2024-12-29T13:37:47.801698Z","iopub.status.idle":"2024-12-29T13:37:49.463214Z","shell.execute_reply.started":"2024-12-29T13:37:47.801671Z","shell.execute_reply":"2024-12-29T13:37:49.462208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"defog = defog[(defog['Task']==1)&(defog['Valid']==1)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:37:51.489651Z","iopub.execute_input":"2024-12-29T13:37:51.489989Z","iopub.status.idle":"2024-12-29T13:37:51.783284Z","shell.execute_reply.started":"2024-12-29T13:37:51.489957Z","shell.execute_reply":"2024-12-29T13:37:51.782264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('the shape of defog dataset is {}'.format(defog.shape))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:37:53.660088Z","iopub.execute_input":"2024-12-29T13:37:53.660465Z","iopub.status.idle":"2024-12-29T13:37:53.665171Z","shell.execute_reply.started":"2024-12-29T13:37:53.660419Z","shell.execute_reply":"2024-12-29T13:37:53.664161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"defog_metadata = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/defog_metadata.csv\")\ndefog_metadata","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:37:57.165848Z","iopub.execute_input":"2024-12-29T13:37:57.166137Z","iopub.status.idle":"2024-12-29T13:37:57.183277Z","shell.execute_reply.started":"2024-12-29T13:37:57.166115Z","shell.execute_reply":"2024-12-29T13:37:57.182466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"defog_m= defog_metadata.merge(defog, how = 'inner', left_on = 'Id', right_on = 'file')\ndefog_m.drop(['file','Valid','Task'], axis = 1, inplace = True)\ndefog_m","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:37:59.822740Z","iopub.execute_input":"2024-12-29T13:37:59.823067Z","iopub.status.idle":"2024-12-29T13:38:01.307851Z","shell.execute_reply.started":"2024-12-29T13:37:59.823036Z","shell.execute_reply":"2024-12-29T13:38:01.306969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# summary table function\ndef summary(df):\n    print(f'data shape: {df.shape}')\n    summ = pd.DataFrame(df.dtypes, columns=['data type'])\n    summ['#missing'] = df.isnull().sum().values * 100\n    summ['%missing'] = df.isnull().sum().values / len(df)\n    summ['#unique'] = df.nunique().values\n    desc = pd.DataFrame(df.describe(include='all').transpose())\n    summ['min'] = desc['min'].values\n    summ['max'] = desc['max'].values\n    summ['first value'] = df.loc[0].values\n    summ['second value'] = df.loc[1].values\n    summ['third value'] = df.loc[2].values\n    \n    return summ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:38:05.099584Z","iopub.execute_input":"2024-12-29T13:38:05.099866Z","iopub.status.idle":"2024-12-29T13:38:05.105064Z","shell.execute_reply.started":"2024-12-29T13:38:05.099844Z","shell.execute_reply":"2024-12-29T13:38:05.104112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"summary(defog_m)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:38:08.791504Z","iopub.execute_input":"2024-12-29T13:38:08.791835Z","iopub.status.idle":"2024-12-29T13:38:12.809311Z","shell.execute_reply.started":"2024-12-29T13:38:08.791806Z","shell.execute_reply":"2024-12-29T13:38:12.808410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# garbage collection for memory\nimport gc\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:38:18.211688Z","iopub.execute_input":"2024-12-29T13:38:18.211983Z","iopub.status.idle":"2024-12-29T13:38:18.327341Z","shell.execute_reply.started":"2024-12-29T13:38:18.211962Z","shell.execute_reply":"2024-12-29T13:38:18.326462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_ROOT_TDCSFOG = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog/'\ntdcsfog = pd.DataFrame()\nfor root, dirs, files in os.walk(DATA_ROOT_TDCSFOG):\n    for name in files:       \n        f = os.path.join(root, name)\n        df_list= pd.read_csv(f)\n        words = name.split('.')[0]\n        df_list['file']= name.split('.')[0]\n        tdcsfog = pd.concat([tdcsfog, df_list], axis=0)\ntdcsfog","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:38:21.209699Z","iopub.execute_input":"2024-12-29T13:38:21.210033Z","iopub.status.idle":"2024-12-29T13:40:06.032818Z","shell.execute_reply.started":"2024-12-29T13:38:21.210005Z","shell.execute_reply":"2024-12-29T13:40:06.032029Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tdcsfog = reduce_memory_usage(tdcsfog)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:23.402052Z","iopub.execute_input":"2024-12-29T13:40:23.402427Z","iopub.status.idle":"2024-12-29T13:40:24.153621Z","shell.execute_reply.started":"2024-12-29T13:40:23.402387Z","shell.execute_reply":"2024-12-29T13:40:24.152817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tdcsfog_metadata = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/tdcsfog_metadata.csv\")\ntdcsfog_metadata","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:26.195335Z","iopub.execute_input":"2024-12-29T13:40:26.195724Z","iopub.status.idle":"2024-12-29T13:40:26.215901Z","shell.execute_reply.started":"2024-12-29T13:40:26.195696Z","shell.execute_reply":"2024-12-29T13:40:26.215176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tdcsfog_m= tdcsfog_metadata.merge(tdcsfog, how = 'inner', left_on = 'Id', right_on = 'file')\ntdcsfog_m.drop(['file'], axis = 1, inplace = True)\ntdcsfog_m","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:28.958087Z","iopub.execute_input":"2024-12-29T13:40:28.958442Z","iopub.status.idle":"2024-12-29T13:40:31.577096Z","shell.execute_reply.started":"2024-12-29T13:40:28.958411Z","shell.execute_reply":"2024-12-29T13:40:31.576162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# garbage collection for memory\nimport gc\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:34.400667Z","iopub.execute_input":"2024-12-29T13:40:34.400999Z","iopub.status.idle":"2024-12-29T13:40:34.515798Z","shell.execute_reply.started":"2024-12-29T13:40:34.400970Z","shell.execute_reply":"2024-12-29T13:40:34.515066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import KFold, StratifiedKFold, train_test_split, GridSearchCV\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:36.344783Z","iopub.execute_input":"2024-12-29T13:40:36.345063Z","iopub.status.idle":"2024-12-29T13:40:36.348866Z","shell.execute_reply.started":"2024-12-29T13:40:36.345040Z","shell.execute_reply":"2024-12-29T13:40:36.348010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conditions = [\n    (defog_m['StartHesitation'] == 1),\n    (defog_m['Turn'] == 1),\n    (defog_m['Walking'] == 1)]\nchoices = ['StartHesitation', 'Turn', 'Walking']\ndefog_m['event'] = np.select(conditions, choices, default='Normal')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:38.408624Z","iopub.execute_input":"2024-12-29T13:40:38.408920Z","iopub.status.idle":"2024-12-29T13:40:39.249222Z","shell.execute_reply.started":"2024-12-29T13:40:38.408896Z","shell.execute_reply":"2024-12-29T13:40:39.248617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"defog_m['event'].value_counts().to_frame().style.background_gradient()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:41.427511Z","iopub.execute_input":"2024-12-29T13:40:41.427792Z","iopub.status.idle":"2024-12-29T13:40:41.776181Z","shell.execute_reply.started":"2024-12-29T13:40:41.427770Z","shell.execute_reply":"2024-12-29T13:40:41.775442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = defog_m[['AccV','AccML','AccAP','event']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:43.877821Z","iopub.execute_input":"2024-12-29T13:40:43.878100Z","iopub.status.idle":"2024-12-29T13:40:43.943129Z","shell.execute_reply.started":"2024-12-29T13:40:43.878078Z","shell.execute_reply":"2024-12-29T13:40:43.942464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\n\ntrain_df['target'] = le.fit_transform(train_df['event'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:46.395194Z","iopub.execute_input":"2024-12-29T13:40:46.395638Z","iopub.status.idle":"2024-12-29T13:40:47.027769Z","shell.execute_reply.started":"2024-12-29T13:40:46.395600Z","shell.execute_reply":"2024-12-29T13:40:47.027075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_df.drop(['event','target'], axis=1)\ny = train_df['target']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:48.741921Z","iopub.execute_input":"2024-12-29T13:40:48.742211Z","iopub.status.idle":"2024-12-29T13:40:48.761058Z","shell.execute_reply.started":"2024-12-29T13:40:48.742180Z","shell.execute_reply":"2024-12-29T13:40:48.760120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\n!pip install --upgrade lightgbm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:40:51.606100Z","iopub.execute_input":"2024-12-29T13:40:51.606435Z","iopub.status.idle":"2024-12-29T13:40:56.196947Z","shell.execute_reply.started":"2024-12-29T13:40:51.606405Z","shell.execute_reply":"2024-12-29T13:40:56.195840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\n\n# Split dataset into training and test set\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1004)\n\n# Converting the dataset to the proper LGB format\nd_train = lgb.Dataset(X_train, label=y_train)\n\n# Setting up the parameters for GPU usage\nparams = {\n    'learning_rate': 0.03,\n    'boosting_type': 'gbdt',  # GradientBoostingDecisionTree\n    'objective': 'multiclass',  # Multi-class target feature\n    'metric': 'multi_logloss',  # Metric for multi-class\n    'max_depth': 7,\n    'num_class': 4,  # No. of unique values in the target class (excluding the end value)\n    'verbose': -1,\n    'device': 'gpu',  # Use GPU for training\n    'gpu_use_dp': True  # Optional, for using double precision on GPU\n}\n\n# Training the model with GPU enabled\nclf = lgb.train(params, d_train, num_boost_round=1000)  # 1000 epochs, show progress every 100 iterations\n\n# Prediction on the test dataset\ny_pred_1 = clf.predict(X_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:54:03.954580Z","iopub.execute_input":"2024-12-29T13:54:03.954919Z","iopub.status.idle":"2024-12-29T14:00:41.279237Z","shell.execute_reply.started":"2024-12-29T13:54:03.954890Z","shell.execute_reply":"2024-12-29T14:00:41.278490Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_1[:1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:00:49.990709Z","iopub.execute_input":"2024-12-29T14:00:49.991017Z","iopub.status.idle":"2024-12-29T14:00:49.996430Z","shell.execute_reply.started":"2024-12-29T14:00:49.990993Z","shell.execute_reply":"2024-12-29T14:00:49.995691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 'macro' option is to calculate metrics for each label, and find their unweighted mean. \n# This does not take label imbalance into account.\nfrom sklearn.metrics import precision_score\nprecision_score(y_test, np.argmax(y_pred_1, axis=-1), average='macro')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:00:53.352087Z","iopub.execute_input":"2024-12-29T14:00:53.352414Z","iopub.status.idle":"2024-12-29T14:00:53.786584Z","shell.execute_reply.started":"2024-12-29T14:00:53.352384Z","shell.execute_reply":"2024-12-29T14:00:53.785688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_defog_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/defog/02ab235146.csv'\ntest_defog = pd.read_csv(test_defog_path)\nname = os.path.basename(test_defog_path)\nid_value = name.split('.')[0]\ntest_defog['Id_value'] = id_value\ntest_defog['Id'] = test_defog['Id_value'].astype(str) + '_' + test_defog['Time'].astype(str)\ntest_defog = test_defog[['Id','AccV','AccML','AccAP']]\ntest_defog.set_index('Id',inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:03:23.569120Z","iopub.execute_input":"2024-12-29T14:03:23.569504Z","iopub.status.idle":"2024-12-29T14:03:24.133027Z","shell.execute_reply.started":"2024-12-29T14:03:23.569473Z","shell.execute_reply":"2024-12-29T14:03:24.132356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# predict event probability\ntest_defog_pred=clf.predict(test_defog)\ntest_defog['event'] = np.argmax(test_defog_pred, axis=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:03:33.043895Z","iopub.execute_input":"2024-12-29T14:03:33.044268Z","iopub.status.idle":"2024-12-29T14:04:04.378621Z","shell.execute_reply.started":"2024-12-29T14:03:33.044222Z","shell.execute_reply":"2024-12-29T14:04:04.377859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# expand event column it to three columns\ntest_defog['StartHesitation'] = np.where(test_defog['event']==1, 1, 0)\ntest_defog['Turn'] = np.where(test_defog['event']==2, 1, 0)\ntest_defog['Walking'] = np.where(test_defog['event']==3, 1, 0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:04:17.056097Z","iopub.execute_input":"2024-12-29T14:04:17.056446Z","iopub.status.idle":"2024-12-29T14:04:17.065196Z","shell.execute_reply.started":"2024-12-29T14:04:17.056406Z","shell.execute_reply":"2024-12-29T14:04:17.064404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_defog.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:04:27.754543Z","iopub.execute_input":"2024-12-29T14:04:27.754852Z","iopub.status.idle":"2024-12-29T14:04:27.765544Z","shell.execute_reply.started":"2024-12-29T14:04:27.754828Z","shell.execute_reply":"2024-12-29T14:04:27.764729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conditions = [\n    (tdcsfog_m['StartHesitation'] == 1),\n    (tdcsfog_m['Turn'] == 1),\n    (tdcsfog_m['Walking'] == 1)]\nchoices = ['StartHesitation', 'Turn', 'Walking']\ntdcsfog_m['event'] = np.select(conditions, choices, default='Normal')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:06:44.554011Z","iopub.execute_input":"2024-12-29T14:06:44.554333Z","iopub.status.idle":"2024-12-29T14:06:46.040976Z","shell.execute_reply.started":"2024-12-29T14:06:44.554310Z","shell.execute_reply":"2024-12-29T14:06:46.040155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tdcsfog_m['event'].value_counts().to_frame().style.background_gradient()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:07:01.256252Z","iopub.execute_input":"2024-12-29T14:07:01.256692Z","iopub.status.idle":"2024-12-29T14:07:01.802441Z","shell.execute_reply.started":"2024-12-29T14:07:01.256644Z","shell.execute_reply":"2024-12-29T14:07:01.801599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = defog_m[['AccV','AccML','AccAP','event']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:07:33.729125Z","iopub.execute_input":"2024-12-29T14:07:33.729512Z","iopub.status.idle":"2024-12-29T14:07:33.913752Z","shell.execute_reply.started":"2024-12-29T14:07:33.729479Z","shell.execute_reply":"2024-12-29T14:07:33.912818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\n\ntrain_df['target'] = le.fit_transform(train_df['event'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:07:54.542722Z","iopub.execute_input":"2024-12-29T14:07:54.543049Z","iopub.status.idle":"2024-12-29T14:07:55.151210Z","shell.execute_reply.started":"2024-12-29T14:07:54.543022Z","shell.execute_reply":"2024-12-29T14:07:55.150316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_df.drop(['event','target'], axis=1)\ny = train_df['target']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:08:07.195825Z","iopub.execute_input":"2024-12-29T14:08:07.196280Z","iopub.status.idle":"2024-12-29T14:08:07.216290Z","shell.execute_reply.started":"2024-12-29T14:08:07.196239Z","shell.execute_reply":"2024-12-29T14:08:07.215455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\n\n# Split dataset into training and test set\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1004)\n\n# Converting the dataset to the proper LGB format\nd_train = lgb.Dataset(X_train, label=y_train)\n\n# Setting up the parameters for GPU usage\nparams = {\n    'learning_rate': 0.03,\n    'boosting_type': 'gbdt',  # GradientBoostingDecisionTree\n    'objective': 'multiclass',  # Multi-class target feature\n    'metric': 'multi_logloss',  # Metric for multi-class\n    'max_depth': 7,\n    'num_class': 4,  # No. of unique values in the target class (excluding the end value)\n    'verbose': -1,\n    'device': 'gpu',  # Use GPU for training\n    'gpu_use_dp': True  # Optional, for using double precision on GPU\n}\n\n# Training the model with GPU enabled\nclf = lgb.train(params, d_train, num_boost_round=1000)  # 1000 epochs, show progress every 100 iterations\n\n# Prediction on the test dataset\ny_pred_1 = clf.predict(X_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:08:26.102612Z","iopub.execute_input":"2024-12-29T14:08:26.102908Z","iopub.status.idle":"2024-12-29T14:14:55.368903Z","shell.execute_reply.started":"2024-12-29T14:08:26.102884Z","shell.execute_reply":"2024-12-29T14:14:55.368187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_1[:1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:20:03.013190Z","iopub.execute_input":"2024-12-29T14:20:03.013602Z","iopub.status.idle":"2024-12-29T14:20:03.018972Z","shell.execute_reply.started":"2024-12-29T14:20:03.013570Z","shell.execute_reply":"2024-12-29T14:20:03.018040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 'macro' option is to calculate metrics for each label, and find their unweighted mean. \n# This does not take label imbalance into account.\nfrom sklearn.metrics import precision_score\nprecision_score(y_test, np.argmax(y_pred_1, axis=-1), average='macro')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:20:25.522873Z","iopub.execute_input":"2024-12-29T14:20:25.523171Z","iopub.status.idle":"2024-12-29T14:20:25.959062Z","shell.execute_reply.started":"2024-12-29T14:20:25.523149Z","shell.execute_reply":"2024-12-29T14:20:25.958305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_tdcsfog_path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/tdcsfog/003f117e14.csv'\ntest_tdcsfog = pd.read_csv(test_tdcsfog_path)\nname = os.path.basename(test_tdcsfog_path)\nid_value = name.split('.')[0]\ntest_tdcsfog['Id_value'] = id_value\ntest_tdcsfog['Id'] = test_tdcsfog['Id_value'].astype(str) + '_' + test_tdcsfog['Time'].astype(str)\ntest_tdcsfog = test_tdcsfog[['Id','AccV','AccML','AccAP']]\ntest_tdcsfog.set_index('Id',inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:16:52.442774Z","iopub.execute_input":"2024-12-29T14:16:52.443087Z","iopub.status.idle":"2024-12-29T14:16:52.472032Z","shell.execute_reply.started":"2024-12-29T14:16:52.443062Z","shell.execute_reply":"2024-12-29T14:16:52.471376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_tdcsfog_pred=clf.predict(test_tdcsfog)\ntest_tdcsfog['event'] = np.argmax(test_tdcsfog_pred, axis=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:17:43.039642Z","iopub.execute_input":"2024-12-29T14:17:43.040048Z","iopub.status.idle":"2024-12-29T14:17:43.712251Z","shell.execute_reply.started":"2024-12-29T14:17:43.040014Z","shell.execute_reply":"2024-12-29T14:17:43.711240Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_tdcsfog['StartHesitation'] = np.where(test_tdcsfog['event']==1, 1, 0)\ntest_tdcsfog['Turn'] = np.where(test_tdcsfog['event']==2, 1, 0)\ntest_tdcsfog['Walking'] = np.where(test_tdcsfog['event']==3, 1, 0)\ntest_tdcsfog.reset_index('Id', inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:17:53.029635Z","iopub.execute_input":"2024-12-29T14:17:53.029930Z","iopub.status.idle":"2024-12-29T14:17:53.037344Z","shell.execute_reply.started":"2024-12-29T14:17:53.029909Z","shell.execute_reply":"2024-12-29T14:17:53.036638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_tdcsfog.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:18:00.459042Z","iopub.execute_input":"2024-12-29T14:18:00.459409Z","iopub.status.idle":"2024-12-29T14:18:00.470184Z","shell.execute_reply.started":"2024-12-29T14:18:00.459342Z","shell.execute_reply":"2024-12-29T14:18:00.469526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit = pd.concat([test_tdcsfog,test_defog])\nsubmit = submit[['Id', 'StartHesitation', 'Turn','Walking']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:18:12.017708Z","iopub.execute_input":"2024-12-29T14:18:12.018002Z","iopub.status.idle":"2024-12-29T14:18:12.039520Z","shell.execute_reply.started":"2024-12-29T14:18:12.017980Z","shell.execute_reply":"2024-12-29T14:18:12.038741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submit.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:18:18.552286Z","iopub.execute_input":"2024-12-29T14:18:18.552645Z","iopub.status.idle":"2024-12-29T14:18:18.560771Z","shell.execute_reply.started":"2024-12-29T14:18:18.552617Z","shell.execute_reply":"2024-12-29T14:18:18.559940Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:18:25.839220Z","iopub.execute_input":"2024-12-29T14:18:25.839596Z","iopub.status.idle":"2024-12-29T14:18:26.082580Z","shell.execute_reply.started":"2024-12-29T14:18:25.839563Z","shell.execute_reply":"2024-12-29T14:18:26.081615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:18:34.579319Z","iopub.execute_input":"2024-12-29T14:18:34.579707Z","iopub.status.idle":"2024-12-29T14:18:34.587987Z","shell.execute_reply.started":"2024-12-29T14:18:34.579677Z","shell.execute_reply":"2024-12-29T14:18:34.587214Z"}},"outputs":[],"execution_count":null}]}