Forecasting Eye State from Electroencephalogram Readings with MindsDB
This tutorial uses the Lightwood integration that requires the mindsdb/mindsdb:lightwood
Docker image. Learn more here.
Introduction
In this tutorial, we’ll create and train a machine learning model, or as we call
it, an AI Table
or a predictor
. By querying the model, we’ll produce
categorical forecasts for a multivariate time series.
Install MindsDB locally via Docker or Docker Desktop.
Let’s get started.
Data Setup
Connecting the Data
There are a couple of ways you can get the data to follow through with this tutorial.
Connecting as a database
You can connect to a demo database that we’ve prepared for you. It contains the data used throughout this tutorial (the example_db.demo_data.eeg_eye
table).
Now you can run queries directly on the demo database. Let’s preview the data that we’ll use to train our predictor.
Connecting as a file
The dataset we use in this tutorial is the UCI’s EEG Eye State dataset. You can download it here in the ARFF
format that should be converted to the CSV
format before uploading it via MindsDB SQL Editor.
Follow this guide to find out how to upload a file to MindsDB.
Now you can run queries directly on the file as if it were a table. Let’s preview the data that we’ll use to train our predictor.
Pay Attention to the Queries” From now on, we’ll use the files.eeg_eye
file
as a table. Make sure you replace it with example_db.demo_data.eeg_eye
if
you connect the data as a database.
Understanding the Data
We use the UCI’s EEG Eye State dataset, where each row contains data of one
electroencephalogram (EEG) reading plus the current state of the patient’s eye,
where 0
indicates open eye and 1
indicates closed eye. We want to know ahead
of time when the eye state will change, so we predict the eyeDetection
column.
Below is the sample data stored in the files.eeg_eye
table.
Where:
Column | Description | Data Type | Usage |
---|---|---|---|
AF3 ,F7 ,F3 ,FC5 ,T7 ,P7 ,O1 ,O2 ,P8 ,T8 ,FC6 ,F4 ,F8 ,AF4 | The EEG measurement data. | float | Feature |
eyeDetectin | The state of the patient’s eye where 0 indicates open eye and 1 indicates closed eye. | binary | Label |
Labels and Features A label is a column whose values will be predicted (the y variable in simple linear regression). A feature is a column used to train the model (the x variable in simple linear regression).
Training a Predictor
Let’s create and train the machine learning model. For that, we use the
CREATE MODEL
statement and specify the
input columns used to train FROM
(features) and what we want to
PREDICT
(labels).
The eyeDetection
column is our target variable. The interesting thing about
this example is that we aim to forecast labels that are not strictly
numerical. Even though this example is simple (because the variable is a binary
category), this can easily be generalized to more than two categories.
We order the measurements by the Timestamps
column that shows readings
frequency of approximately 8 milliseconds.
As the sampling frequency is 8 ms, this predictor is trained using a historical
context of roughly 400 ms ((50 * 8) = 400 [ms]
) to predict the following 80 ms
((10 * 8) = 80 [ms]
).
Status of a Predictor
A predictor may take a couple of minutes for the training to complete. You can monitor the status of the predictor by using this SQL command:
If we run it right after creating a predictor, we get this output:
A bit later, this is the output:
And at last, this should be the output:
Now, if the status of our predictor says complete
, we can start making
predictions!
Making Predictions
You can make predictions by querying the predictor joined with the data table.
The SELECT
statement lets you make predictions for the
label based on the chosen features for a given time period. Usually, you want to
know what happens right after the latest training data point that was fed. We
have a special keyword for that, the LATEST
keyword.
Let’s run a query to get predictions for the next HORIZON
timesteps into
the future, which in this case is roughly 80 milliseconds.
On execution, we get:
That’s it. We can now JOIN
any set of WINDOW
rows worth of
measurements with this predictor, and forecasts will be emitted to help us
expect a change in the state of the patient’s eye based on the EEG readings.
Alternate Problem Framings
It is also possible to reframe this task as a normal forecasting scenario
where the variable is numeric. There are a few options here. It boils down to
what the broader scenario is and what format would maximize the value of any
specific prediction.
For example, a simple mapping of eye is open
to 0
and eye is closed
to 1
would be enough to replicate the above behavior.
We could also explore other options. With some data transformations on the data
layer, we could get a countdown to the next change in state, effectively
predicting a date if we cast this back into the timestamp domain.
What’s Next?
Have fun while trying it out yourself!
- Bookmark MindsDB repository on GitHub.
- Install MindsDB locally via Docker or Docker Desktop.
- Engage with the MindsDB community on Slack or GitHub to ask questions and share your ideas and thoughts.
If this tutorial was helpful, please give us a GitHub star here.
Was this page helpful?