{"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":"none","dataSources":[{"sourceId":41880,"databundleVersionId":5677426,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport time\n\n# Installed libraries\nimport numpy as np\nimport pandas as pd\nfrom sklearn.metrics import roc_auc_score, log_loss\nfrom sklearn.model_selection import train_test_split\nimport torch\n\n# Imports from our package\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import accuracy_score\nimport matplotlib.pyplot as plt\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom xgboost import XGBClassifier\nfrom lightgbm import LGBMClassifier\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:14:30.922556Z","iopub.execute_input":"2023-12-27T03:14:30.923936Z","iopub.status.idle":"2023-12-27T03:14:36.424528Z","shell.execute_reply.started":"2023-12-27T03:14:30.923867Z","shell.execute_reply":"2023-12-27T03:14:36.423350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import tdcsfog","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/tdcsfog/'\ntdcsfog = pd.concat([pd.read_csv(os.path.join(root, name)) for root, _, files in os.walk(path) for name in files], axis=0)\n\ntdcsfog.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:14:36.427652Z","iopub.execute_input":"2023-12-27T03:14:36.428756Z","iopub.status.idle":"2023-12-27T03:15:00.017745Z","shell.execute_reply.started":"2023-12-27T03:14:36.428702Z","shell.execute_reply":"2023-12-27T03:15:00.016465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import defog","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog'\ndefog = pd.concat([pd.read_csv(os.path.join(root, name)) for root, _, files in os.walk(path) for name in files], axis=0)\n\ndefog.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:00.019496Z","iopub.execute_input":"2023-12-27T03:15:00.020668Z","iopub.status.idle":"2023-12-27T03:15:26.601991Z","shell.execute_reply.started":"2023-12-27T03:15:00.020615Z","shell.execute_reply":"2023-12-27T03:15:26.601019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocess","metadata":{}},{"cell_type":"code","source":"defog_data_valid = defog.loc[ \\\n                        (defog.Valid == True) & (defog.Task == True)].copy()\n\ndefog_data_valid.reset_index(drop=True, inplace=True)\ndefog_data_valid.drop(['Valid', 'Task'], axis=1, inplace=True)\ndefog_data_valid.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:26.603537Z","iopub.execute_input":"2023-12-27T03:15:26.604106Z","iopub.status.idle":"2023-12-27T03:15:27.127862Z","shell.execute_reply.started":"2023-12-27T03:15:26.604070Z","shell.execute_reply":"2023-12-27T03:15:27.126792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data = pd.concat([tdcsfog, defog_data_valid])\nfull_data","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:27.130982Z","iopub.execute_input":"2023-12-27T03:15:27.131659Z","iopub.status.idle":"2023-12-27T03:15:27.548842Z","shell.execute_reply.started":"2023-12-27T03:15:27.131618Z","shell.execute_reply":"2023-12-27T03:15:27.547521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"condlist = [\n    full_data['StartHesitation'] == 1,\n    full_data['Turn'] == 1,\n    full_data['Walking'] == 1\n]\n\nchoicelist = ['StartHesitation', 'Turn', 'Walking']\nfull_data['TARGET'] = np.select(condlist=condlist, choicelist=choicelist, default='Normal')\n\ntarget_counts = full_data['TARGET'].value_counts().to_frame()\ntarget_counts","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:27.550683Z","iopub.execute_input":"2023-12-27T03:15:27.551064Z","iopub.status.idle":"2023-12-27T03:15:33.422136Z","shell.execute_reply.started":"2023-12-27T03:15:27.551029Z","shell.execute_reply":"2023-12-27T03:15:33.420683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\nfull_data['TARGET'] = le.fit_transform(full_data['TARGET'])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:33.424156Z","iopub.execute_input":"2023-12-27T03:15:33.424707Z","iopub.status.idle":"2023-12-27T03:15:37.219372Z","shell.execute_reply.started":"2023-12-27T03:15:33.424659Z","shell.execute_reply":"2023-12-27T03:15:37.217865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_balanced = full_data.groupby('TARGET', group_keys=False).apply(lambda x: x.sample(min(len(x), full_data['TARGET'].value_counts().min())))\ndf_balanced.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:37.221073Z","iopub.execute_input":"2023-12-27T03:15:37.221460Z","iopub.status.idle":"2023-12-27T03:15:39.424208Z","shell.execute_reply.started":"2023-12-27T03:15:37.221428Z","shell.execute_reply":"2023-12-27T03:15:39.422534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_counts_balanced = df_balanced['TARGET'].value_counts().to_frame()\ntarget_counts_balanced","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:39.426322Z","iopub.execute_input":"2023-12-27T03:15:39.426787Z","iopub.status.idle":"2023-12-27T03:15:39.454587Z","shell.execute_reply.started":"2023-12-27T03:15:39.426737Z","shell.execute_reply":"2023-12-27T03:15:39.453059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_balanced.drop(['Time', 'Turn', 'Walking', 'StartHesitation'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:39.456747Z","iopub.execute_input":"2023-12-27T03:15:39.457221Z","iopub.status.idle":"2023-12-27T03:15:39.481039Z","shell.execute_reply.started":"2023-12-27T03:15:39.457180Z","shell.execute_reply":"2023-12-27T03:15:39.479525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train-test split","metadata":{}},{"cell_type":"code","source":"X = df_balanced.drop(['TARGET'], axis=1)\ny = df_balanced['TARGET']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:39.482633Z","iopub.execute_input":"2023-12-27T03:15:39.483043Z","iopub.status.idle":"2023-12-27T03:15:39.733933Z","shell.execute_reply.started":"2023-12-27T03:15:39.483008Z","shell.execute_reply":"2023-12-27T03:15:39.732270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Classification","metadata":{}},{"cell_type":"markdown","source":"## XG Boost","metadata":{}},{"cell_type":"code","source":"xgb_classifier = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)\n\n# Train the classifier\nxgb_classifier.fit(X_train, y_train)\n\n# Make predictions on the test set\ny_pred_1 = xgb_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:15:39.736633Z","iopub.execute_input":"2023-12-27T03:15:39.737046Z","iopub.status.idle":"2023-12-27T03:16:00.360499Z","shell.execute_reply.started":"2023-12-27T03:15:39.737009Z","shell.execute_reply":"2023-12-27T03:16:00.358946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('XG Boost accuracy:', accuracy_score(y_test, y_pred_1))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:16:00.362536Z","iopub.execute_input":"2023-12-27T03:16:00.362932Z","iopub.status.idle":"2023-12-27T03:16:00.393214Z","shell.execute_reply.started":"2023-12-27T03:16:00.362898Z","shell.execute_reply":"2023-12-27T03:16:00.391960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Decision Tree","metadata":{}},{"cell_type":"code","source":"dtc = DecisionTreeClassifier(random_state=42)\n\n# Train the classifier\ndtc.fit(X_train, y_train)\n\n# Make predictions on the test set\ny_pred_2 = dtc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:16:00.398950Z","iopub.execute_input":"2023-12-27T03:16:00.399371Z","iopub.status.idle":"2023-12-27T03:16:15.496908Z","shell.execute_reply.started":"2023-12-27T03:16:00.399334Z","shell.execute_reply":"2023-12-27T03:16:15.495653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Decision Tree accuracy:', accuracy_score(y_test, y_pred_2))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:16:15.498703Z","iopub.execute_input":"2023-12-27T03:16:15.499094Z","iopub.status.idle":"2023-12-27T03:16:15.531826Z","shell.execute_reply.started":"2023-12-27T03:16:15.499060Z","shell.execute_reply":"2023-12-27T03:16:15.530593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest","metadata":{}},{"cell_type":"code","source":"rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)\n\n# Train the classifier\nrf_classifier.fit(X_train, y_train)\n\ny_pred_3 = rf_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:16:15.533785Z","iopub.execute_input":"2023-12-27T03:16:15.534651Z","iopub.status.idle":"2023-12-27T03:23:22.869330Z","shell.execute_reply.started":"2023-12-27T03:16:15.534603Z","shell.execute_reply":"2023-12-27T03:23:22.867470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Random Forest accuracy:', accuracy_score(y_test, y_pred_3))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:23:22.871312Z","iopub.execute_input":"2023-12-27T03:23:22.871797Z","iopub.status.idle":"2023-12-27T03:23:22.902253Z","shell.execute_reply.started":"2023-12-27T03:23:22.871710Z","shell.execute_reply":"2023-12-27T03:23:22.900821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## KNN","metadata":{}},{"cell_type":"code","source":"knn_classifier = KNeighborsClassifier(n_neighbors=2) \n\n# Train the classifier\nknn_classifier.fit(X_train, y_train)\n\n# Make predictions on the test set\ny_pred_4 = knn_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:23:22.903913Z","iopub.execute_input":"2023-12-27T03:23:22.904769Z","iopub.status.idle":"2023-12-27T03:23:52.085368Z","shell.execute_reply.started":"2023-12-27T03:23:22.904724Z","shell.execute_reply":"2023-12-27T03:23:52.084278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('KNN accuracy:', accuracy_score(y_test, y_pred_4))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:23:52.086700Z","iopub.execute_input":"2023-12-27T03:23:52.088109Z","iopub.status.idle":"2023-12-27T03:23:52.120787Z","shell.execute_reply.started":"2023-12-27T03:23:52.088019Z","shell.execute_reply":"2023-12-27T03:23:52.119526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## preprocess Data","metadata":{}},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:23:52.123580Z","iopub.execute_input":"2023-12-27T03:23:52.123955Z","iopub.status.idle":"2023-12-27T03:23:52.183239Z","shell.execute_reply.started":"2023-12-27T03:23:52.123924Z","shell.execute_reply":"2023-12-27T03:23:52.181647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:23:52.185998Z","iopub.execute_input":"2023-12-27T03:23:52.186954Z","iopub.status.idle":"2023-12-27T03:23:52.203948Z","shell.execute_reply.started":"2023-12-27T03:23:52.186916Z","shell.execute_reply":"2023-12-27T03:23:52.202271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = full_data.drop(['TARGET', 'StartHesitation', 'Turn', 'Walking'], axis=1)\ny = full_data['TARGET']\n\nX_train2, X_test2, y_train2, y_test2 = train_test_split(X, y, test_size=0.3, random_state=42)\n\nprint(\"Class distribution before SMOTE:\")\nprint(y_train2.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:43:22.035143Z","iopub.execute_input":"2023-12-27T03:43:22.035690Z","iopub.status.idle":"2023-12-27T03:43:25.405461Z","shell.execute_reply.started":"2023-12-27T03:43:22.035647Z","shell.execute_reply":"2023-12-27T03:43:25.404159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\n\nsmote = SMOTE(sampling_strategy='auto', random_state=42)\nX_train_resampled, y_train_resampled = smote.fit_resample(X_train2, y_train2)\n\nprint(\"\\nClass distribution after SMOTE:\")\nprint(pd.Series(y_train_resampled).value_counts())\n\nprint(\"\\nShapes after SMOTE:\")\nprint(\"X_train_resampled:\", X_train_resampled.shape)\nprint(\"y_train_resampled:\", y_train_resampled.shape)\nprint(\"X_test:\", X_test2.shape)\nprint(\"y_test:\", y_test2.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:43:28.801689Z","iopub.execute_input":"2023-12-27T03:43:28.802199Z","iopub.status.idle":"2023-12-27T03:44:14.526566Z","shell.execute_reply.started":"2023-12-27T03:43:28.802161Z","shell.execute_reply":"2023-12-27T03:44:14.525044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize the XGBoost classifier\nxgb_classifier2 = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)\n\n# Train the classifier\nxgb_classifier2.fit(X_train_resampled, y_train_resampled)\n\n# Make predictions on the test set\ny_pred_6 = xgb_classifier2.predict(X_test2)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:45:04.253187Z","iopub.execute_input":"2023-12-27T03:45:04.254518Z","iopub.status.idle":"2023-12-27T03:55:30.918949Z","shell.execute_reply.started":"2023-12-27T03:45:04.254463Z","shell.execute_reply":"2023-12-27T03:55:30.917225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('xgb accuracy:', accuracy_score(y_test2, y_pred_6))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T03:55:38.892103Z","iopub.execute_input":"2023-12-27T03:55:38.892578Z","iopub.status.idle":"2023-12-27T03:55:39.111970Z","shell.execute_reply.started":"2023-12-27T03:55:38.892540Z","shell.execute_reply":"2023-12-27T03:55:39.110908Z"},"trusted":true},"execution_count":null,"outputs":[]}]}