Building an EEG Classifier to Differentiate Between Attention and Non-attention States

Have you ever wondered what happens in your brain when doing homework or getting distracted by scrolling on social media?

Jodie Lechtenberg
Insights of Nature
5 min readApr 17, 2024

--

By Jodie Lechtenberg

doing homework…

Well, I wanted to know too!

So for the past month, I made it my mission to build an electroencephalography (EEG) classifier, that can distinguish between attention and distraction states for me, so I can know whether or not I’m focused or distracted when doing work.

What even is attention?

Attention is a cognitive process that allows us to selectively focus on specific stimuli while filtering out irrelevant information -> attention plays an important role in shaping our perception, memory, and behavior.

Where does attention “come” from?

Various regions of the brain work together to regulate attentional processes.

The most important region is the prefrontal cortex, located at the front of the brain. This region is involved in higher-order cognitive functions, such as goal-setting, decision-making, and working memory. It helps us maintain attention by directing our focus toward task-relevant information while inhibiting distractions.

The prefrontal cortex (blue)

The parietal cortex, situated near the top and back of the brain, is also pretty important for attention. It integrates sensory information from different modalities, such as vision and touch, and helps us orient our attention toward salient stimuli in the environment. Additionally, the parietal cortex also coordinates spatial awareness and eye movements during visual search tasks.

Functions of the parietal cortex

The thalamus often called the “gateway to the cortex,” is a relay station for sensory information entering the brain. It filters incoming stimuli and directs relevant information to the right cortical areas for further processing. Through its connections with the prefrontal and parietal cortices, the thalamus helps regulate attentional control and selective attention.

The thalamus, located in the centre of the brain

How does the brain modulate attention?

One important neurotransmitter is dopamine, often associated with reward and motivation. Neurotransmitters are just like mail carriers that deliver letters and packages between houses, but instead, neurotransmitters deliver chemical “messages” between neurons (nerve cells) in the brain and body.

(Don’t comment on my drawing skills. I can only draw stick people.)

Dopamine levels in the brain increase when we encounter novel or rewarding stimuli, which enhances our attention and focus. Dysfunction in the dopamine system has been implicated in attention-related disorders such as attention deficit hyperactivity disorder (ADHD).

3D molecular structure of dopamine

Another neurotransmitter, acetylcholine, influences attention by regulating cortical arousal and vigilance. Increased acetylcholine release is associated with heightened attention and improved cognitive performance, while disruptions in the cholinergic system can lead to deficits in attention and cognitive function.

3D molecular structure of acetylcholine

How does our brain maintain attention?

Alpha oscillations (8–12 Hz), so rhythmic patterns of neural activity have been linked to the inhibition of task-irrelevant brain regions, which allows us to maintain focused attention on relevant stimuli. Gamma oscillations (25–100 Hz), on the other hand, are associated with the synchronization of neural networks involved in sensory perception and cognitive processing, facilitating the integration of information across brain regions.

And how did you build the classifier?

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

Importing Libraries: This part imports necessary Python libraries that I used in the code. `numpy` is used for numerical computations, `train_test_split` is used to split data into training and testing sets, `RandomForestClassifier` is a machine learning algorithm, and `accuracy_score` is used to calculate the accuracy of the classifier.

eeg_data = np.load('eeg_data.npy')

Loading EEG Data: I loaded EEG data from a file called ‘eeg_data.npy’. This file contains numerical data representing brainwave signals recorded from the Mind Monitor app, since my brainwaves weren’t the best to work with, for the reason that I didn’t use an amplifier or a noise-extracting feature to enhance the accuracy of the signals.

If you want to build your own classifier, replace ‘eeg_data.npy’ with the path to your EEG data file.

X = eeg_data[:, :-1]
y = eeg_data[:, -1]

Splitting Data: The data contains both features (brainwave data) and labels (e.g., ‘attention’ or ‘distraction’), so I split the data into two arrays: `X` (features) and `y` (labels).

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Splitting into Training and Testing Sets: I split the data into two sets: a training set and a testing set. I used the training set to train the classifier, while I used the testing to evaluate its performance. I used about 80% of the data for training (`X_train` and `y_train`) and 20% for testing (`X_test` and `y_test`).

classifier = RandomForestClassifier(n_estimators=100, random_state=42)
classifier.fit(X_train, y_train)

Initializing and Training the Classifier: I initialized a Random Forest classifier with 100 trees and a random seed of 42. Then, I trained the classifier using the training data.

Why did I use a Random Forest classifier?

  1. This classifier doesn’t make as many mistakes as some others, which is great because it means it’s more reliable.
  2. Even if there’s a lot of data, it’s still easy to use and interpret, unlike more complex models like neural networks. This is important because EEG data can be pretty complicated. It explains which parts of the EEG data are most important for figuring out if someone’s paying attention or not.
  3. It’s quick and can handle a lot of data at once. This means I could potentially use it on big datasets without it taking forever to give me an answer.
y_pred = classifier.predict(X_test)

Making Predictions: I used the trained classifier to make predictions on the testing set (`X_test`). The predicted labels are stored in the variable `y_pred`.

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Calculating Accuracy: Finally, I calculated the accuracy of the classifier by comparing the predicted labels (`y_pred`) with the actual labels (`y_test`). The accuracy score is printed on the console.

Thanks for reading my article! Hope to see you next time!

--

--

Jodie Lechtenberg
Insights of Nature

intellectually curious individual, excited to learn about everything