Predict Purchasing Power per Country using MindsDB
Tags: tutorial, machinelearning, datascience, database
Published: 2022-10-09 21:18:20


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

## Introduction

MindsDB embeds AI and machine learning into databases to assist groups working with day-to-day data to determine patterns, predict trends, and train models.

Currently, MindsDB offers two types of services to its users.
- [Self-Hosted](https://docs.mindsdb.com/setup/self-hosted/docker/)
- [MindsDB Cloud](https://docs.mindsdb.com/setup/cloud/)

Not only it transforms traditional databases to AI-enabled tables, but also offers a free version (with 10K rows limit) to all its users around the globe.

So, in this tutorial we will be using MindsDB Cloud to train a Predictor Model that can predict the purchasing power of a country based on its `cost_index` and `monthly_income`. You can download the dataset we will use for this tutorial [here](https://www.kaggle.com/datasets/madhurpant/world-economic-data?select=cost_of_living.csv).

Let's get started now with the rest of the steps.

## Adding Data to MindsDB Cloud

We can upload the [dataset](https://www.kaggle.com/datasets/madhurpant/world-economic-data?select=cost_of_living.csv) we downloaded above directly in the MindsDB Cloud GUI and create a table from it easily. 

Now let's setup our MindsDB Cloud account first so that we can proceed with the next steps.

**Step 1:** Create a new MindsDB Cloud account [here](https://cloud.mindsdb.com/register) if you don't have one or else simply [login](https://cloud.mindsdb.com/login) to your existing account.

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

**Step 2:** Once you're in logged in, you can find the MindsDB Cloud Editor opened up for you.

You will find the Query Editor at the top, there's a Result Viewer at the bottom and Learning Hub resources are present at the right side.

![MindsDB Editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fefzy42j56jq7c0sxan.png)

**Step 3:** Now click on `Add Data` from the top right corner and switch the tab from `Databases` to `Files`. Then hit the `Import File` button to proceed further.

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

**Step 4:** Browse and open the `.CSV` file that you just downloaded on the `Import File` dashboard, provide a suitable table name in the `Table Name` field and then hit the `Save and Continue` button to create a table in the given name with the dataset you just uploaded.

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

**Step 5:** Once the file is successfully uploaded, you will be automatically redirected to the MindsDB Cloud Editor page where you can see two queries that we can execute to check whether the table is created with all the proper data or not.

![Query Editor after Upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a08qxe4za0ab9spumwj1.png)

Execute both the queries to check the results.

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

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

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

![Select from Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlax3u6zdubk837qzf9y.png)

We are now ready to move to the next part of the tutorial where we need to train the Predictor model.

## Training a Predictor Model

It is really easy to train a Predictor model using MindsDB. We have to just use a basic SQL kind of statement to do the job.

**Step 1:** We can now simply use the `CREATE PREDICTOR` statement to create and train a new Predictor model. Find the syntax below.

``` 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)
```
We have to execute this query with the correct parameters required and it should return a successful status in the Result Viewer. The actual query should look something like this.

``` sql
CREATE PREDICTOR mindsdb.country_capita_predictor
FROM files 
(SELECT cost_index monthly_income FROM country_capita LIMIT 10000)
PREDICT purchasing_power_index;
```

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

> Note: You can only train a Predictor Model with 10K data rows maximum in free version. So, we have used `LIMIT 10000` so that the query doesn't error out in case we have more rows in the table.

**Step 2:** Depending on how large the dataset is, the Predictor may take a while to complete its training. Meanwhile, we can check the status of the Predictor with the following statement.

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

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

## Describing the Predictor Model

In this section, we will try to find out some basic details about the Predictor model that we just trained above. MindsDB provides a simple `DESCRIBE` statement that we can use to query different details about the model.

Basically, we can use the `DESCRIBE` statement in the following three ways.

- By Features
- By Model
- By Model Ensemble

### By Features

This statement returns the type of encoders used on each column to train the model along with the role that each column serves for the model. The query looks something like this.

``` sql
DESCRIBE mindsdb.predictor_model_name.features;
```

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

### By Model

This statement returns the candidate model that is selected for the Predictor to do the predictions. The list contains all the candidate models used while training the data and the one with the highest performance gets selected and the `selected` column for this is set to 1 while other are set at 0.

The statament should be as follows.

``` sql
DESCRIBE mindsdb.predictor_model_name.model;
```

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

### By Model Ensemble

This statement returns a JSON object with a list of different attributes that are considered to select the best candidate model for the Predictor. We can use the query mentioned below to fetch this output.

``` sql
DESCRIBE mindsdb.predictor_model_name.ensemble;
```

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

## Querying the Model

The Predictor model is now ready to predict the target values for us. MindsDB eases the user experience as it allows simple SQL-like query statements to do the job.

We will start by predicting the `purchasing_power_index` value based on only single feature parameter. The query for this will be as follows.

``` sql
SELECT purchasing_power_index purchasing_power_index_explain
FROM mindsdb.country_capita_predictor
WHERE cost_index =150;
```

![Single Parameter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh1hc6ttcyh70giymhyq.png)

Now, let's try to predict the `purchasing_power_index` value using both the feature parameters i.e., `cost_index` and `monthly_income`. The query will be like the one below.

``` sql
SELECT purchasing_power_index
FROM mindsdb.country_capita_predictor
WHERE cost_index =200 AND monthly_income=100;
```

![Multiple Feature Parameter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3p8ky73iocgy3287kfkp.png)

## Conclusion

We have now reached the end of this tutorial. It's time for a quick recap now. So, in this tutorial, we set up our MindsDB Cloud account, uploaded our dataset to the MindsDB GUI directly, created a Predictor Model from the uploaded dataset and finally predicted the purchasing power of a country based on the `cost_index` and `monthly_income`.

Now, I recommend all of you to create your own MindsDB accounts, find some interesting datasets from the internet and then train some amazing Predictor models with MindsDB. Remember, it's all free to use!

Lastly, before you leave this page, show some support by hitting the `LIKE` button and also don't forget to key in your feedback below.

[![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/6ritjzew9z0v0fpfuhag.png)](https://github.com/sponsors/Rutam21)