{"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":"# Data Analysis & Visualization\nimport os\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport pandas as pd\nimport numpy as np\nimport torch\nimport torch.nn as nn\nimport glob\nimport gc\nfrom pathlib import Path\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-20T21:21:05.531460Z","iopub.execute_input":"2023-04-20T21:21:05.531954Z","iopub.status.idle":"2023-04-20T21:21:08.863092Z","shell.execute_reply.started":"2023-04-20T21:21:05.531910Z","shell.execute_reply":"2023-04-20T21:21:08.861798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FEATURES = [\"AccV\", \"AccML\", \"AccAP\"]\nTARGETS = [\"StartHesitation\", \"Turn\", \"Walking\"]\n","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:21:08.865971Z","iopub.execute_input":"2023-04-20T21:21:08.867118Z","iopub.status.idle":"2023-04-20T21:21:08.873013Z","shell.execute_reply.started":"2023-04-20T21:21:08.867055Z","shell.execute_reply":"2023-04-20T21:21:08.871703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read in the train and test data\ntrain_df = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/train/defog/0c55be4384.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/defog/02ab235146.csv\")\ntrain_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:21:08.874604Z","iopub.execute_input":"2023-04-20T21:21:08.874990Z","iopub.status.idle":"2023-04-20T21:21:09.613466Z","shell.execute_reply.started":"2023-04-20T21:21:08.874952Z","shell.execute_reply":"2023-04-20T21:21:09.612095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:21:09.614725Z","iopub.execute_input":"2023-04-20T21:21:09.615090Z","iopub.status.idle":"2023-04-20T21:21:09.627939Z","shell.execute_reply.started":"2023-04-20T21:21:09.615055Z","shell.execute_reply":"2023-04-20T21:21:09.626543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=\"../input/tlvmc-parkinsons-freezing-gait-prediction/\"\n\ntrain_defog = glob.glob(path+'train/**/**')\ntrain_tdcsfog = glob.glob(path+'test/**/**')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:21:09.632588Z","iopub.execute_input":"2023-04-20T21:21:09.633119Z","iopub.status.idle":"2023-04-20T21:21:09.730101Z","shell.execute_reply.started":"2023-04-20T21:21:09.633062Z","shell.execute_reply":"2023-04-20T21:21:09.728633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# func read data \ndef reader(f):\n    df = pd.read_csv(f)\n    df['Id'] = f.split('/')[-1].split('.')[0]\n    return df\n# read train data\ntrain_defog = pd.concat([reader(f) for f in train_defog]).fillna(0); print(train_defog.shape)\ncols = [c for c in train_defog.columns if c not in ['Id', 'StartHesitation', 'Turn' , 'Walking', 'Valid', 'Task','Event']] # except categorical and target fetaure","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:21:09.731588Z","iopub.execute_input":"2023-04-20T21:21:09.731999Z","iopub.status.idle":"2023-04-20T21:22:39.669220Z","shell.execute_reply.started":"2023-04-20T21:21:09.731959Z","shell.execute_reply":"2023-04-20T21:22:39.666858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_defog","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:22:39.670966Z","iopub.execute_input":"2023-04-20T21:22:39.671373Z","iopub.status.idle":"2023-04-20T21:22:39.708442Z","shell.execute_reply.started":"2023-04-20T21:22:39.671333Z","shell.execute_reply":"2023-04-20T21:22:39.705667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tdcsfog = pd.concat([reader(f) for f in train_tdcsfog]).fillna(0); print(train_tdcsfog.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:22:39.711459Z","iopub.execute_input":"2023-04-20T21:22:39.712004Z","iopub.status.idle":"2023-04-20T21:22:39.967188Z","shell.execute_reply.started":"2023-04-20T21:22:39.711937Z","shell.execute_reply":"2023-04-20T21:22:39.965626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tdcsfog","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:22:39.968973Z","iopub.execute_input":"2023-04-20T21:22:39.969623Z","iopub.status.idle":"2023-04-20T21:22:39.989444Z","shell.execute_reply.started":"2023-04-20T21:22:39.969575Z","shell.execute_reply":"2023-04-20T21:22:39.987951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.concat([train_defog,train_tdcsfog])\n\n#df_train","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:22:39.990981Z","iopub.execute_input":"2023-04-20T21:22:39.991368Z","iopub.status.idle":"2023-04-20T21:22:47.221359Z","shell.execute_reply.started":"2023-04-20T21:22:39.991330Z","shell.execute_reply":"2023-04-20T21:22:47.218682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:22:47.225224Z","iopub.execute_input":"2023-04-20T21:22:47.225721Z","iopub.status.idle":"2023-04-20T21:23:06.242357Z","shell.execute_reply.started":"2023-04-20T21:22:47.225677Z","shell.execute_reply":"2023-04-20T21:23:06.241192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:06.243845Z","iopub.execute_input":"2023-04-20T21:23:06.244792Z","iopub.status.idle":"2023-04-20T21:23:06.264808Z","shell.execute_reply.started":"2023-04-20T21:23:06.244749Z","shell.execute_reply":"2023-04-20T21:23:06.262982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.iloc[:, 1:-1].values\ny = train_df.iloc[:, -1].values\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:06.266887Z","iopub.execute_input":"2023-04-20T21:23:06.267502Z","iopub.status.idle":"2023-04-20T21:23:06.377497Z","shell.execute_reply.started":"2023-04-20T21:23:06.267447Z","shell.execute_reply":"2023-04-20T21:23:06.376140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\nX_train = sc.fit_transform(X_train)\nX_test = sc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:06.382244Z","iopub.execute_input":"2023-04-20T21:23:06.382984Z","iopub.status.idle":"2023-04-20T21:23:06.548550Z","shell.execute_reply.started":"2023-04-20T21:23:06.382930Z","shell.execute_reply":"2023-04-20T21:23:06.547088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self, input_size, hidden_size, num_classes):\n        super(Net, self).__init__()\n        self.fc1 = nn.Linear(input_size, hidden_size) \n        self.relu = nn.ReLU()\n        self.fc2 = nn.Linear(hidden_size, num_classes)  \n    \n    def forward(self, x):\n        out = self.fc1(x)\n        out = self.relu(out)\n        out = self.fc2(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:06.549984Z","iopub.execute_input":"2023-04-20T21:23:06.550398Z","iopub.status.idle":"2023-04-20T21:23:06.559138Z","shell.execute_reply.started":"2023-04-20T21:23:06.550359Z","shell.execute_reply":"2023-04-20T21:23:06.557623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_size = X_train.shape[1]\nhidden_size = 10\nnum_classes = 2\nnet = Net(input_size, hidden_size, num_classes)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(net.parameters(), lr=0.001)\n\nnum_epochs = 100\nfor epoch in range(num_epochs):\n    inputs = torch.from_numpy(X_train).float()\n    targets = torch.from_numpy(y_train).long()\n\n    optimizer.zero_grad()\n    outputs = net(inputs)\n    loss = criterion(outputs, targets)\n    loss.backward()\n    optimizer.step()\n\n    if (epoch+1) % 10 == 0:\n        print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:06.561031Z","iopub.execute_input":"2023-04-20T21:23:06.561526Z","iopub.status.idle":"2023-04-20T21:23:08.565263Z","shell.execute_reply.started":"2023-04-20T21:23:06.561469Z","shell.execute_reply":"2023-04-20T21:23:08.564190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = torch.from_numpy(X_test).float()\noutputs = net(inputs)\n_, predicted = torch.max(outputs.data, 1)\n\nprint('Accuracy:', accuracy_score(y_test, predicted))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:08.566805Z","iopub.execute_input":"2023-04-20T21:23:08.568058Z","iopub.status.idle":"2023-04-20T21:23:08.588611Z","shell.execute_reply.started":"2023-04-20T21:23:08.568001Z","shell.execute_reply":"2023-04-20T21:23:08.587546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:08.590611Z","iopub.execute_input":"2023-04-20T21:23:08.591596Z","iopub.status.idle":"2023-04-20T21:23:08.610565Z","shell.execute_reply.started":"2023-04-20T21:23:08.591527Z","shell.execute_reply":"2023-04-20T21:23:08.609082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_predicted = predicted.numpy()\nnew_predicted","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:08.612545Z","iopub.execute_input":"2023-04-20T21:23:08.614213Z","iopub.status.idle":"2023-04-20T21:23:08.624510Z","shell.execute_reply.started":"2023-04-20T21:23:08.614154Z","shell.execute_reply":"2023-04-20T21:23:08.622725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate the dataset for the independent variables.\n\ncols = ['Id','StartHesitation','Turn','Walking']\ntest = df_train[cols]","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:08.626907Z","iopub.execute_input":"2023-04-20T21:23:08.627397Z","iopub.status.idle":"2023-04-20T21:23:09.239514Z","shell.execute_reply.started":"2023-04-20T21:23:08.627349Z","shell.execute_reply":"2023-04-20T21:23:09.238369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(path+'sample_submission.csv')\n#sub['t'] = 0\nsubmission = pd.merge(sub[['Id']], test, 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-20T21:25:40.520975Z","iopub.execute_input":"2023-04-20T21:25:40.521478Z","iopub.status.idle":"2023-04-20T21:25:45.438986Z","shell.execute_reply.started":"2023-04-20T21:25:40.521438Z","shell.execute_reply":"2023-04-20T21:25:45.437529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:25:48.983629Z","iopub.execute_input":"2023-04-20T21:25:48.984166Z","iopub.status.idle":"2023-04-20T21:25:49.003345Z","shell.execute_reply.started":"2023-04-20T21:25:48.984117Z","shell.execute_reply":"2023-04-20T21:25:49.002267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test.fillna(0).to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:23:13.695426Z","iopub.execute_input":"2023-04-20T21:23:13.695879Z","iopub.status.idle":"2023-04-20T21:24:02.139001Z","shell.execute_reply.started":"2023-04-20T21:23:13.695816Z","shell.execute_reply":"2023-04-20T21:24:02.136930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_defog[cols]","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:24:02.140854Z","iopub.status.idle":"2023-04-20T21:24:02.141995Z","shell.execute_reply.started":"2023-04-20T21:24:02.141603Z","shell.execute_reply":"2023-04-20T21:24:02.141646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:24:02.143850Z","iopub.status.idle":"2023-04-20T21:24:02.144861Z","shell.execute_reply.started":"2023-04-20T21:24:02.144463Z","shell.execute_reply":"2023-04-20T21:24:02.144545Z"},"trusted":true},"execution_count":null,"outputs":[]}]}