{"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":"import pandas as pd\nimport os\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import average_precision_score\nfrom sklearn.model_selection import train_test_split\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:54:15.496970Z","iopub.execute_input":"2023-05-01T10:54:15.497521Z","iopub.status.idle":"2023-05-01T10:54:15.505391Z","shell.execute_reply.started":"2023-05-01T10:54:15.497473Z","shell.execute_reply":"2023-05-01T10:54:15.503755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_full_metadata(folder_name):\n    df = pd.concat([pd.read_csv(f\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/{folder_name}/{file_name}\")\n                    for file_name in os.listdir(f\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/{folder_name}/\") if file_name.endswith('.csv')],\n                   axis=0)\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:10:57.085497Z","iopub.execute_input":"2023-05-01T10:10:57.085925Z","iopub.status.idle":"2023-05-01T10:10:57.093560Z","shell.execute_reply.started":"2023-05-01T10:10:57.085885Z","shell.execute_reply":"2023-05-01T10:10:57.092052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_defog = create_full_metadata(folder_name=\"defog\")\ndf_tdcsfog = create_full_metadata(folder_name=\"tdcsfog\")","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:14:58.861625Z","iopub.execute_input":"2023-05-01T10:14:58.862232Z","iopub.status.idle":"2023-05-01T10:15:49.166148Z","shell.execute_reply.started":"2023-05-01T10:14:58.862185Z","shell.execute_reply":"2023-05-01T10:15:49.164317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_df_defog = df_defog[(df_defog['Valid'] == True) & df_defog['Task'] == True].drop(columns=['Valid', 'Task'])","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:15:49.168819Z","iopub.execute_input":"2023-05-01T10:15:49.169626Z","iopub.status.idle":"2023-05-01T10:15:50.294869Z","shell.execute_reply.started":"2023-05-01T10:15:49.169558Z","shell.execute_reply":"2023-05-01T10:15:50.293362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_df = pd.concat([true_df_defog, df_tdcsfog], axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:15:50.296855Z","iopub.execute_input":"2023-05-01T10:15:50.297719Z","iopub.status.idle":"2023-05-01T10:15:50.818403Z","shell.execute_reply.started":"2023-05-01T10:15:50.297654Z","shell.execute_reply":"2023-05-01T10:15:50.816556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:15:53.133565Z","iopub.execute_input":"2023-05-01T10:15:53.134126Z","iopub.status.idle":"2023-05-01T10:15:53.167451Z","shell.execute_reply.started":"2023-05-01T10:15:53.134072Z","shell.execute_reply":"2023-05-01T10:15:53.165815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_df.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:15:54.641446Z","iopub.execute_input":"2023-05-01T10:15:54.641962Z","iopub.status.idle":"2023-05-01T10:15:54.652945Z","shell.execute_reply.started":"2023-05-01T10:15:54.641912Z","shell.execute_reply":"2023-05-01T10:15:54.651526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = merged_df[['AccV', 'AccML', 'AccAP']]\ny1 = merged_df['StartHesitation']\ny2 = merged_df['Turn']\ny3 = merged_df['Walking']","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:15:55.790020Z","iopub.execute_input":"2023-05-01T10:15:55.790540Z","iopub.status.idle":"2023-05-01T10:15:56.521985Z","shell.execute_reply.started":"2023-05-01T10:15:55.790495Z","shell.execute_reply":"2023-05-01T10:15:56.520337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1_train, X1_test, y1_train, y1_test = train_test_split(X, y1, test_size = 0.33, random_state = 42)\nX2_train, X2_test, y2_train, y2_test = train_test_split(X, y2, test_size = 0.33, random_state = 42)\nX3_train, X3_test, y3_train, y3_test = train_test_split(X, y3, test_size = 0.33, random_state = 42)\n\nmodel1 = LogisticRegression() # StartHesitation\nmodel2 = LogisticRegression() # Turn\nmodel3 = LogisticRegression() # Walking\n\nmodel1.fit(X1_train, y1_train)\nmodel2.fit(X2_train, y2_train)\nmodel3.fit(X3_train, y3_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:15:56.525051Z","iopub.execute_input":"2023-05-01T10:15:56.525697Z","iopub.status.idle":"2023-05-01T10:16:41.198543Z","shell.execute_reply.started":"2023-05-01T10:15:56.525626Z","shell.execute_reply":"2023-05-01T10:16:41.195888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"AP 'StartHesitation':\", average_precision_score(y1_test, model1.predict_proba(X1_test)[:, 1]))\nprint(\"AP 'Turn':\", average_precision_score(y2_test, model2.predict_proba(X2_test)[:, 1]))\nprint(\"AP 'Walking':\", average_precision_score(y3_test, model3.predict_proba(X3_test)[:, 1]))","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:16:41.201939Z","iopub.execute_input":"2023-05-01T10:16:41.202991Z","iopub.status.idle":"2023-05-01T10:16:46.530698Z","shell.execute_reply.started":"2023-05-01T10:16:41.202924Z","shell.execute_reply":"2023-05-01T10:16:46.528971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:23:05.818923Z","iopub.execute_input":"2023-05-01T10:23:05.820229Z","iopub.status.idle":"2023-05-01T10:23:05.826200Z","shell.execute_reply.started":"2023-05-01T10:23:05.820162Z","shell.execute_reply":"2023-05-01T10:23:05.824554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = '/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction'","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:21:58.854605Z","iopub.execute_input":"2023-05-01T10:21:58.855104Z","iopub.status.idle":"2023-05-01T10:21:58.861901Z","shell.execute_reply.started":"2023-05-01T10:21:58.855064Z","shell.execute_reply":"2023-05-01T10:21:58.860494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tdcs_meta = pd.read_csv(os.path.join(DATA_PATH, 'tdcsfog_metadata.csv'))","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:22:51.753150Z","iopub.execute_input":"2023-05-01T10:22:51.753756Z","iopub.status.idle":"2023-05-01T10:22:51.769180Z","shell.execute_reply.started":"2023-05-01T10:22:51.753704Z","shell.execute_reply":"2023-05-01T10:22:51.767410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdcs_test_file_path = glob.glob(os.path.join(DATA_PATH, 'test', 'tdcsfog', '*.csv'), recursive=True)\ndefog_test_file_path = glob.glob(os.path.join(DATA_PATH, 'test', 'defog', '*.csv'), recursive=True)\nprint(f'the number of files to be read: {len(tdcs_test_file_path) + len(defog_test_file_path)}')","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:43:20.882178Z","iopub.execute_input":"2023-05-01T10:43:20.882751Z","iopub.status.idle":"2023-05-01T10:43:20.896740Z","shell.execute_reply.started":"2023-05-01T10:43:20.882707Z","shell.execute_reply":"2023-05-01T10:43:20.894876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tdcs_test = pd.DataFrame()\nfor fp in tqdm(tdcs_test_file_path):\n    \n    # load data into a variable 'tmp'.\n    tmp = pd.read_csv(fp)\n    \n    # get file Id from csv file name.\n    file_id = os.path.basename(fp).replace(\".csv\", \"\")\n    \n    # add Id data to submit.\n    tmp['Id'] = file_id + '_' + tmp['Time'].astype(str)\n    \n    # concat the data\n    df_tdcs_test = pd.concat([df_tdcs_test, tmp]).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:43:24.459689Z","iopub.execute_input":"2023-05-01T10:43:24.460455Z","iopub.status.idle":"2023-05-01T10:43:24.489231Z","shell.execute_reply.started":"2023-05-01T10:43:24.460399Z","shell.execute_reply":"2023-05-01T10:43:24.487747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_defog_test = pd.DataFrame()\n\nfor fp in tqdm(defog_test_file_path):\n    # load data into a variable 'tmp'.\n    tmp = pd.read_csv(fp)\n    \n    # get file Id from csv file name.\n    file_id = os.path.basename(fp).replace(\".csv\", \"\")\n    \n    # add Id data to submit.\n    tmp['Id'] = file_id + '_' + tmp['Time'].astype(str)\n    \n    # concat the data\n    df_defog_test = pd.concat([df_defog_test, tmp]).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:43:31.346677Z","iopub.execute_input":"2023-05-01T10:43:31.348053Z","iopub.status.idle":"2023-05-01T10:43:32.065226Z","shell.execute_reply.started":"2023-05-01T10:43:31.347983Z","shell.execute_reply":"2023-05-01T10:43:32.064015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.concat([df_tdcs_test, df_defog_test]).reset_index(drop=True)\nX_test = df_test.drop(columns=[\"Time\", \"Id\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:43:48.177507Z","iopub.execute_input":"2023-05-01T10:43:48.178038Z","iopub.status.idle":"2023-05-01T10:43:48.203101Z","shell.execute_reply.started":"2023-05-01T10:43:48.177990Z","shell.execute_reply":"2023-05-01T10:43:48.201555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert X_test.shape[1] == X1_train.shape[1]\nassert all(X_test.columns == X1_train.columns)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:44:39.361973Z","iopub.execute_input":"2023-05-01T10:44:39.362659Z","iopub.status.idle":"2023-05-01T10:44:39.370798Z","shell.execute_reply.started":"2023-05-01T10:44:39.362612Z","shell.execute_reply":"2023-05-01T10:44:39.368985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(os.path.join(DATA_PATH, \"sample_submission.csv\"))\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:44:42.014647Z","iopub.execute_input":"2023-05-01T10:44:42.016501Z","iopub.status.idle":"2023-05-01T10:44:42.263214Z","shell.execute_reply.started":"2023-05-01T10:44:42.016390Z","shell.execute_reply":"2023-05-01T10:44:42.261470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert submission.shape[0] == X_test.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:45:07.878108Z","iopub.execute_input":"2023-05-01T10:45:07.878990Z","iopub.status.idle":"2023-05-01T10:45:07.886447Z","shell.execute_reply.started":"2023-05-01T10:45:07.878934Z","shell.execute_reply":"2023-05-01T10:45:07.884723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(df_test[\"Id\"])\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:45:32.254499Z","iopub.execute_input":"2023-05-01T10:45:32.255689Z","iopub.status.idle":"2023-05-01T10:45:32.277656Z","shell.execute_reply.started":"2023-05-01T10:45:32.255620Z","shell.execute_reply":"2023-05-01T10:45:32.276389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"StartHesitation\"] = model1.predict(X_test)\nsubmission[\"Turn\"] = model2.predict(X_test)\nsubmission[\"Walking\"] = model3.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:45:34.099849Z","iopub.execute_input":"2023-05-01T10:45:34.100303Z","iopub.status.idle":"2023-05-01T10:45:34.135186Z","shell.execute_reply.started":"2023-05-01T10:45:34.100254Z","shell.execute_reply":"2023-05-01T10:45:34.132933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(model3.predict_proba(X_test)[:, 1] > 0.5), sum(model3.predict_proba(X_test)[:, 1] > 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:56:49.842690Z","iopub.execute_input":"2023-05-01T10:56:49.843479Z","iopub.status.idle":"2023-05-01T10:56:51.352975Z","shell.execute_reply.started":"2023-05-01T10:56:49.843406Z","shell.execute_reply":"2023-05-01T10:56:51.351593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(model2.predict_proba(X_test)[:, 1] > 0.5)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:56:00.026343Z","iopub.execute_input":"2023-05-01T10:56:00.027246Z","iopub.status.idle":"2023-05-01T10:56:00.762730Z","shell.execute_reply.started":"2023-05-01T10:56:00.027158Z","shell.execute_reply":"2023-05-01T10:56:00.761677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(model1.predict_proba(X_test)[:, 1] > 0.5)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:50:44.639754Z","iopub.execute_input":"2023-05-01T10:50:44.640513Z","iopub.status.idle":"2023-05-01T10:50:45.380069Z","shell.execute_reply.started":"2023-05-01T10:50:44.640459Z","shell.execute_reply":"2023-05-01T10:50:45.378143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model3.predict_proba(X_test)[:, 1]\npreds = np.where(preds > 0.2, 1, 0)\n\nsubmission[\"Walking\"] = preds","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:57:03.121815Z","iopub.execute_input":"2023-05-01T10:57:03.122324Z","iopub.status.idle":"2023-05-01T10:57:03.151762Z","shell.execute_reply.started":"2023-05-01T10:57:03.122273Z","shell.execute_reply":"2023-05-01T10:57:03.149583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"Walking\"].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:57:14.219211Z","iopub.execute_input":"2023-05-01T10:57:14.221076Z","iopub.status.idle":"2023-05-01T10:57:14.234649Z","shell.execute_reply.started":"2023-05-01T10:57:14.220992Z","shell.execute_reply":"2023-05-01T10:57:14.232973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:57:17.386331Z","iopub.execute_input":"2023-05-01T10:57:17.387165Z","iopub.status.idle":"2023-05-01T10:57:17.405223Z","shell.execute_reply.started":"2023-05-01T10:57:17.387093Z","shell.execute_reply":"2023-05-01T10:57:17.403599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T10:57:23.423351Z","iopub.execute_input":"2023-05-01T10:57:23.424475Z","iopub.status.idle":"2023-05-01T10:57:23.873078Z","shell.execute_reply.started":"2023-05-01T10:57:23.424393Z","shell.execute_reply":"2023-05-01T10:57:23.871603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}