Using brainwave data to detect whether someone is feeling good
Recently I’ve been fascinated by the brain and how we can control the external world all through our brain!
BCI’s are a revolutionizing technology that unlock an exciting future whether that be playing games from our mind, collecting and understanding our brainwaves or even diagnosing/detecting disorders.
In this article, I’ll breakdown my project of how I used brainwave data to detect whether someone is feeling good or bad.
Diving into BCI’s
For this project, a Brain Computer Interface is used to collect the brainwave data of negative and positive emotions to build a machine learning classifier!
Brain Computer Interfaces are revolutionizing the way we treat, diagnose and detect! BCI’s are a technology where brain signals are used to control external machines or environments. It involves three steps
- collecting brain signals
- interpreting the brain signals
- executing a command based on the interpretation (this is where machine learning comes in!)
For my emotional brain wave classifier I have to find discriminative EEG-based features and classification methods to categorize brainwave patterns based on their level of activity or frequency for mental state recognition.
The first step consists of collecting the brain waves, there are a few methods to do this!
This consists of sensors being placed on top of a scalp to measure the electrical potentials produced by the brain or the magnetic field.
Electrodes are placed on the exposed surface of the brain collecting EcoG data.
Micro-electrodes are placed directly into the cortex, measuring the activity of a single neuron.
In this experiment, electroencephalogram (EEG) data was non-invasively gathered from a range of subjects during these three emotional states
Step 1: Collecting the brain-wave data!
Using a non-invasive device the EEG signals for this experiment was collected. The subjects used the Muse headband, where a movie was played and the data was collected for 3 minutes per state positive, neutral, and negative. Muse is a commercialized EEG sensing device with five dry application sensors to record brain wave activity.
Since all conscious human activities are triggered in the brain, we can use EEG to discern what a person is thinking, doing, and feeling.
The data was observed to be streaming at a variable frequency within the range of 150–270 Hz.
Step 2: Understanding the data
A major challenge with BCI applications is understanding the data being collected and what the EEG waves mean. One of the main issues with classifying EEG signals is the amount of data needed to properly describe the different states, since the signals are complex, non-linear, non-stationary, and random in nature.
In order to understand and use the EEG data we go through 2 processes, the first being a Statistical extraction and features selection.
We form the dataset through statistical extraction, the statistics extracted from each EEG wave is used to produce the full dataset. Each of these statistics for the time windows were then labelled to the emotion, forming a dataset with very high dimensionality.
Feature selection allows us to understand the features that class each mental state. The set of features considered in this work to adequately discriminate different classes of mental states rely on statistical techniques, time-frequency based on fast Fourier transform (FFT), Shannon entropy, max-min features in temporal sequences, log-covariance, and others.
Step 3: Building out the model
The last step is to actually do something with the data collected, in our case we want to build a classifier to determine when given a certain brainwave is it showcasing a negative, positive or neutral emotional. The data we collected is used as our training data and then gets executed in a machine learning model.
The best machine learning models for classification included Random Trees, SVM, Naïve Bayes, Bayes Network, Logistic Regression, or a Deep Neural Network.
How I Built My Own Mental Emotional Sentiment Classifier
In reality I would collect EEG data through an actual headset to collect my brain waves, but for this experiment I used a dataset online that was recorded from when subjects were watching a series of five different movie scenes, and I used the dataset to try to predict the emotional state of a given subject during a given movie. The results of the emotional state are either negative, neutral or positive.
If you’d like to check out my code, you can check out my Github repository!
In order to build this model I built a RNN to detect the different emotional states.
First we import the libraries necessary to build this model and read the data using pd.read_csv()
Next we looked at part of the data as a graph, took a sample and started with the first row to plot it.
Then I took a look at the class distribution by getting the value count for each label column at the end (emotion states).
In this step I converted the label column from categorical to numerical values by applying the label mapping from the previous piece of code. I then split the training and testing data, where 70% of the data was used for training and 30% of the data for testing.
I made the model with one Gated Recurrent Unit (GRU) layer, which is a type of recurrent neural network.
The model composed of a neural net that consisted of 5 layers where the neural net picked on the key things that made sure if the brainwave was showing someone feeling good or bad.
For the model, adam is used as the optimizer and accuracy as the metric. Then the model was fit with the training data.
Finally we calculate the accuracy to see if our model is effective! The test accuracy was printed, which showed the model to be 93% accurate.
Then, I got a confusion_matrix that showed me how many of the predictions made were correct and how many were incorrect for each of the three emotional state classifications using a visual plot.
And that’s how I built a model to detect the emotional state from given brainwaves!
I built this model from a tutorial by Gabriel Atkin Tutorial Part 1, Part 2.
If you enjoyed this article:
- Share it with your network 🙏
- Connect with me on Linkedin to stay updated on my AI journey, and shoot me a message (I love meeting new people).
- Subscribe to my newsletter, for monthly updates on what I’m working on!