{"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":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt \nimport seaborn as sns ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-12T14:28:42.138502Z","iopub.execute_input":"2024-02-12T14:28:42.139554Z","iopub.status.idle":"2024-02-12T14:28:43.241137Z","shell.execute_reply.started":"2024-02-12T14:28:42.139509Z","shell.execute_reply":"2024-02-12T14:28:43.239289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.243611Z","iopub.execute_input":"2024-02-12T14:28:43.244157Z","iopub.status.idle":"2024-02-12T14:28:43.497654Z","shell.execute_reply.started":"2024-02-12T14:28:43.244120Z","shell.execute_reply":"2024-02-12T14:28:43.496409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.499259Z","iopub.execute_input":"2024-02-12T14:28:43.500674Z","iopub.status.idle":"2024-02-12T14:28:43.526639Z","shell.execute_reply.started":"2024-02-12T14:28:43.500628Z","shell.execute_reply":"2024-02-12T14:28:43.525340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/test.csv')\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.529081Z","iopub.execute_input":"2024-02-12T14:28:43.529458Z","iopub.status.idle":"2024-02-12T14:28:43.542801Z","shell.execute_reply.started":"2024-02-12T14:28:43.529425Z","shell.execute_reply":"2024-02-12T14:28:43.541800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/sample_submission.csv')\nprint(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.544173Z","iopub.execute_input":"2024-02-12T14:28:43.544642Z","iopub.status.idle":"2024-02-12T14:28:43.575568Z","shell.execute_reply.started":"2024-02-12T14:28:43.544609Z","shell.execute_reply":"2024-02-12T14:28:43.574342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.577031Z","iopub.execute_input":"2024-02-12T14:28:43.578176Z","iopub.status.idle":"2024-02-12T14:28:43.615404Z","shell.execute_reply.started":"2024-02-12T14:28:43.578125Z","shell.execute_reply":"2024-02-12T14:28:43.614122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def counts(col):\n    return len(train[col].value_counts())\n\nfor col in train.columns:\n    print(f\"{counts(col)} <- {col}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.617126Z","iopub.execute_input":"2024-02-12T14:28:43.617970Z","iopub.status.idle":"2024-02-12T14:28:43.692506Z","shell.execute_reply.started":"2024-02-12T14:28:43.617923Z","shell.execute_reply":"2024-02-12T14:28:43.691339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\n\ntrain['expert_consensus'] = encoder.fit_transform(train['expert_consensus'])\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.693778Z","iopub.execute_input":"2024-02-12T14:28:43.694844Z","iopub.status.idle":"2024-02-12T14:28:43.908410Z","shell.execute_reply.started":"2024-02-12T14:28:43.694807Z","shell.execute_reply":"2024-02-12T14:28:43.907063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.909968Z","iopub.execute_input":"2024-02-12T14:28:43.910352Z","iopub.status.idle":"2024-02-12T14:28:43.932223Z","shell.execute_reply.started":"2024-02-12T14:28:43.910316Z","shell.execute_reply":"2024-02-12T14:28:43.931065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = ['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.936828Z","iopub.execute_input":"2024-02-12T14:28:43.938048Z","iopub.status.idle":"2024-02-12T14:28:43.943755Z","shell.execute_reply.started":"2024-02-12T14:28:43.938007Z","shell.execute_reply":"2024-02-12T14:28:43.942569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotgraph(x, y, data):\n    sns.scatterplot(data=data, x=x, y=y)\n    plt.xlabel(f'{x}_feature')\n    plt.ylabel(f'{y}_feature')\n    plt.title(f'Visualization of {x} and {y}')\n    plt.show()\n\ncolumn = train.columns\nfor x in range(len(column)):\n    for y in range(x+1, len(column)):\n        plotgraph(column[x], column[y], train)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:28:43.945555Z","iopub.execute_input":"2024-02-12T14:28:43.946074Z","iopub.status.idle":"2024-02-12T14:29:37.477104Z","shell.execute_reply.started":"2024-02-12T14:28:43.946035Z","shell.execute_reply":"2024-02-12T14:29:37.475844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correlationGraph(data, target):\n    correlation = data.corr(method='pearson')\n    data2 = data.drop(columns=[target], axis=1)\n    data2.corrwith(data[target]).plot.bar(figsize=(10, 8), title=f'Relation of different features with {target} feature', rot=90, grid=True)\n    plt.show()\n\nfor col in targets:\n    correlationGraph(train, col)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:37.479213Z","iopub.execute_input":"2024-02-12T14:29:37.479649Z","iopub.status.idle":"2024-02-12T14:29:40.432664Z","shell.execute_reply.started":"2024-02-12T14:29:37.479613Z","shell.execute_reply":"2024-02-12T14:29:40.431427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlation = train.corr(method='pearson')\nplt.figure(figsize=(12, 10))\nsns.heatmap(correlation, annot=True, cmap='YlGnBu', fmt='.2f')\nplt.title('Correlation Heatmap')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:40.434356Z","iopub.execute_input":"2024-02-12T14:29:40.435807Z","iopub.status.idle":"2024-02-12T14:29:41.543646Z","shell.execute_reply.started":"2024-02-12T14:29:40.435758Z","shell.execute_reply":"2024-02-12T14:29:41.542460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.drop(columns=targets, axis=1)\ny = train[targets]","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:41.545770Z","iopub.execute_input":"2024-02-12T14:29:41.546708Z","iopub.status.idle":"2024-02-12T14:29:41.558552Z","shell.execute_reply.started":"2024-02-12T14:29:41.546663Z","shell.execute_reply":"2024-02-12T14:29:41.556946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:41.560205Z","iopub.execute_input":"2024-02-12T14:29:41.560707Z","iopub.status.idle":"2024-02-12T14:29:41.566937Z","shell.execute_reply.started":"2024-02-12T14:29:41.560669Z","shell.execute_reply":"2024-02-12T14:29:41.565919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nxtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)\n\nprint('Training set:', xtrain.shape, ytrain.shape)\nprint('Testing set:', xtest.shape, ytest.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:41.568515Z","iopub.execute_input":"2024-02-12T14:29:41.569111Z","iopub.status.idle":"2024-02-12T14:29:41.626263Z","shell.execute_reply.started":"2024-02-12T14:29:41.569077Z","shell.execute_reply":"2024-02-12T14:29:41.625391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\n\nmodel = DecisionTreeClassifier()\nmodel.fit(xtrain, ytrain)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:41.627612Z","iopub.execute_input":"2024-02-12T14:29:41.628162Z","iopub.status.idle":"2024-02-12T14:29:44.585161Z","shell.execute_reply.started":"2024-02-12T14:29:41.628129Z","shell.execute_reply":"2024-02-12T14:29:44.583821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = model.predict(xtest)\n\nfrom sklearn.metrics import r2_score\n\nscore = r2_score(ytest, ypred)\nscore","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:44.587109Z","iopub.execute_input":"2024-02-12T14:29:44.587698Z","iopub.status.idle":"2024-02-12T14:29:44.653603Z","shell.execute_reply.started":"2024-02-12T14:29:44.587644Z","shell.execute_reply":"2024-02-12T14:29:44.650288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\n\nmodel = Sequential()\nmodel.add(Dense(128, activation='relu', input_shape=(xtrain.shape[1],)))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(16, activation='relu'))\nmodel.add(Dense(ytrain.shape[1], activation='linear'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:44.655745Z","iopub.execute_input":"2024-02-12T14:29:44.656251Z","iopub.status.idle":"2024-02-12T14:29:49.250502Z","shell.execute_reply.started":"2024-02-12T14:29:44.656197Z","shell.execute_reply":"2024-02-12T14:29:49.249183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(xtrain, ytrain, epochs=3, batch_size=32, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:29:49.251903Z","iopub.execute_input":"2024-02-12T14:29:49.252259Z","iopub.status.idle":"2024-02-12T14:30:08.662125Z","shell.execute_reply.started":"2024-02-12T14:29:49.252227Z","shell.execute_reply":"2024-02-12T14:30:08.661043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(xtest, ytest)\nprint(f'Test Accuracy: {test_acc}')","metadata":{"execution":{"iopub.status.busy":"2024-02-12T14:30:08.663329Z","iopub.execute_input":"2024-02-12T14:30:08.664312Z","iopub.status.idle":"2024-02-12T14:30:10.043184Z","shell.execute_reply.started":"2024-02-12T14:30:08.664275Z","shell.execute_reply":"2024-02-12T14:30:10.041936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}