<jupyter_start><jupyter_text>Epileptic Seizure RecognitionThis dataset comes from here and contains 11500 EEG exams from patients. Each EEG contains 178 variables. The output is divided in 5 categories as follows:1. Recording of seizure activity.2. They recorder the EEG from the area where the tumor was located.3. Yes they identify where the region of the tumor was in the brain and recording the EEG activity from the healthy brain area.4. eyes closed, means when they were recording the EEG signal the patient had their eyes closed.5. eyes open, means when they were recording the EEG signal of the brain the patient had their eyes open.We are mainly interested in class 1, and we can say that classes {2,3,4,5} = 0<jupyter_code>import joblib
import pandas as pd
import numpy as np

from sklearn import preprocessing
from matplotlib import pyplot as plt  
from sklearn.metrics import accuracy_score
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
# Read dataset
df = pd.read_csv('./dataset/Epileptic_Seizure_Recognition.csv')
df.head(5)<jupyter_output><empty_output><jupyter_text>We can see that our data consist of 179 features, though the first one is a patient ID we can ignore in this case. We also have the output column y, in that case we have a 1 indicating a seizure and 0,2,3,4 and 5 are non seizure events. In this case, we will also normalize the y column to generalize those cases as 0.<jupyter_code># Remove Unnamed column, it contains ID's that we won't use.
df = df.drop(['Unnamed'], axis=1)

# Normalize 2,3,4,5 as they are all considered a non-epileptic state we will call 0.
for i in {2,3,4,5}:
    df.y[df.y == i] = 0<jupyter_output><empty_output><jupyter_text>Time to do some normalization and scale the data. We are also going to split the data into train and test sets, with a test set consisting of the 20% of the total data we have.<jupyter_code>normalized  = preprocessing.normalize(df, norm='l2')
data = preprocessing.StandardScaler().fit_transform(normalized)

X = data[:,:-1]             # Select only the features
y = data[:,-1].astype(int)  # Select only the output

# Do a split for test data.
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = .20, random_state = 42)<jupyter_output><empty_output><jupyter_text>Let's try to find an ideal depth, in this case we tried values between 1 and 35 and we found ut that the best value for our test set was around 31.<jupyter_code>proc_max_depth = False
train_acc = []
test_acc  = []
if proc_max_depth:
    for i in range(1,35):
        model = RandomForestClassifier(150, max_depth=i)
        model.fit(x_train, y_train)
        train_acc.append(accuracy_score(y_train, model.predict(x_train)))
        pipeline_prediction = model.predict(x_test)
        test_acc.append(accuracy_score(y_test, model.predict(x_test)))
else:
    train_acc = [0.6271739130434782, 0.6271739130434782, 0.6271739130434782, 0.6271739130434782, 0.6303260869565217, 0.6563043478260869, 0.6778260869565217, 0.7166304347826087, 0.7460869565217392, 0.7772826086956521, 0.8177173913043478, 0.8660869565217392, 0.9143478260869565, 0.9479347826086957, 0.9689130434782609, 0.9828260869565217, 0.9889130434782609, 0.993804347826087, 0.9954347826086957, 0.9976086956521739, 0.9984782608695653, 0.9981521739130435, 0.9993478260869565, 0.9993478260869565, 0.9996739130434783, 0.9996739130434783, 0.9998913043478261, 1.0, 0.9998913043478261, 1.0, 1.0, 1.0, 1.0, 1.0]
    test_acc  = [0.6239130434782608, 0.6239130434782608, 0.6239130434782608, 0.6239130434782608, 0.6243478260869565, 0.6413043478260869, 0.6495652173913044, 0.662608695652174, 0.671304347826087, 0.6730434782608695, 0.6839130434782609, 0.688695652173913, 0.6939130434782609, 0.6978260869565217, 0.7030434782608695, 0.7021739130434783, 0.7034782608695652, 0.7008695652173913, 0.7047826086956521, 0.7043478260869566, 0.7039130434782609, 0.7095652173913043, 0.7091304347826087, 0.7052173913043478, 0.7060869565217391, 0.7130434782608696, 0.711304347826087, 0.7039130434782609, 0.7095652173913043, 0.7130434782608696, 0.7108695652173913, 0.7147826086956521, 0.7139130434782609, 0.71]
    
print("".join(("Best max_depth value to max the accuracy in our test set is ", str(np.argmax(test_acc)))))
plt.plot(range(1,35), train_acc, label = 'Training accuracy')
plt.plot(range(1,35), test_acc, label = 'Testing accuracy')<jupyter_output>Best max_depth value to max the accuracy in our test set is 31<jupyter_text>Now that we have a good model, lets work on it and make it even better. Let's do some hyperparameter tuning and hope we can improve the model as we go.<jupyter_code>model = RandomForestClassifier(150, max_depth=31, max_features='sqrt', random_state = 42)
model.fit(x_train, y_train)
prediction = model.predict(x_test)
accuracy = accuracy_score(y_test, prediction)
print(accuracy)<jupyter_output>0.8778260869565218<jupyter_text>Our final model accuracy is of around 87.8%Last thing to do is to save the model for production.<jupyter_code>joblib.dump(model, "production/model.joblib")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08638642499035866, 'fraction_numerical': 0.22059390667180873, 'mean_word_length': 5.565822784810127, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '370922', 'license': 'MIT', 'n_tokens_mistral': 2536, 'n_tokens_neox': 1843, 'n_words': 543, 'path': 'Epileptic Seizure Recognition.ipynb', 'repo_name': 'Juanmam/epileptic_seizure_model', 'size': '5186'}