Predicting Tesla Stock Prices with MindsDB!
Tags: machinelearning, ai, database, datascience
Published: 2022-11-03 13:39:06


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5alj5mrht9y8d735a5i.png)

## Introduction

If you don't know what MindsDB is, Go checkout my [blog](https://dev.to/learnearnfun/quickstart-to-mindsdb-1ifm) 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](https://cloud.mindsdb.com) Account, If you already haven't done so


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyl879m8rqyle1qqu2z7.png)

_**Step 2**_: Download this [Dataset](https://www.kaggle.com/datasets/harshsingh2209/tesla-stock-pricing-20172022)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akviqjexfbrd9ako315e.png)

**_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y81wyngeqdj0ccdh3mo8.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r2selb0tph0jrls2dg1.png)

**_Step 5_**: To verify the dataset has successfully imported in:

Go into the Editor Tab and run this command:

```
SHOW TABLES FROM files;
```

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm6jjeykt5zfw8nqvhn1.png)





## 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;                        
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkzguripykdlso1khb3u.png)




**_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';
```

Once it returns `complete` we can start predicting with it!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/razrm7w7tymsu23g1msa.png)


## 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;
```
 
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;
```

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;
```

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,
       Closing_explain
FROM mindsdb.tesla_predictor as T
JOIN files.TeslaStock as P
WHERE P.value_date > LATEST
LIMIT 60;
```

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.



## Conclusion

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`