Epileptic Seizure Detection


%%html
<style>
@import url('https://fonts.googleapis.com/css?family=Ewert|Roboto&effect=3d|ice|');
body {background-color: gainsboro;}
a {color: #37c9e1; font-family: 'Roboto';}
h1 {color: #37c9e1; font-family: 'Orbitron'; text-shadow: 4px 4px 4px #aaa;}
h2, h3 {color: slategray; font-family: 'Orbitron'; text-shadow: 4px 4px 4px #aaa;}
h4 {color: #818286; font-family: 'Roboto';}
span {font-family:'Roboto'; color:black; text-shadow: 5px 5px 5px #aaa;}  
div.output_area pre{font-family:'Roboto'; font-size:110%; color:lightblue;}      
</style>
import numpy as np
import pandas as pd
df = pd.read_csv('../input/epileptic-seizure-recognition/Epileptic Seizure Recognition.csv')
df.head()
Unnamed X1 X2 X3 X4 X5 X6 X7 X8 X9 ... X170 X171 X172 X173 X174 X175 X176 X177 X178 y
0 X21.V1.791 135 190 229 223 192 125 55 -9 -33 ... -17 -15 -31 -77 -103 -127 -116 -83 -51 4
1 X15.V1.924 386 382 356 331 320 315 307 272 244 ... 164 150 146 152 157 156 154 143 129 1
2 X8.V1.1 -32 -39 -47 -37 -32 -36 -57 -73 -85 ... 57 64 48 19 -12 -30 -35 -35 -36 5
3 X16.V1.60 -105 -101 -96 -92 -89 -95 -102 -100 -87 ... -82 -81 -80 -77 -85 -77 -72 -69 -65 5
4 X20.V1.54 -9 -65 -98 -102 -78 -48 -16 0 -21 ... 4 2 -12 -32 -41 -65 -83 -89 -73 5

5 rows × 180 columns

df['y'].value_counts()
5    2300
4    2300
3    2300
2    2300
1    2300
Name: y, dtype: int64
import seaborn as sns
cols = df.columns
target = df.y
target.unique()
target[target > 1 ] = 0
ax = sns.countplot(target)
non_seizure, seizure = target.value_counts()
print('The number of trials for the non-seizure class is:', non_seizure)
print('The number of trials for the seizure class is:', seizure)
The number of trials for the non-seizure class is: 9200
The number of trials for the seizure class is: 2300


/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  after removing the cwd from sys.path.

png

df.isnull().sum()
Unnamed    0
X1         0
X2         0
X3         0
X4         0
          ..
X175       0
X176       0
X177       0
X178       0
y          0
Length: 180, dtype: int64
y = df.iloc[:,179].values
y
array([0, 1, 0, ..., 0, 0, 0])
y[y>1]=0
y
array([0, 1, 0, ..., 0, 0, 0])
X = df.iloc[:,1:179].values
X.shape
(11500, 178)
from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size = 0.2)
from sklearn.metrics import accuracy_score, classification_report
def evaluate(name, predictions, ytest):
    print(f'Accuracy of {name} : {accuracy_score(predictions, ytest) * 100} %')
    print(classification_report(predictions, ytest, target_names = ['Non Seizure', 'Seizure']))
import warnings
warnings.filterwarnings('ignore')
#1. Logistic Regression
from sklearn.linear_model import LogisticRegression
LR = LogisticRegression()
LR.fit(xtrain, ytrain)
predictions = LR.predict(xtest)
evaluate(LR, predictions, ytest)
Accuracy of LogisticRegression() : 66.52173913043478 %
              precision    recall  f1-score   support

 Non Seizure       0.73      0.83      0.77      1597
     Seizure       0.43      0.30      0.35       703

    accuracy                           0.67      2300
   macro avg       0.58      0.56      0.56      2300
weighted avg       0.64      0.67      0.65      2300
from sklearn.svm import SVC
SV = SVC(kernel = 'rbf', C = 2)
SV.fit(xtrain, ytrain)
predictions = SV.predict(xtest)
evaluate(SV, predictions, ytest)
Accuracy of SVC(C=2) : 97.78260869565217 %
              precision    recall  f1-score   support

 Non Seizure       1.00      0.98      0.99      1844
     Seizure       0.91      0.98      0.95       456

    accuracy                           0.98      2300
   macro avg       0.95      0.98      0.97      2300
weighted avg       0.98      0.98      0.98      2300
from sklearn.tree import DecisionTreeClassifier
DT = DecisionTreeClassifier(criterion = 'entropy', random_state = 0, max_depth = 16)
DT.fit(xtrain, ytrain)
predictions = DT.predict(xtest)
evaluate(DT, predictions, ytest)
Accuracy of DecisionTreeClassifier(criterion='entropy', max_depth=16, random_state=0) : 94.26086956521739 %
              precision    recall  f1-score   support

 Non Seizure       0.97      0.95      0.96      1847
     Seizure       0.83      0.89      0.86       453

    accuracy                           0.94      2300
   macro avg       0.90      0.92      0.91      2300
weighted avg       0.94      0.94      0.94      2300
from sklearn.ensemble import RandomForestClassifier
RF = RandomForestClassifier(n_estimators = 20, random_state = 2, max_depth = 40)
RF.fit(xtrain, ytrain)
predictions = RF.predict(xtest)
evaluate(RF, predictions, ytest)
Accuracy of RandomForestClassifier(max_depth=40, n_estimators=20, random_state=2) : 97.47826086956522 %
              precision    recall  f1-score   support

 Non Seizure       0.98      0.98      0.98      1813
     Seizure       0.94      0.94      0.94       487

    accuracy                           0.97      2300
   macro avg       0.96      0.96      0.96      2300
weighted avg       0.97      0.97      0.97      2300
from sklearn.naive_bayes import GaussianNB
NB = GaussianNB()
NB.fit(xtrain, ytrain)
predictions = NB.predict(xtest)
evaluate(NB, predictions, ytest)
Accuracy of GaussianNB() : 95.78260869565217 %
              precision    recall  f1-score   support

 Non Seizure       0.98      0.97      0.97      1828
     Seizure       0.88      0.92      0.90       472

    accuracy                           0.96      2300
   macro avg       0.93      0.94      0.94      2300
weighted avg       0.96      0.96      0.96      2300
from sklearn.neighbors import KNeighborsClassifier
KNN= KNeighborsClassifier(n_neighbors = 10)
KNN.fit(xtrain, ytrain)
predictions = KNN.predict(xtest)
evaluate(KNN, predictions, ytest)
Accuracy of KNeighborsClassifier(n_neighbors=10) : 90.34782608695652 %
              precision    recall  f1-score   support

 Non Seizure       1.00      0.89      0.94      2031
     Seizure       0.55      1.00      0.71       269

    accuracy                           0.90      2300
   macro avg       0.77      0.94      0.82      2300
weighted avg       0.95      0.90      0.91      2300
from xgboost import XGBClassifier
XGB = XGBClassifier(learning_rate = 0.01, n_estimators = 25,
                    max_depth = 25, gamma = 0.6,
                    subsample = 0.52, colsample_bytree = 0.6,
                    seed = 27, reg_lambda = 2, booster = 'dart',
                    colsample_bylevel = 0.6, colsample_bynode = 0.5
                   )
XGB.fit(xtrain, ytrain)
predictions = XGB.predict(xtest)
evaluate(XGB, predictions, ytest)
Accuracy of XGBClassifier(base_score=0.5, booster='dart', colsample_bylevel=0.6,
              colsample_bynode=0.5, colsample_bytree=0.6, gamma=0.6, gpu_id=-1,
              importance_type='gain', interaction_constraints='',
              learning_rate=0.01, max_delta_step=0, max_depth=25,
              min_child_weight=1, missing=nan, monotone_constraints='()',
              n_estimators=25, n_jobs=0, num_parallel_tree=1, random_state=27,
              reg_alpha=0, reg_lambda=2, scale_pos_weight=1, seed=27,
              subsample=0.52, tree_method='exact', validate_parameters=1,
              verbosity=None) : 96.86956521739131 %
              precision    recall  f1-score   support

 Non Seizure       0.99      0.97      0.98      1841
     Seizure       0.90      0.95      0.92       459

    accuracy                           0.97      2300
   macro avg       0.94      0.96      0.95      2300
weighted avg       0.97      0.97      0.97      2300
from mlxtend.classifier import StackingCVClassifier
SCV=StackingCVClassifier(classifiers=[XGB,SV,KNN, NB, RF, DT],meta_classifier= SV,random_state=42)
SCV.fit(xtrain, ytrain)
predictions = SCV.predict(xtest)
evaluate(SCV, predictions, ytest)