{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-19T01:50:11.418764Z","iopub.execute_input":"2023-04-19T01:50:11.419220Z","iopub.status.idle":"2023-04-19T01:50:11.471704Z","shell.execute_reply.started":"2023-04-19T01:50:11.419181Z","shell.execute_reply":"2023-04-19T01:50:11.470261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = '../input/tlvmc-parkinsons-freezing-gait-prediction/'\n#short hand for future mentions\n\n\ntrain_data = glob.glob(p+\"train/**/**\")\ntest_data = glob.glob(p+'test/**/**')\n#list of CSV files obtained\n\nsubjects = pd.read_csv(p+'subjects.csv')\ntasks = pd.read_csv(p+'tasks.csv')\nsub = pd.read_csv(p+'sample_submission.csv')\n#labeling data\n\ntdcsfog_metadata = pd.read_csv(p+'tdcsfog_metadata.csv')\ndefog_metadata = pd.read_csv(p+'defog_metadata.csv')\ndaily_metadata = pd.read_csv(p+'daily_metadata.csv')\n#labelingdata\n\ntdcsfog_metadata[\"Module\"] = 'tdcsfog'\ndefog_metadata[\"Module\"] = \"defog\"\n#adding modulal source to metadata\n\nmetadata = pd.concat([tdcsfog_metadata, defog_metadata])\n#concatanation of metadata","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:50:11.474775Z","iopub.execute_input":"2023-04-19T01:50:11.476371Z","iopub.status.idle":"2023-04-19T01:50:12.023641Z","shell.execute_reply.started":"2023-04-19T01:50:11.476307Z","shell.execute_reply":"2023-04-19T01:50:12.022202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def datagather(data):\n    \n    data_set = pd.DataFrame()\n    \n    for file in data:\n        \n        f = pd.read_csv(file)\n        \n        try:\n            if f.loc[:,['AccV','AccML','AccAP','StartHesitation','Turn','Walking']].equals(data_set):\n                pass\n            else:\n                try:\n                    df = f.loc[((f['Valid'] == True) & (f['Task'] == True)),['Time','AccV','AccML','AccAP','StartHesitation','Turn','Walking']]\n                    data_set = data_set.append(df, ignore_index = True)\n                    continue\n                except: pass\n                try:\n                    df = f.loc[((f['Valid'] == False) | (f['Task'] == False)),['Time','AccV','AccML','AccAP','StartHesitation','Turn','Walking']]\n                    continue\n                except: pass\n                try:\n                    df = f.loc[:,['Time','AccV','AccML','AccAP','StartHesitation','Turn','Walking']]\n                    data_set = data_set.append(df, ignore_index = True)\n                except: pass\n        except: \n            #print(\"dup\")\n            pass\n\n    return data_set","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:50:12.027655Z","iopub.execute_input":"2023-04-19T01:50:12.028138Z","iopub.status.idle":"2023-04-19T01:50:12.041026Z","shell.execute_reply.started":"2023-04-19T01:50:12.028088Z","shell.execute_reply":"2023-04-19T01:50:12.039434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = datagather(train_data)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:50:12.045685Z","iopub.execute_input":"2023-04-19T01:50:12.046151Z","iopub.status.idle":"2023-04-19T01:53:46.836203Z","shell.execute_reply.started":"2023-04-19T01:50:12.046117Z","shell.execute_reply":"2023-04-19T01:53:46.834892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.tail()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:53:46.838464Z","iopub.execute_input":"2023-04-19T01:53:46.839129Z","iopub.status.idle":"2023-04-19T01:53:46.851841Z","shell.execute_reply.started":"2023-04-19T01:53:46.839091Z","shell.execute_reply":"2023-04-19T01:53:46.850489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(data)\nm, n = data.shape\n\nnp.random.shuffle(data)\n\ndata_dev = data[0:4000].T\nY_dev = data_dev[4:m]\nX_dev = data_dev[1:4]\n\ndata_train = data[4000:m].T\nY_train = data_train[4:m]\nX_train = data_train[1:4]","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:53:46.852823Z","iopub.execute_input":"2023-04-19T01:53:46.853149Z","iopub.status.idle":"2023-04-19T01:54:01.749141Z","shell.execute_reply.started":"2023-04-19T01:53:46.853118Z","shell.execute_reply":"2023-04-19T01:54:01.747922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:54:01.750548Z","iopub.execute_input":"2023-04-19T01:54:01.750893Z","iopub.status.idle":"2023-04-19T01:54:01.757744Z","shell.execute_reply.started":"2023-04-19T01:54:01.750858Z","shell.execute_reply":"2023-04-19T01:54:01.756556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:54:01.759258Z","iopub.execute_input":"2023-04-19T01:54:01.759925Z","iopub.status.idle":"2023-04-19T01:54:01.772431Z","shell.execute_reply.started":"2023-04-19T01:54:01.759885Z","shell.execute_reply":"2023-04-19T01:54:01.771196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_params():\n    W1 = np.random.randn(10,3)\n    b1 = np.random.randn(10, 1) \n    W2 = np.random.randn(3,10) \n    b2 = np.random.randn(3, 1) \n    return W1, b1, W2, b2\n\ndef ReLU(Z):\n    return np.maximum(Z, 0)\n    # creates the ReLU function\n    \ndef softmax(Z):\n    A = np.exp(Z) / sum(np.exp(Z))\n    return np.nan_to_num(A,nan=0)\n    # creates the probability function\n\ndef foward_prop(W1, b1, W2, b2, X):\n    Z1 = W1.dot(X) + b1\n    # .dot() is a matrix operation \n    A1 = ReLU(Z1)\n    Z2 = W2.dot(A1) + b2\n    A2 = softmax(Z2)\n    return Z1, A1, Z2, A2\n\ndef one_hot(Y):\n    one_hot_Y = np.zeros((Y.size, Y.max() + 1))\n    # creates a matrix with appropriate sizing\n    one_hot_Y[np.arange(Y.size), Y] = 1\n    # looks through and sets to 1\n    one_hot_Y = one_hot_Y.T\n    # transposes\n    return one_hot_Y\n\ndef deriv_ReLU(Z):\n    return Z > 0\n    # true = 1 false = 0, bolean and numbers line up\n\ndef back_prop(Z1, A1, Z2, A2, W1, W2, X, Y):\n    m = Y.size\n    one_hot_Y = Y\n    dZ2 = A2 - one_hot_Y\n    dW2 = 1 / m * dZ2.dot(A1.T)\n    db2 = 1 / m * np.sum(dZ2)\n    dZ1 = W2.T.dot(dZ2) * deriv_ReLU(Z1)\n    dW1 = 1/ m * dZ1.dot(X.T)\n    db1 = 1/ m * np.sum(dZ1)\n    return dW1, db1, dW2, db2\n\ndef update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha):\n    W1 = W1 - alpha * dW1\n    b1 = b1 - alpha * db1\n    W2 = W2 - alpha * dW2\n    b2 = b2 - alpha * db2\n    return W1, b1, W2, b2\n    ","metadata":{"execution":{"iopub.status.busy":"2023-04-19T01:54:01.774271Z","iopub.execute_input":"2023-04-19T01:54:01.774740Z","iopub.status.idle":"2023-04-19T01:54:01.790778Z","shell.execute_reply.started":"2023-04-19T01:54:01.774703Z","shell.execute_reply":"2023-04-19T01:54:01.789564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_predictions(A2):\n    #print(\"Prediction: \\n\", np.round(A2))\n    #print(\"argmax of A2 \\n\", np.argmax(A2, axis=0))\n    return np.argmax(A2, axis=0)\n    #return np.nan_to_num(A2,nan=0)\n\ndef get_accuracy(predictions, Y):\n    #print(\"Actual: \\n\", Y)\n    Y = np.argmax(Y, axis = 0)\n    #print(\"postargmax Y \\n\", Y)\n    #print(\"np.sum of argmax A2 \\n\", np.sum(predictions == Y))\n    #print(np.vstack((np.round(predictions),np.argmax(Y, 0))))\n    return np.sum(predictions == Y) / Y.size\n    #return np.dot(vec1, vec2.T) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))\n    \ndef gradient_descent(X, Y, iterations, alpha):\n    W1, b1, W2, b2 = init_params()\n    # set weights and biases to initial values\n    \n    x_val = []\n    y_val = []\n    \n    for i in range(iterations):\n        #creates a loop\n        Z1, A1, Z2, A2 = foward_prop(W1, b1, W2, b2, X)\n        dW1,db1,dW2,db2 = back_prop(Z1, A1, Z2, A2, W1, W2, X, Y)\n        # calculus for deriv\n        W1,b1,W2,b2 = update_params(W1, b1, W2, b2, dW1, db1, dW2, db2, alpha)\n        # update our weights and biases\n        \n        if i % 10 == 0:\n            # mod 10 means every 10\n            print(\"Iteration: \", i)\n            a = get_accuracy(get_predictions(A2), Y)\n            print(\"Accuracy: \", a)\n            \n            x_val.append(i)\n            y_val.append(a)\n    \n    plot(x_val,y_val)\n    \n    return W1,b1,W2,b2\n\ndef plot(x, y):\n    \n    fig, ax = plt.subplots()\n    ax.plot(x, y)\n    \n    ax.set_xlabel('Iterations')\n    ax.set_ylabel('Accuracy')\n    ax.set_title('Rate of Improvment')\n    \n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-19T02:58:11.328574Z","iopub.execute_input":"2023-04-19T02:58:11.329152Z","iopub.status.idle":"2023-04-19T02:58:11.342691Z","shell.execute_reply.started":"2023-04-19T02:58:11.329102Z","shell.execute_reply":"2023-04-19T02:58:11.341276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, b1, W2, b2 = gradient_descent(X_train, Y_train, 200 , 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:26:45.683880Z","iopub.execute_input":"2023-04-19T03:26:45.684882Z","iopub.status.idle":"2023-04-19T03:35:59.747012Z","shell.execute_reply.started":"2023-04-19T03:26:45.684821Z","shell.execute_reply":"2023-04-19T03:35:59.745608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check(X,Y):\n    Z1, A1, Z2, A2 = foward_prop(W1, b1, W2, b2, X)\n    print(\"Accuracy: \", get_accuracy(get_predictions(A2), Y))","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:36:08.960658Z","iopub.execute_input":"2023-04-19T03:36:08.961206Z","iopub.status.idle":"2023-04-19T03:36:08.968935Z","shell.execute_reply.started":"2023-04-19T03:36:08.961164Z","shell.execute_reply":"2023-04-19T03:36:08.967589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check(X_dev, Y_dev)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T03:36:11.461495Z","iopub.execute_input":"2023-04-19T03:36:11.462007Z","iopub.status.idle":"2023-04-19T03:36:11.474170Z","shell.execute_reply.started":"2023-04-19T03:36:11.461969Z","shell.execute_reply":"2023-04-19T03:36:11.471982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T02:50:28.918177Z","iopub.execute_input":"2023-04-19T02:50:28.918522Z","iopub.status.idle":"2023-04-19T02:50:28.926555Z","shell.execute_reply.started":"2023-04-19T02:50:28.918490Z","shell.execute_reply":"2023-04-19T02:50:28.925285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def comp(data):\n\n    data_set = pd.DataFrame([],columns = ['Id','StartHesitation','Turn','Walking'])\n    \n    for file in data:\n        f = pd.read_csv(file) \n        df = np.array(f.loc[:,['AccV','AccML','AccAP']]).T\n        \n        #print(df)\n        \n        Z1, A1, Z2, A2 = foward_prop(W1, b1, W2, b2, df)\n        \n        #print(np.round(A2)) \n        \n        Time = f['Time']\n        #print(Time.shape)\n        \n        SeriesId = pd.DataFrame(([file.split(\"/\")[-1].split(\".\")[0]] * Time.size), columns = ['Id'])\n        \n        Id = pd.concat([SeriesId, Time.astype(str)], axis = 1)\n        \n        Id = Id.apply(lambda x: '_'.join(x), axis=1)\n        Id = Id.drop(columns=[0,1])\n        \n        #print(SeriesId)\n        \n        output = pd.DataFrame((np.round(A2).T).astype(int), columns = ['StartHesitation','Turn','Walking'])\n        output[\"Id\"] = Id\n        \n        \n        \n        #data_set = data_set.append(output,ignore_index = True)\n        data_set = pd.concat([output, data_set], ignore_index=True)\n        data_set = data_set[['Id','StartHesitation','Turn','Walking']]\n        \n        #print(output.shape)\n        #data_set = data_set.append(SeriesId, ignore_index = True)\n    \n    return data_set","metadata":{"execution":{"iopub.status.busy":"2023-04-19T04:40:46.984547Z","iopub.execute_input":"2023-04-19T04:40:46.985605Z","iopub.status.idle":"2023-04-19T04:40:46.999324Z","shell.execute_reply.started":"2023-04-19T04:40:46.985549Z","shell.execute_reply":"2023-04-19T04:40:46.998022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2023-04-19T04:38:12.292943Z","iopub.execute_input":"2023-04-19T04:38:12.293347Z","iopub.status.idle":"2023-04-19T04:38:12.310891Z","shell.execute_reply.started":"2023-04-19T04:38:12.293312Z","shell.execute_reply":"2023-04-19T04:38:12.309459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm = comp(test_data)\n\nsubm\n","metadata":{"execution":{"iopub.status.busy":"2023-04-19T04:40:50.260161Z","iopub.execute_input":"2023-04-19T04:40:50.261049Z","iopub.status.idle":"2023-04-19T04:40:53.824515Z","shell.execute_reply.started":"2023-04-19T04:40:50.261005Z","shell.execute_reply":"2023-04-19T04:40:53.823139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T04:41:01.673901Z","iopub.execute_input":"2023-04-19T04:41:01.674325Z","iopub.status.idle":"2023-04-19T04:41:02.156238Z","shell.execute_reply.started":"2023-04-19T04:41:01.674287Z","shell.execute_reply":"2023-04-19T04:41:02.154828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm = pd.read_csv('/kaggle/working/submission.csv')\nsubm","metadata":{"execution":{"iopub.status.busy":"2023-04-19T04:41:03.396877Z","iopub.execute_input":"2023-04-19T04:41:03.397320Z","iopub.status.idle":"2023-04-19T04:41:03.574047Z","shell.execute_reply.started":"2023-04-19T04:41:03.397282Z","shell.execute_reply":"2023-04-19T04:41:03.572807Z"},"trusted":true},"execution_count":null,"outputs":[]}]}