{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Harmful Brain Activity - Long-Short Term Memory (LSTM) Model**\n\n## **Written by:** [Aarish Asif Khan](https://www.kaggle.com/aarishasifkhan)\n\n## **Date:** 28th March 2024\n\n## **Dataset:** [HMS - Harmful Brain Activity](https://www.kaggle.com/competitions/hms-harmful-brain-activity-classification)","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense, Dropout\nfrom tensorflow.keras.callbacks import TensorBoard\n","metadata":{"execution":{"iopub.status.busy":"2024-03-28T16:00:12.722474Z","iopub.execute_input":"2024-03-28T16:00:12.723836Z","iopub.status.idle":"2024-03-28T16:00:12.735539Z","shell.execute_reply.started":"2024-03-28T16:00:12.723793Z","shell.execute_reply":"2024-03-28T16:00:12.734084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\ntrain_data = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:24.455964Z","iopub.execute_input":"2024-03-28T15:59:24.456649Z","iopub.status.idle":"2024-03-28T15:59:24.753341Z","shell.execute_reply.started":"2024-03-28T15:59:24.456614Z","shell.execute_reply":"2024-03-28T15:59:24.752377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into features (X) and labels (y)\nX = train_data.drop(columns=['eeg_id', 'eeg_sub_id', 'spectrogram_id', 'spectrogram_sub_id', 'label_id', 'patient_id', 'expert_consensus'])\ny = train_data[['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']]","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:24.754460Z","iopub.execute_input":"2024-03-28T15:59:24.754971Z","iopub.status.idle":"2024-03-28T15:59:24.772917Z","shell.execute_reply.started":"2024-03-28T15:59:24.754943Z","shell.execute_reply":"2024-03-28T15:59:24.771671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:24.775658Z","iopub.execute_input":"2024-03-28T15:59:24.776023Z","iopub.status.idle":"2024-03-28T15:59:24.802007Z","shell.execute_reply.started":"2024-03-28T15:59:24.775993Z","shell.execute_reply":"2024-03-28T15:59:24.800749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scale features\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_val_scaled = scaler.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:24.803317Z","iopub.execute_input":"2024-03-28T15:59:24.803830Z","iopub.status.idle":"2024-03-28T15:59:24.828446Z","shell.execute_reply.started":"2024-03-28T15:59:24.803802Z","shell.execute_reply":"2024-03-28T15:59:24.827386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape X_train_scaled and X_val_scaled\nX_train_reshaped = X_train_scaled.reshape((X_train_scaled.shape[0], X_train_scaled.shape[1], 1))\nX_val_reshaped = X_val_scaled.reshape((X_val_scaled.shape[0], X_val_scaled.shape[1], 1))","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:24.830172Z","iopub.execute_input":"2024-03-28T15:59:24.830512Z","iopub.status.idle":"2024-03-28T15:59:24.836561Z","shell.execute_reply.started":"2024-03-28T15:59:24.830485Z","shell.execute_reply":"2024-03-28T15:59:24.835390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the LSTM model\nmodel = Sequential([\n    LSTM(128, return_sequences=True, input_shape=(X_train_reshaped.shape[1], X_train_reshaped.shape[2])),\n    Dropout(0.2),\n    LSTM(64, return_sequences=True),\n    Dropout(0.2),\n    LSTM(32),\n    Dropout(0.2),\n    Dense(64, activation='relu'),\n    Dense(32, activation='relu'),\n    Dense(6, activation='softmax')  # Assuming there are 6 output classes\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:24.838433Z","iopub.execute_input":"2024-03-28T15:59:24.838789Z","iopub.status.idle":"2024-03-28T15:59:25.028505Z","shell.execute_reply.started":"2024-03-28T15:59:24.838761Z","shell.execute_reply":"2024-03-28T15:59:25.026114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:25.031085Z","iopub.execute_input":"2024-03-28T15:59:25.031442Z","iopub.status.idle":"2024-03-28T15:59:25.047911Z","shell.execute_reply.started":"2024-03-28T15:59:25.031414Z","shell.execute_reply":"2024-03-28T15:59:25.046421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define TensorBoard callback\ntensorboard_callback = TensorBoard(log_dir=\"./logs_lstm\")","metadata":{"execution":{"iopub.status.busy":"2024-03-28T16:00:20.397589Z","iopub.execute_input":"2024-03-28T16:00:20.398005Z","iopub.status.idle":"2024-03-28T16:00:20.403866Z","shell.execute_reply.started":"2024-03-28T16:00:20.397976Z","shell.execute_reply":"2024-03-28T16:00:20.402333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model with TensorBoard callback\nhistory = model.fit(X_train_reshaped, y_train, epochs=10, batch_size=32, validation_data=(X_val_reshaped, y_val), callbacks=[tensorboard_callback])","metadata":{"execution":{"iopub.status.busy":"2024-03-28T16:00:22.389371Z","iopub.execute_input":"2024-03-28T16:00:22.389793Z","iopub.status.idle":"2024-03-28T16:11:07.644430Z","shell.execute_reply.started":"2024-03-28T16:00:22.389765Z","shell.execute_reply":"2024-03-28T16:11:07.643180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the validation set\nval_loss, val_accuracy = model.evaluate(X_val_reshaped, y_val)\nprint(\"Validation Loss:\", val_loss)\nprint(\"Validation Accuracy:\", val_accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T15:59:25.942159Z","iopub.status.idle":"2024-03-28T15:59:25.942622Z","shell.execute_reply.started":"2024-03-28T15:59:25.942412Z","shell.execute_reply":"2024-03-28T15:59:25.942431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext tensorboard\n%tensorboard --logdir=./logs","metadata":{},"execution_count":null,"outputs":[]}]}