{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-30T08:13:22.038549Z","iopub.execute_input":"2021-10-30T08:13:22.039295Z","iopub.status.idle":"2021-10-30T08:13:23.087185Z","shell.execute_reply.started":"2021-10-30T08:13:22.039194Z","shell.execute_reply":"2021-10-30T08:13:23.08642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\nFILE_PATH = '../input/grasp-and-lift-eeg-detection'\nlist_dir = os.listdir(FILE_PATH)\n\nfor zipfile in list_dir:\n    with ZipFile(os.path.join(FILE_PATH, zipfile), 'r') as z:\n        z.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:13:26.750674Z","iopub.execute_input":"2021-10-30T08:13:26.750992Z","iopub.status.idle":"2021-10-30T08:14:17.398045Z","shell.execute_reply.started":"2021-10-30T08:13:26.75096Z","shell.execute_reply":"2021-10-30T08:14:17.396401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\ndef read_csv(data, events):\n    x = pd.read_csv(data)\n    y = pd.read_csv(events)\n    id = '_'.join(x.iloc[0, 0].split('_')[:-1])\n    x = x.iloc[:,1:].values\n    y = y.iloc[:,1:].values\n    return x, y\n    \n\ntrainset = []\ngt = []\nfor filename in tqdm(os.listdir('./train')):\n    if 'data' in filename:\n        data_file_name = os.path.join('./train', filename)\n        id = filename.split('.')[0]\n        events_file_name = os.path.join('./train', '_'.join(id.split('_')[:-1]) + '_events.csv')\n        x, y = read_csv(data_file_name, events_file_name)\n        trainset.append(x.T.astype(np.float32))\n        gt.append(y.T.astype(np.float32))\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:15:11.13489Z","iopub.status.idle":"2021-10-30T08:15:11.135516Z","shell.execute_reply.started":"2021-10-30T08:15:11.135324Z","shell.execute_reply":"2021-10-30T08:15:11.135346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset= np.array(trainset)\ngt = np.array(gt)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:15:11.138157Z","iopub.status.idle":"2021-10-30T08:15:11.138472Z","shell.execute_reply.started":"2021-10-30T08:15:11.138299Z","shell.execute_reply":"2021-10-30T08:15:11.138322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = np.argmax(y, axis=1)\nnp.shape(Y)\n\nclf = LinearDiscriminantAnalysis()\n\nclf.fit(x,Y)\n\nprint(clf.coef_/np.linalg.norm(clf.coef_))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x,y in (trainset,gt):\n    print(x,y)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.predict(x[2731].reshape(1,-1))","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:22:47.129947Z","iopub.execute_input":"2021-10-30T08:22:47.130209Z","iopub.status.idle":"2021-10-30T08:22:47.136386Z","shell.execute_reply.started":"2021-10-30T08:22:47.130182Z","shell.execute_reply":"2021-10-30T08:22:47.135498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[2731]","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:22:58.686326Z","iopub.execute_input":"2021-10-30T08:22:58.687125Z","iopub.status.idle":"2021-10-30T08:22:58.69429Z","shell.execute_reply.started":"2021-10-30T08:22:58.687083Z","shell.execute_reply":"2021-10-30T08:22:58.69348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nclf1 = SVC(kernel = 'linear', C=1e5)\n\nclf1.fit(x,Y)\n\nw = clf.coef_\nb= clf.intercept_\n\nprint('w= ',w)\nprint('b= ',b)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:37:56.750109Z","iopub.execute_input":"2021-10-30T08:37:56.750563Z","iopub.status.idle":"2021-10-30T08:37:56.755699Z","shell.execute_reply.started":"2021-10-30T08:37:56.750517Z","shell.execute_reply":"2021-10-30T08:37:56.754849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam \nfrom keras.layers import Lambda, Conv1D, Dropout, Dense, Flatten, MaxPooling1D, GlobalAveragePooling1D\nfrom keras.regularizers import l2\n\nX_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:35:12.621649Z","iopub.execute_input":"2021-10-30T08:35:12.62248Z","iopub.status.idle":"2021-10-30T08:35:12.720477Z","shell.execute_reply.started":"2021-10-30T08:35:12.622437Z","shell.execute_reply":"2021-10-30T08:35:12.719704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(12, input_dim=32, activation='relu'))\nmodel.add(Dense(8, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:36:23.801209Z","iopub.execute_input":"2021-10-30T08:36:23.802085Z","iopub.status.idle":"2021-10-30T08:36:23.833554Z","shell.execute_reply.started":"2021-10-30T08:36:23.802037Z","shell.execute_reply":"2021-10-30T08:36:23.832887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:36:27.129454Z","iopub.execute_input":"2021-10-30T08:36:27.130168Z","iopub.status.idle":"2021-10-30T08:36:27.139574Z","shell.execute_reply.started":"2021-10-30T08:36:27.130133Z","shell.execute_reply":"2021-10-30T08:36:27.138885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit the keras model on the dataset\nmodel.fit(X_train, y_train, epochs=150, batch_size=10)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:37:34.486422Z","iopub.execute_input":"2021-10-30T08:37:34.486859Z","iopub.status.idle":"2021-10-30T08:37:34.611644Z","shell.execute_reply.started":"2021-10-30T08:37:34.486826Z","shell.execute_reply":"2021-10-30T08:37:34.610332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv1D(16,3, strides=2, padding='same', activation='relu', input_shape=(2064,95)))\nmodel.add(Dropout(0.4))\nmodel.add(MaxPooling1D(2,2))\nmodel.add(Flatten())\n#model.add(tf.keras.layers.BatchNormalization())\nmodel.add(Dense(3, activation='softmax'))\n\nopt = tf.keras.optimizers.Adam(learning_rate=0.01)\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=opt,\n              metrics=['accuracy'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Model\nH=model.fit(X_train, y_train, validation_data=(X_test, y_test),batch_size=100, epochs=100)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T08:27:01.522193Z","iopub.execute_input":"2021-10-30T08:27:01.522879Z","iopub.status.idle":"2021-10-30T08:27:01.546329Z","shell.execute_reply.started":"2021-10-30T08:27:01.522829Z","shell.execute_reply":"2021-10-30T08:27:01.545257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(H.history['accuracy'],label='accuracy')\nplt.plot(H.history['val_accuracy'],label='val_accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()","metadata":{},"execution_count":null,"outputs":[]}]}