DEV Community

Cover image for Predicting Tesla Stock Prices with MindsDB!
Hridya for Learn Earn & Fun

Posted on • Updated on

Predicting Tesla Stock Prices with MindsDB!

Image description


If you don't know what MindsDB is, Go checkout my blog to know more

Basically, MindsDB is an Open-Source AI Layer for existing Databases.

It's an AI Layer for traditional databases such as PostgreSQL, MariaDB, MySQL, etc..

In this tutorial, We are going to be predicting the stock prices of tesla using MindsDB!

Importing Data into MindsDB Cloud

In order to import the dataset to MindsDB Cloud, we need to first download it from Kaggle and then upload it simply to MindsDB using the steps mentioned below.

Step 1: Create a MindsDB Cloud Account, If you already haven't done so

Image description

Step 2: Download this Dataset

Image description

Step 3: Go into Add Data -> Files -> Import File
Lastly, Add the dataset (After downloading you will get a .zip file, you have to extract it and import the csv inside)

Image description

Step 4: Name the Table: TeslaStock (You can name it anything you like!)

Image description

Step 5: To verify the dataset has successfully imported in:

Go into the Editor Tab and run this command:

Enter fullscreen mode Exit fullscreen mode

If you see TeslaStock or whatever you named it that means it's successfully imported!

Image description

Training a Model

MindsDB provides very simple SQL queries to carry out different tasks in its interface. So, we will now proceed with the steps below to get ready with the model.

Step 1: Create a Model, we will be creating a Predictor Model.

CREATE PREDICTOR mindsdb.tesla_predictor   
FROM files
(SELECT * FROM TeslaStock)              
PREDICT Close                      
ORDER BY value_date                
WINDOW 120                         
HORIZON 60;                        
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2: Based on the size of the dataset, it might take some time.

There's 3 stages once you run the command to create the model:

  1. Generating: The model's generating!
  2. Training: Model is getting trained with the dataset
  3. Complete: The model is ready to do predictions

To check the status, this is the query:

SELECT status
FROM mindsdb.predictors
WHERE name='tesla_predictor';
Enter fullscreen mode Exit fullscreen mode

Once it returns complete we can start predicting with it!

Image description

Describe the Model

Before we proceed to the final part of predicting the tesla stock price, let us first understand the model that we just trained.

MindsDB provides the following 3 types of descriptions for the model using the DESCRIBE statement.

  1. By Features
  2. By Model
  3. By Model Ensemble

By Features

DESCRIBE mindsdb.predictor_model_name.features;
Enter fullscreen mode Exit fullscreen mode

This query shows the role of each column for the predictor model along with the type of encoders used on the columns while training.

By Model

DESCRIBE mindsdb.predictor_model_name.model;
Enter fullscreen mode Exit fullscreen mode

This query shows the list of all the underlying candidate models that were used during training. The one with the best performance (whose value is 1), is selected. You can see the value 1 for the selected one in the selected column while others are set at 0.

By Model Ensemble

DESCRIBE mindsdb.predictor_model_name.ensemble;
Enter fullscreen mode Exit fullscreen mode

This query gives back a JSON output that contains the different parameters that helped to choose the best candidate model for the Predictor Model.

As we are done now understanding our Predictor model, let's move on to prediciting values in the next section.

Predicting the Target Value

Before we proceed, we need to understand the basic scenario here. So, we have a dataset that lists out the closing stock values of Tesla over a period of time. Now, with this predictor model, we will try to predict the closing value after the last record available in the dataset. We can do that by using another keyword LATEST for the dates now.

This is the query:

SELECT T.value_date as date,
       T.Closing as Forecast,
FROM mindsdb.tesla_predictor as T
JOIN files.TeslaStock as P
WHERE P.value_date > LATEST
Enter fullscreen mode Exit fullscreen mode

This query should return us with the forecasts of up to 2 months (60 Days) after the last date that was available in the dataset.

Awesome! We have now successfully predicted the Tesla Stock Prices using a Predictor.


This concludes the tutorial here. Before we wrap this up, let's do a quick recap of what we did here. We first started with creating a MindsDB Cloud account, fed the dataset and created a table using the cloud UI, trained a Predictor model, described its model features and finally predicted the target closing value.

Lastly, before you leave, I would love to know your feedback in the Comments section below and please drop a LIKE

Top comments (0)