{"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":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Harmful Brain Activity - Creating a Recurrent Neural Network**\n\n## **Written by:** [Aarish Asif Khan](https://github.com/aarish47)\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 pandas as pd\nimport numpy as np\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.layers import SimpleRNN","metadata":{},"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-28T14:42:43.779086Z","iopub.execute_input":"2024-03-28T14:42:43.779487Z","iopub.status.idle":"2024-03-28T14:42:44.156969Z","shell.execute_reply.started":"2024-03-28T14:42:43.779454Z","shell.execute_reply":"2024-03-28T14:42:44.155104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = 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-28T14:42:44.157684Z","iopub.status.idle":"2024-03-28T14:42:44.158058Z","shell.execute_reply.started":"2024-03-28T14:42:44.157865Z","shell.execute_reply":"2024-03-28T14:42:44.157881Z"},"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-28T14:42:44.159463Z","iopub.status.idle":"2024-03-28T14:42:44.159902Z","shell.execute_reply.started":"2024-03-28T14:42:44.159696Z","shell.execute_reply":"2024-03-28T14:42:44.159713Z"},"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-28T14:42:44.161211Z","iopub.status.idle":"2024-03-28T14:42:44.161617Z","shell.execute_reply.started":"2024-03-28T14:42:44.161421Z","shell.execute_reply":"2024-03-28T14:42:44.161440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape X_train_scaled and X_val_scaled for RNN input\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))\n\n# Define RNN model\nmodel = Sequential([\n    SimpleRNN(64, input_shape=(X_train_reshaped.shape[1], X_train_reshaped.shape[2])),\n    Dropout(0.2),\n    Dense(64, activation='relu'),\n    Dense(6, activation='softmax')  # Assuming there are 6 classes for the labels\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:42:44.162994Z","iopub.status.idle":"2024-03-28T14:42:44.163489Z","shell.execute_reply.started":"2024-03-28T14:42:44.163256Z","shell.execute_reply":"2024-03-28T14:42:44.163275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile model\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:42:44.164555Z","iopub.status.idle":"2024-03-28T14:42:44.164999Z","shell.execute_reply.started":"2024-03-28T14:42:44.164768Z","shell.execute_reply":"2024-03-28T14:42:44.164787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train model\nhistory = model.fit(X_train_scaled, y_train, epochs=10, batch_size=32, validation_data=(X_val_scaled, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:42:44.166271Z","iopub.status.idle":"2024-03-28T14:42:44.166583Z","shell.execute_reply.started":"2024-03-28T14:42:44.166425Z","shell.execute_reply":"2024-03-28T14:42:44.166438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation\n# Evaluate model on validation set\nval_loss, val_acc = model.evaluate(X_val_scaled, y_val)\nprint(\"Validation Loss:\", val_loss)\nprint(\"Validation Accuracy:\", val_acc)","metadata":{"execution":{"iopub.status.busy":"2024-03-28T14:42:44.167850Z","iopub.status.idle":"2024-03-28T14:42:44.168201Z","shell.execute_reply.started":"2024-03-28T14:42:44.168002Z","shell.execute_reply":"2024-03-28T14:42:44.168015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on validation set\npredictions = model.predict(X_val_reshaped)\n\n# Print the first few predictions\nprint(predictions[:5])","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-28T14:42:44.169665Z","iopub.status.idle":"2024-03-28T14:42:44.170005Z","shell.execute_reply.started":"2024-03-28T14:42:44.169844Z","shell.execute_reply":"2024-03-28T14:42:44.169869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}