Predicting Flight Prices with MindsDB
Published: 2023-10-14 13:17:14

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

This tutorial will teach you how to train a model to predict flight prices based on many variables, such as duration, stops etc.. with 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/datasets/shubhambathwal/flight-price-prediction)



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




**_Step 3_**: Click the Upload file option under the Add List.
(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/qpgcufpks739k91ab82i.png)




_**Step 4**_: Name the Table



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




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

Run this Query:

```
SHOW TABLES FROM files;
```

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


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

Change the paramaters with the ones you want to use

```
CREATE PREDICTOR mindsdb.flightprice_predictor
FROM files 
(SELECT * FROM FlightPrices LIMIT 10000)
PREDICT price;
```



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

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


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





## Describe the Model

Before we proceed to the final part of predicting flight prices, 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.flightprice_predictor.features;
```



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



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



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

We will start by predicting that only 1 feature parameter is supported by price and therefore the query should look like this.

NOTE: While predicting always input multiple feature parameters as the prediction accuracy degrades.

```
SELECT price
FROM mindsdb.flightprice_predictor
WHERE duration ='2.29';
```



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


```
SELECT price
FROM mindsdb.flightprice_predictor
WHERE airline = 'Vistara' and duration = '2.29';
```



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




You can play around with the values to predict different prices based on the dataset

We have now successfully predicted the Flight prices 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.