Predict Wine Quality using MindsDB
Tags: tutorial, database, machinelearning, mindsdb
Published: 2022-10-15 18:41:49

![Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvw6vja831zzfsg0ydj4.gif)

## Introduction

MindsDB, undoubtedly, brings in the best-in-class machine learning capabilities to traditional databases. It acts as an AI layer on top of the existing tables and enables to train models and predict outcomes easily and instantly with the help of simple SQL statements.

MindsDB provides a huge collection of integrations with almost all the available databases and many ML Frameworks to make it easier for users to easily manage their existing data infrastructure along with MindsDB. Also, MinsdDB offers its service in two ways i.e., Local Deployments (Using Docker or PIP) and MindsDB Cloud, both of which offer a free tier to all their consumers.

In this tutorial we will be predicting the quality of wine based on its feature set using MindsDB Cloud.

## Importing Data to MindsDB Cloud

We can obtain the free datasets from any of the available sources like Kaggle, Datahub, Google Dataset Search, etc to train our Predictor model. We will use [this dataset](https://www.kaggle.com/code/febymelania/aggregation-red-wine-quality-datasets/data?select=winequality-red.csv) from Kaggle for this tutorial.

So, let's get started with the process of adding this dataset to MindsDB Cloud.

**Step 1:** We need to first [sign in](https://cloud.mindsdb.com/login) to the MindsDB Cloud console or start [registering](https://cloud.mindsdb.com/register) for a new account.

![MindsDB Register](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b1tbxpab5p556dkksvt.png)

**Step 2:** After logging in, it opens up the MindsDB Cloud Editor. We find a **Query Editor** at the top where we can write the queries and then hit the `Run(Shift+Enter)` button above it to execute them, a **Result Viewer** at the bottom where the results are displayed for the queries and finally, we have the **Learning Hub** at the right side which works as a learning aid for the new users.

![Cloud Editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfpzyu8duljrt75ooxy2.png)

**Step 3:** Now find the `Add Data` button from the top right corner and tap on it. Then switch the tab at the top to `Files` instead of `Databases` and click on the `Import File` button.

![Files Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc4lo6mkgtr5303wi25l.png)

**Step 4:** Now click on the `Import File` section, browse the dataset file that we just downloaded from the above link and select it. Provide a name for the table in the `Table name` field and simply hit the `Save and continue` button to start the import process. 

![Import File](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lb9atrztxv8ohs2gp4ah.png)

**Step 5:** We will get redirected back to the MindsDB Cloud Editor upon successful import of the dataset. The Query Editor will now have two basic SQL queries listed in it.

Let's execute them one by one and check the results.

The first query should show the list of available Tables. Make sure there's a table in the list with the name that we supplied above while importing the dataset.

``` sql
SHOW TABLES FROM files;
```

![Show Tables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xfeyk5gggfqvsb4w0os.png)

The second query lets you check whether we have the right data records present inside the table that we just imported.

``` sql
SELECT * FROM files.Wines LIMIT 10;
```

![Show Table Data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmpqxmgg5cupgpukue5f.png)

We are now ready with the data table and can now proceed to the next section.

## Training a Predictor Model

With MindsDB, training a Predictor Model can be as easy as writing a SQL query and then executing it. So, let's see how we can do that by following the steps below.

**Step 1:** MindsDB offers a `CREATE PREDICTOR` statement that we can use to create and train the model. The format of the syntax is as follows.

``` sql
CREATE PREDICTOR mindsdb.predictor_name       (Your Predictor Name)
FROM database_name                            (Your Database Name)
(SELECT columns FROM table_name LIMIT 10000)  (Your Table Name)
PREDICT target_parameter;                     (Your Target Parameter)
```

An actualy query with real `field_names` instead of the placeholders will look like the one below.

``` sql
CREATE PREDICTOR mindsdb.wine_predictor
FROM files 
(SELECT * FROM Wines LIMIT 10000)
PREDICT quality;
```

![Create Predictor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgj0oh21drnxb1hy7ug4.png)

**Step 2:** The model might take some time to complete its training based on the size of the training data provided.

While we wait, we can check the status of the model with the command below. If the query returns `complete`, then the model is ready to do the predictions. But, if it returns `generating` or `training`, it is advised to wait until the status is `complete`.

``` sql
SELECT status
FROM mindsdb.predictors
WHERE name='Name_of_the_Predictor_Model';
```

The real query will be something like this.

``` sql
SELECT status
FROM mindsdb.predictors
WHERE name='wine_predictor'
```

![Predictor Status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i2h3zj4e19eiuxhd070.png)

As we got the `complete` status, we can now do the predictions for wine quality.

## Describing the Predictor Model

It is really very important to understand the details about our Predictor model before directly jumping in to do the predictions.

So, in this section we will try to figure out the details of our model in 3 different ways using the `DESCRIBE` statement.

- By Features
- By Model
- By Model Ensemble

### By Features

This query is designed to return the roles of each column in the table for the model and also mentions the specific encoders used on each of these columns to train the model.

``` sql
DESCRIBE mindsdb.predictor_name.features;
```

![Features](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9a4i5ew0etxou9f6fafm.png)

### By Model

This query fetches the list of all the available candidate models that were used during training. The candidate model which has `1` under its `selected` column is selected to be used in the Predictor model and is supposed to have the `best performance` value.

``` sql
DESCRIBE mindsdb.predictor_name.model;
```

![Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbypxbqmjcntqz0kycxr.png)

### By Model Ensemble

This query is designed to provide us with a JSON output with the list of different parameters that helped to determine the best candidate model for the Predictor.

``` sql
DESCRIBE mindsdb.predictor_name.ensemble;
```

![Ensemble](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/do4kkic4d0jm2h1uo3dr.png)

It's time to now move on to the interesting part of predicting the wine quality values.

## Querying the Model

MindsDB provides the ease of predicting target values with the help of the `SELECT` statement only. Here, we can predict the quality of wine using a simple `SELECT` query and ask the model to return us the predicted quality back.

It should be noted that the quality of wine is determined by multiple feature values altogether. The accuracy may degrade if some of these feature values are left out.

But let us still try to predict the wine quality based on a few feature sets with a query like this.

``` sql
SELECT target_value_name, target_value_confidence, target_value_confidence
FROM mindsdb.predictor_name
WHERE feature1=value1 AND feature2=value 2,...;
```
Our real query will take the values of the feature parameter set like this.

``` sql
SELECT quality,quality_confidence,quality_explain
FROM mindsdb.wine_predictor
WHERE pH=3.3 AND density=0.997 AND alcohol=9.4 AND sulphates=0.46;
```

![Wine Quality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6g6ipubqgrlxtgetca00.png)

Here the predicted `quality` (Wine Quality) is 5, so the wine can neither be called good nor bad.

It's time to pass values of all the feature sets to have a more accurate prediction. Now that makes the query like the one below.

``` sql
SELECT quality,quality_confidence,quality_explain
FROM mindsdb.wine_predictor
WHERE pH=3.5 AND density=0.9976 AND alcohol=13 AND sulphates=0.86 
AND fixed_acidity=10.3 AND volatile_acidity=0.3 AND citric_acid=0.72 
AND residual_sugar=2.5 AND chlorides=0.075 AND free_sulfur_dioxide=11 
AND total_sulfur_dioxide=20;
```

![Wine Quality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exori9qkf0bq7ghhy1yt.png)

This can be definitely called a wine of good quality as the value is 8.

That's it! We have now successfully predicted the wine quality using the Predictor Model.

> Note: Parameters like `quality_confidence`, `quality_explain` will fetch us the confidence value and other details like anomaly, truth value and probability classes respectively.

## Conclusion

This marks the end of the tutorial. It's time for a quick recap now. Initially, we started with creating a MindsDB Cloud account, imported the dataset and created a table using the cloud GUI, created and trained a Predictor model, described its model details in three possible ways and finally predicted the quality of wine.

As this tutorial is over now, I would recommend all of you to create your own MindsDB accounts for free and then give it a spin. You can also install and use it locally. All the related instructions for doing this can be found [here](https://docs.mindsdb.com/setup/self-hosted/docker/).

Lastly, before you leave, don't forget to key in your feedback in the `Comments` section below and show some love by dropping a `LIKE` on this article.


[![MindsDB Sponsor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54redaz26efthtchdvlh.png)](https://mindsdb.com/)

[![Sponsorship Badge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kksqbxtz7ff9kup9ylyt.png)](https://github.com/sponsors/Rutam21)