{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn import *\nimport os\nimport glob\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-19T23:43:17.810368Z","iopub.execute_input":"2023-04-19T23:43:17.810795Z","iopub.status.idle":"2023-04-19T23:43:19.733190Z","shell.execute_reply.started":"2023-04-19T23:43:17.810757Z","shell.execute_reply":"2023-04-19T23:43:19.731860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/\"\n\n# daily = pd.read_csv(path + \"daily_metadata.csv\")\n# defog = pd.read_csv(path + \"defog_metadata.csv\")\n# tdcsfog = pd.read_csv(path + \"tdcsfog_metadata.csv\")\n# subjects = pd.read_csv(path + \"subjects.csv\")\n# events = pd.read_csv(path + \"events.csv\")\n# tasks = pd.read_csv(path + \"tasks.csv\")\n\ntrain_defog = glob.glob(path + \"train/defog/**\")\ntrain_tdcsfog = glob.glob(path + \"train/tdcsfog/**\")","metadata":{"execution":{"iopub.status.busy":"2023-04-19T23:43:19.735207Z","iopub.execute_input":"2023-04-19T23:43:19.736030Z","iopub.status.idle":"2023-04-19T23:43:19.843397Z","shell.execute_reply.started":"2023-04-19T23:43:19.735993Z","shell.execute_reply":"2023-04-19T23:43:19.842387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_data(f):\n    new = pd.read_csv(f)\n    new['Id'] = f.split('/')[-1].split('.')[0]\n    new['data_type'] = f.split('/')[-2]\n    return new\n\ntrain_defog_all = pd.concat([read_data(f) for f in train_defog])\ntrain_tdcsfog_all = pd.concat([read_data(f) for f in train_tdcsfog])\n\ntrain_all = pd.concat([train_defog_all, train_tdcsfog_all])\ntrain_all = train_defog_all\n\ntrain_all.fillna(0, inplace=True)\ntrain_all","metadata":{"execution":{"iopub.status.busy":"2023-04-19T23:43:19.845159Z","iopub.execute_input":"2023-04-19T23:43:19.845960Z","iopub.status.idle":"2023-04-19T23:44:08.281678Z","shell.execute_reply.started":"2023-04-19T23:43:19.845909Z","shell.execute_reply":"2023-04-19T23:44:08.280574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"Time\", \"AccV\", \"AccML\", \"AccAP\"]\ntargets = [\"StartHesitation\", \"Turn\", \"Walking\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-19T23:44:08.283987Z","iopub.execute_input":"2023-04-19T23:44:08.284310Z","iopub.status.idle":"2023-04-19T23:44:08.289854Z","shell.execute_reply.started":"2023-04-19T23:44:08.284279Z","shell.execute_reply":"2023-04-19T23:44:08.288505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = model_selection.train_test_split(train_all[features], train_all[targets], test_size=0.30, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T23:44:08.291317Z","iopub.execute_input":"2023-04-19T23:44:08.291637Z","iopub.status.idle":"2023-04-19T23:44:12.241308Z","shell.execute_reply.started":"2023-04-19T23:44:08.291607Z","shell.execute_reply":"2023-04-19T23:44:12.240146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_all\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T23:44:12.242937Z","iopub.execute_input":"2023-04-19T23:44:12.243280Z","iopub.status.idle":"2023-04-19T23:44:12.367848Z","shell.execute_reply.started":"2023-04-19T23:44:12.243244Z","shell.execute_reply":"2023-04-19T23:44:12.366563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# forest = ensemble.RandomForestRegressor(n_estimators=100, max_depth=10, n_jobs=-1, random_state=42)\nforest = ensemble.RandomForestClassifier(n_estimators=100, max_depth=10, n_jobs=-1, random_state=42)\nforest.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T23:44:12.369552Z","iopub.execute_input":"2023-04-19T23:44:12.369904Z","iopub.status.idle":"2023-04-20T00:09:25.449062Z","shell.execute_reply.started":"2023-04-19T23:44:12.369872Z","shell.execute_reply":"2023-04-20T00:09:25.447988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = metrics.average_precision_score(y_test, forest.predict(X_test).clip(0.0,1.0))\nscore","metadata":{"execution":{"iopub.status.busy":"2023-04-20T00:09:25.450405Z","iopub.execute_input":"2023-04-20T00:09:25.450807Z","iopub.status.idle":"2023-04-20T00:10:08.130477Z","shell.execute_reply.started":"2023-04-20T00:09:25.450776Z","shell.execute_reply":"2023-04-20T00:10:08.129603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(path + \"sample_submission.csv\")\ntest = glob.glob(path + \"test/**/**\")\n\nsub['t'] = 0\nsubmission = []\nfor f in test:\n    df = pd.read_csv(f)\n    df['Id'] = f.split('/')[-1].split('.')[0]\n    df = df.fillna(0).reset_index(drop=True)\n    res = pd.DataFrame(np.round(forest.predict(df[features]),3), columns=['StartHesitation', 'Turn' , 'Walking'])\n    df = pd.concat([df,res], axis=1)\n    df['Id'] = df['Id'].astype(str) + '_' + df['Time'].astype(str)\n    submission.append(df[['Id','StartHesitation', 'Turn' , 'Walking']])\nsubmission = pd.concat(submission)\nsubmission = pd.merge(sub[['Id','t']], submission, how='left', on='Id').fillna(0.0)\nsubmission[['Id','StartHesitation', 'Turn' , 'Walking']].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T00:10:08.131743Z","iopub.execute_input":"2023-04-20T00:10:08.132165Z","iopub.status.idle":"2023-04-20T00:10:12.439002Z","shell.execute_reply.started":"2023-04-20T00:10:08.132134Z","shell.execute_reply":"2023-04-20T00:10:12.437837Z"},"trusted":true},"execution_count":null,"outputs":[]}]}