{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Harmful Brain Activity Classification - MICE Imputation**\n\n## **Written by:** [Aarish Asif Khan](https://www.kaggle.com/aarishasifkhan)\n\n## **Date:** 23 February 2024\n\n## **Dataset:** [HMS - Harmful Brain Activity Dataset](https://www.kaggle.com/competitions/hms-harmful-brain-activity-classification)","metadata":{}},{"cell_type":"code","source":"# Import libraries\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom sklearn.experimental import enable_iterative_imputer\nfrom sklearn.impute import IterativeImputer\nfrom sklearn.model_selection import train_test_split\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn import tree\n\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.448601Z","iopub.execute_input":"2024-03-18T12:44:23.449009Z","iopub.status.idle":"2024-03-18T12:44:23.456349Z","shell.execute_reply.started":"2024-03-18T12:44:23.448979Z","shell.execute_reply":"2024-03-18T12:44:23.454928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the dataset\ntrain_data = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\n\n# Print the 5 rows of the dataset\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.458591Z","iopub.execute_input":"2024-03-18T12:44:23.458958Z","iopub.status.idle":"2024-03-18T12:44:23.714173Z","shell.execute_reply.started":"2024-03-18T12:44:23.458926Z","shell.execute_reply":"2024-03-18T12:44:23.712831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Handle missing values if any (fill with mode for simplicity)\ntrain_data.fillna(train_data.mode().iloc[0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.716228Z","iopub.execute_input":"2024-03-18T12:44:23.716632Z","iopub.status.idle":"2024-03-18T12:44:23.862045Z","shell.execute_reply.started":"2024-03-18T12:44:23.716598Z","shell.execute_reply":"2024-03-18T12:44:23.861018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop irrelevant columns (e.g., IDs)\ntrain_data.drop(['eeg_id', 'eeg_sub_id', 'spectrogram_id', 'spectrogram_sub_id', 'label_id', 'patient_id'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.863512Z","iopub.execute_input":"2024-03-18T12:44:23.863952Z","iopub.status.idle":"2024-03-18T12:44:23.875079Z","shell.execute_reply.started":"2024-03-18T12:44:23.863914Z","shell.execute_reply":"2024-03-18T12:44:23.873481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert categorical variables to numerical using LabelEncoder\nlabel_encoder = LabelEncoder()\n\ncategorical_columns = train_data.select_dtypes(include=['object']).columns\nfor column in categorical_columns:\n    train_data[column] = label_encoder.fit_transform(train_data[column])","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.878205Z","iopub.execute_input":"2024-03-18T12:44:23.878747Z","iopub.status.idle":"2024-03-18T12:44:23.918846Z","shell.execute_reply.started":"2024-03-18T12:44:23.878699Z","shell.execute_reply":"2024-03-18T12:44:23.917431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform MICE imputation\nmice_imputer = IterativeImputer()","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.921059Z","iopub.execute_input":"2024-03-18T12:44:23.921423Z","iopub.status.idle":"2024-03-18T12:44:23.926511Z","shell.execute_reply.started":"2024-03-18T12:44:23.921390Z","shell.execute_reply":"2024-03-18T12:44:23.925398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imputed_train = mice_imputer.fit_transform(train_data)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:23.928016Z","iopub.execute_input":"2024-03-18T12:44:23.928461Z","iopub.status.idle":"2024-03-18T12:44:25.222163Z","shell.execute_reply.started":"2024-03-18T12:44:23.928421Z","shell.execute_reply":"2024-03-18T12:44:25.220570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert imputed array back to DataFrame\nimputed_train_df = pd.DataFrame(imputed_train, columns=train_data.columns)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:25.224755Z","iopub.execute_input":"2024-03-18T12:44:25.227775Z","iopub.status.idle":"2024-03-18T12:44:25.238135Z","shell.execute_reply.started":"2024-03-18T12:44:25.227718Z","shell.execute_reply":"2024-03-18T12:44:25.236583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the dataset into features and target\nX = imputed_train_df.drop('expert_consensus', axis=1)\ny = imputed_train_df['expert_consensus']","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:25.240721Z","iopub.execute_input":"2024-03-18T12:44:25.242156Z","iopub.status.idle":"2024-03-18T12:44:25.256029Z","shell.execute_reply.started":"2024-03-18T12:44:25.242097Z","shell.execute_reply":"2024-03-18T12:44:25.254958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the dataset into train and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:25.257586Z","iopub.execute_input":"2024-03-18T12:44:25.258503Z","iopub.status.idle":"2024-03-18T12:44:25.286792Z","shell.execute_reply.started":"2024-03-18T12:44:25.258450Z","shell.execute_reply":"2024-03-18T12:44:25.285641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize and train a RandomForestClassifier\nclf = RandomForestClassifier()\nclf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:25.290923Z","iopub.execute_input":"2024-03-18T12:44:25.291783Z","iopub.status.idle":"2024-03-18T12:44:32.795306Z","shell.execute_reply.started":"2024-03-18T12:44:25.291738Z","shell.execute_reply":"2024-03-18T12:44:32.794127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions\ny_pred = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:32.797095Z","iopub.execute_input":"2024-03-18T12:44:32.797828Z","iopub.status.idle":"2024-03-18T12:44:33.125807Z","shell.execute_reply.started":"2024-03-18T12:44:32.797787Z","shell.execute_reply":"2024-03-18T12:44:33.124228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\naccuracy = accuracy_score(y_test, y_pred)\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:33.127703Z","iopub.execute_input":"2024-03-18T12:44:33.128072Z","iopub.status.idle":"2024-03-18T12:44:33.138824Z","shell.execute_reply.started":"2024-03-18T12:44:33.128039Z","shell.execute_reply":"2024-03-18T12:44:33.137536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get feature importances from the trained Random Forest classifier\nimportances = clf.feature_importances_\n\n# Get the names of features\nfeature_names = X.columns\n\n# Sort feature importances in descending order\nindices = importances.argsort()[::-1]\n\n# Plot the feature importances\nplt.figure(figsize=(10, 6))\nplt.title(\"Feature Importances\")\nplt.bar(range(X.shape[1]), importances[indices], align=\"center\")\nplt.xticks(range(X.shape[1]), feature_names[indices], rotation=90)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-18T12:44:33.140874Z","iopub.execute_input":"2024-03-18T12:44:33.141529Z","iopub.status.idle":"2024-03-18T12:44:33.624790Z","shell.execute_reply.started":"2024-03-18T12:44:33.141494Z","shell.execute_reply":"2024-03-18T12:44:33.623528Z"},"trusted":true},"execution_count":null,"outputs":[]}]}