Predicting Water Purity using MindsDB
Tags: tutorial, machinelearning, ai, database
Published: 2022-10-30 12:46:08


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdzoggi0t1p1p8zywbkf.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 purity of water based on several parameters by a dataset found in kaggle!

## 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/adityakadiwal/water-potability)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ltgyxf668b4iv85qgij.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: WaterPU (You can name it anything you like!)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85zbx6xwpnhpg92opksd.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 WaterPU or whatever you named it that means it's successfully imported!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nzvo0vhqjpg1v8eu3nv.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.
For that MindsDB provides a syntax:

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

The Actual query for me, looks like this:

```
CREATE PREDICTOR mindsdb.water_purity
FROM files 
(SELECT * FROM WaterPU LIMIT 10000)
PREDICT Potability;
```


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



NOTE: We are predicting Potability as that's in the dataset, if you write anything else, e.g. Purity, Quality

You won't be able to Predict the Purity!


**_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 syntax:

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

The Actual Query looks something like this:

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

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


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




## Describe the Model

Before we proceed to the final part of predicting the water 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/vtxsioeu9fsl6jaiud25.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/lfcuaznqk5kk0os5c9p4.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/aox813g2ajg6lucp0bce.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 now understanding our Predictor model, let's move on to prediciting values in the next section.

## Predicting the Target Value

Predicitng the water Purity/Quality/Potability is as easy as running a simple SELECT statement using the Predictor.

As water purity depends on many feature parameters, It is preferred to enter all the parameters, However we can go forward by passing a few of them

The syntax for the query will be something like this:

```
SELECT target_value_name, target_value_confidence, target_value_confidence
FROM mindsdb.predictor_name
WHERE feature1=value1 AND feature2=value 2,...;
```
Now, replacing the variables in the above query, the actual query will be like this.


```
SELECT Potability,Potability_confidence,Potability_explain
FROM mindsdb.water_purity
WHERE ph=2.6 AND Hardness=210 AND Solids=18645.233 AND Chloramines=6.546;

```


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



As the predicted Purity is 0, this water is not safe for human consumption.

We will now pass all the required feature parameters to obtain a more accurate prediction of the water quality. So, the query now becomes something like this.

```
SELECT Potability,Potability_confidence,Potability_explain
FROM mindsdb.water_purity
WHERE ph=6.9 AND Hardness=201 
AND Solids=11350.675 AND Chloramines=4.3 AND Sulfate=NULL 
AND Conductivity=467.5 AND Organic_carbon=9.98 AND Trihalomethanes=89.686 AND Turbidity=4.99;
```

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



As the predicted Purity is 1, this water is safe for human consumption.

Fantastic! We have now successfully predicted the water quality using a Predictor.


target_parameter: This returns the value we want to predict.
target_parameter_confidence: This returns how confident the model is about the Prediction.
target_parameter_explain: This returns all the details about the predicted target_value.

## 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 water purity value.

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