Predicting Wine Quality with MindsDB
Tags: database, datascience, machinelearning, ai
Published: 2022-11-04 17:26:57


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

## Introduction

MindsDB is an Open-Source AI Layer for databases, For more details, Checkout this [blog](https://dev.to/learnearnfun/quickstart-to-mindsdb-1ifm) to know more

In this tutorial we will be predicting the quality of wine based on its features 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/code/febymelania/aggregation-red-wine-quality-datasets/data?select=winequality-red.csv)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u2u40w6j0osn1pr1bqg.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



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


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

Run this Query:

```
SHOW TABLES FROM files;
```

If you see the dataset, then it's imported successfully!

We have imported our dataset into MindsDB, next up we will be creating a Predictor Model!


## Training a Model


**_Step 1_**: Creating a Predictor Model

MindsDB provides a syntax which exactly does that!

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

Simply replace the paramaters with the ones you want to use

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3786mi3817pz6llemdev.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='wine_predictor'
```

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

## Describe the Model

Before we proceed to the final part of predicting the wine quality, 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;
```


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


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


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



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

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



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 understanding our Predictor model, let's move on to predicting values.

## Predicting the Target Value

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.

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

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



As the predicted quality is 5, It states that the wine is neither good nor bad.

It's time to pass values of all the features to have a more accurate prediction.

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


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



As the predicted Quality is 8, this wine is good!

Great! We have now successfully predicted the wine quality with MindsDB

## Conclusion

This concludes the tutorial here. 

Lastly, before you leave, I would love to know your feedback in the Comments section below and it would be really great if you drop a `LIKE` on this article.