How To Visualize MindsDB Predictions with Tableau.
Tags: mindsdb, machinelearning, sql, tableau
Published: 2022-09-21 18:16:21

## Introduction
​
In this tutorial, we'll use MindsDb to build, train, and query a machine learning model and then use Tableau to visualize the results.

If you're new to MindsDB, I recommend reading my previous [article](https://dev.to/ephraimx/predicting-future-sales-using-machine-learning-with-mindsdb-3p70), in which I explain what MindsDB is, the problem it solves, and how to get started with it.
​
To complete this tutorial, you must have a working MindsDB connection, either locally or via cloud.mindsdb.com. 
​
You can use this [guide](https://docs.mindsdb.com/connect/mindsdb_editor/) to connect to MindsDB Cloud.
​
## The Data
​
This tutorial uses Apple's (APPL) stock price from September 1984 to November 2017. You can get the data [here](https://storage.googleapis.com/kagglesdsdata/datasets/4538/7213/Stocks/aapl.us.txt?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20220812%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20220812T173401Z&X-Goog-Expires=259200&X-Goog-SignedHeaders=host&X-Goog-Signature=5ea0599ff44b369dbab88290c967c3aefd43f8a1366cec114bf28a0624a6741caab690fc091cbe267afb9c6b28439918d2a04251d49f31ce6a59fad01702fe9224a8af6c717e7bc43442f3302ecf73e141c485e73e7c67b150ab0c3b58ab1d811961b24b397c89de810ffff2465a47266c21a4744ceafcab57cd52f51d1e1808d14d1ebbddbb1c6432f559b628666f7f2a4fbb5e24db4a21995b293a07aab45e984496a7025706df75d36d3a6bdb2b7070c5b94fefa56d1063bf6cf01416cfabf3d4a14981447d1f5eea0cc946f9e2c79e7086d4898684c0790da7acefa625389e1d26ba25dc37a216c42ceb1ce5bf8dab6634f7cc7c3ecfa7a3fe7337e2754c), save it as a CSV file, and upload it to MindsDB's cloud system with the file name `apple_stock_price`. You can learn how to upload the data [here](https://docs.mindsdb.com/sql/create/file/). The stock price data consists of:
- Date: The trading date.
- Open: The stock price at the start of the trading day.
- Close: The stock price at the end of the trading day.
- High: The stock's highest price throughout a trading day.
- Low: The stock's lowest price throughout a trading day.
- Volume: The number of shares exchanged during the trading day.
- OpenInt: Included in the data but does not have any meaning, so it won't be when creating the predictor.
​
Now that you've uploaded the file, you can query it to confirm the data is present.
​
```
SELECT Date, Open, High, Low, Close, Volume FROM files.apple_stock_price LIMIT 5;
```
**Query Output.**
​

<table>
<thead>
<tr>
<th style="text-align:left">Date</th>
<th style="text-align:center">Open</th>
<th style="text-align:center">High</th>
<th style="text-align:center">Low</th>
<th style="text-align:center">Close</th>
<th style="text-align:right">Volume</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1984-09-07</td>
<td style="text-align:center">0.42388</td>
<td style="text-align:center">0.42902</td>
<td style="text-align:center">0.41874</td>
<td style="text-align:center">0.4238807</td>
<td style="text-align:right">23220030</td>
</tr>
<tr>
<td style="text-align:left">1984-09-10</td>
<td style="text-align:center">0.42388</td>
<td style="text-align:center">0.42516</td>
<td style="text-align:center">0.41366</td>
<td style="text-align:center">0.42134</td>
<td style="text-align:right">18022532</td>
</tr>
<tr>
<td style="text-align:left">1984-09-11</td>
<td style="text-align:center">0.42516</td>
<td style="text-align:center">0.43668</td>
<td style="text-align:center">0.42516</td>
<td style="text-align:center">0.42902</td>
<td style="text-align:right">42498199</td>
</tr>
<tr>
<td style="text-align:left">1984-09-12</td>
<td style="text-align:center">0.42902</td>
<td style="text-align:center">0.43157</td>
<td style="text-align:center">0.41618</td>
<td style="text-align:center">0.41618</td>
<td style="text-align:right">37125801</td>
</tr>
<tr>
<td style="text-align:left">1984-09-13</td>
<td style="text-align:center">0.43927</td>
<td style="text-align:center">0.44052</td>
<td style="text-align:center">0.43927</td>
<td style="text-align:center">0.43927</td>
<td style="text-align:right">57822062</td>
</tr>
</tbody>
</table>


​
​
## Creating the Predictor.
​
This article will go over projecting the stock's closing price and visualizing it in Tableau.
​
To begin, create a predictor (MindsDB word for model) that uses the stock price to forecast the stock's closing price. You can learn more about building a predictor by checking [here](https://docs.mindsdb.com/sql/create/predictor/). The syntax for explaining time series predictions is near the bottom of the page.
​
​
**Note**: In machine learning, what you forecast in the data is referred to as a `label`, and what you train the model/predictor on, is referred to as a `feature`. The 'close' column in the data above is the label, while the others are features.
​
You can create the predictor using the SQL syntax below:
​
```
CREATE PREDICTOR mindsdb.apple_stock_predictor
FROM files
    (SELECT Date, Open, High, Low, Close, Volume
    FROM apple_stock_price)
PREDICT Close
ORDER BY Date
WINDOW 20
HORIZON 365;
```
The following describes the commands above:
- `CREATE PREDICTOR`: Creates a predictor with the name `apple_stock_predictor` in the `mindsdb` table.
-  `FROM files`: Points to the table containing the data.
-  `PREDICT Close`: Dictates the column to predict.
-  `ORDER BY`: Shows the column to arrange the data during training.
-  `WINDOW`: Decides how many rows to "look back" into when creating a prediction. In this case, “always look back 20 rows” when making predictions.
-  `HORIZON`: Specifies the number of future predictions. The default is 1, for the code above, 365 future predictions.
​
You can check the predictor status with the query below:
​
```
SELECT status
FROM mindsdb.predictors
WHERE name = 'apple_stock_predictor'
```
​
This syntax returns either of the following responses:
- Generating.
- Training.
- Complete.
- Error.
​

Now, forecast the stock price using the newly created predictor with the query below:
​
```
SELECT Date , Close as `Closing Price`
FROM mindsdb.apple_stock_predictor
JOIN files.apple_stock_price as a
WHERE a.Date > LATEST
LIMIT 5;
```
​
The `LATEST` keyword tells MindsDB to predict forward from the last recorded observation.
​
Query Output.
​
<table>
<thead>
<tr>
<th>Date</th>
<th style="text-align:right">Closing Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>2017-11-11</td>
<td style="text-align:right">120.0069968945692</td>
</tr>
<tr>
<td>2017-11-12</td>
<td style="text-align:right">125.92226412840009</td>
</tr>
<tr>
<td>2017-11-13</td>
<td style="text-align:right">124.78782931643252</td>
</tr>
<tr>
<td>2017-11-14</td>
<td style="text-align:right">127.94804057834217</td>
</tr>
<tr>
<td>2017-11-15</td>
<td style="text-align:right">126.16535730239313</td>
</tr>
</tbody>
</table>

​
You can also check the predictor's accuracy with the query below to see how it performs.
​
```
SELECT accuracy
FROM mindsdb.predictors
WHERE name = 'apple_stock_predictor'
```
​
Query Output.
​
<table>
<thead>
<tr>
<th>accuracy</th>
</tr>
</thead>
<tbody>
<tr>
<td>0.534</td>
</tr>
</tbody>
</table>

​
The accuracy score ranges from 0 to 1, and from the result, the model will be accurate about half the time.
​
This tutorial will use Tableau Public to create visualizations of our predictions. Due to the inability of Tableau Public to connect to the MySQL database, the predictions will be saved as a .csv file and uploaded to Tableau using the text file option.
​
​
## Connecting Tableau to MindsDB
​
1. Log into your Tableau account and create a new workbook.
2. From the connectors tab, select the MySQL option from the list of options.
    ![Tableau MySQL](https://imgur.com/GOCD5W5.jpeg)
3. Input "cloud-mysql.mindsdb.com" for Server, "3306" for Port, "mindsdb" for Database "your mindsdb cloud email" for Username, "your mindsdb cloud password" for Password, and Sign in. 
​

Before you can visualize the predictions in Tableau, you must first choose a data source. And because the stock predictions in this article are generated using a SQL statement, you will need to create a custom SQL query in Tableau to generate the data source. To do this:


- First, select the `New Custom SQL`  at the left side of the window and use the query below to generate the stock prices for the next year. You can preview the results or directly load the data into Tableau. ![Custom SQL option](https://imgur.com/Aa3zNEx.jpeg)
    ```
    SELECT Date , Close as `Closing Price`
    FROM mindsdb.apple_stock_predictor
    JOIN files.apple_stock_price as a
    WHERE a.Date > LATEST
    LIMIT 365
    ```
    ![Custom SQL query](https://imgur.com/YT6uB31.jpeg)
- Create an extract of the data under the connection heading at the top right of the window. You do this to facilitate data conversion to the appropriate data type. The extraction should take one to two minutes.
- Move to the Sheet tab and right-click on the Closing Price measure and Date dimension to convert their data types to Number(decimal) and Date, respectively. Additionally, when right-clicking on the Closing Price measure, choose the option to convert it to a continuous measure.
    ![Closing Price - Number (Decimal)](https://imgur.com/NLdPYav.jpeg)
    ![Date - Date type](https://imgur.com/tYsd5nU.jpeg)
    ![Closing Price - Convert to Continous](https://imgur.com/kJcVkQZ.jpeg)
- Drag the Closing Price measure to the row shelf and the Date dimension to the column shelf.
    ![Closing Price - Row Shelf, Date - Column Shelf](https://imgur.com/bMx1WRZ.jpeg)
- Finally, right-click on the Date dimension and switch from Year to Month. Change the visualization type to Line as well.
    ![Date - Year to Month](https://imgur.com/XrVcEN9.jpeg)
​
Here is the final visualization: 
​
![Final Visualization](https://imgur.com/3hBpuYj.jpeg)
​
## Conclusion.
​
This article explained how to visualize your model's predictions from MindsDB in Tableau. To learn more about MindsDB check out the [documentation](https://docs.mindsdb.com). Also, you can connect with the community on [Slack](https://mindsdb.com/joincommunity?_ga=2.149445302.1457369481.1660150592-302861586.1659217161) to answer questions and share ideas.
​
Thank you for taking the time to read this article. If you enjoyed it, please forward it to others who might find it useful. You can also find me on [Twitter](https://www.twitter.com/ee_ephraim) | [LinkedIn](https://www.linkedin.com/in/oghenefejiro-esosuota) | [GitHub](https://www.github.com/EphraimX). See you in my next article.