Tutorial to Predict the Weather Using MindsDB and MongoDB
Tags: mindsdb, mongodb, machinelearning, hacktoberfest
Published: 2022-11-01 03:24:23

## :ballot_box_with_check: What is MindsDB?

MindsDB is a tool that helps you to leverage machine learning techniques on the data stored inside your database. MindsDB brings machine learning into the database reducing complex workflows and extended durations of processing, creation and deployment. With MindsDB, you can build, train, optimize, and deploy your ML models without the need for other platforms. MindsDB helps you generate forecasts and predictions from your database which can be accessed with the help of a simple query.  The revolutionary model of MindsDB helps businesses make decisions faster and more efficiently providing real-time value to the company.

## :ballot_box_with_check: What will the tutorial entail?

:memo: **Part 1 : Setting up the requirements** 

Firstly, we will set up all the tools we will be needing to utilize the functionalities of MindsDB using MongoAPI and get the dice rolling.

* Download MongoDB and MongoDB Compass
* Getting started with MindsDB
* Integrating MindsDB with MongoDB

:memo: **Part 2 : Generating ML Models**

We will see how we can generate ML models in our database itself and go over the queries required to do so. In today's tutorial, we will be generating an ML Model to predict the weather using [this dataset](https://www.kaggle.com/datasets/ananthr1/weather-prediction).

* Preparing the database
* Understanding our Problem Statement
* Creating the Predictor Model
* Querying the Predictor Model

## :ballot_box_with_check: Part 1 : Setting up the requirements


:point_right: **Download MongoDB and MongoDB Compass**

In this tutorial, we will be using MongoDB as our database and MongoDB Compass to interact with our database and with MindsDB as well. For anyone who is a complete newbie, MongoDB is a cross-platform NoSQL database that is used to store high volumes of data. MongoDB does not use tables and rows like a traditional relational database, instead, the data is organized as collections of documents containing key:value pairs. MongoDB Compass is the GUI for MongoDB so it provides an interface for the user to interact with the database, greatly simplifying things for beginners.

We won’t be diving too deep into the installation of MongoDB. There are two versions of MongoDB available, a free Community Edition and a paid Enterprise Edition. We will be working with the Community Edition as it is more than enough to test out the features of the database. Download the [Community Edition](https://www.mongodb.com/try/download/community) of MongoDB and follow the [installation steps](https://www.mongodb.com/docs/manual/installation/) to ensure correct installation. During the installation you will be provided with an option to download the MongoDB Compass as well. Make sure to select the checkbox or else you can download the [MongoDB Compass](https://www.mongodb.com/try/download/compass) separately as well and [install it](https://www.mongodb.com/docs/compass/current/install/).

Once you have downloaded both MongoDB and MongoDB Compass successfully, we can move on to the next step.

**NOTE** :  Previously the Mongo Shell was bundled together with the MongoDB and didn’t need separate installation. However, now, one must download and install the [Mongo Shell](https://www.mongodb.com/try/download/shell) separately. 
You can download the Mongo Shell and follow the [installation guide](https://www.mongodb.com/docs/mongodb-shell/install/). Make sure you add the MongoDB and Mongo Shell binary files to your environment variables so that you can access them from your command prompt or terminal directly.

:point_right: **Getting started with MindsDB**

MindsDB provides all users with a free MindsDB Cloud version that they can access to generate predictions on their database. You can sign up for the free [MindsDB Cloud Version](https://cloud.mindsdb.com/) by following the [setup guide](https://docs.mindsdb.com/setup/cloud/). Verify your email and log into your account and you are ready to go. Once done, you should be seeing a page like this : 


![MindsDB Cloud Sign Up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqpzyfhmsnqx1awhq84o.png)

If you wish, you can choose to install [MindsDB](https://pypi.org/project/MindsDB/) on your local system using docker image or by using [PyPI](https://pypi.org/project/MindsDB/). However, we will be working with Minds DB Cloud in this tutorial.

:point_right: **Integrating MindsDB with MongoDB**

MindsDB provides us the ability to integrate with MongoDB using the MongoAPI. We can do so by following the given steps.

Open your MongoDB Compass. On the left navigation panel, You will have an option for a New Connection. Click on that Option and you will be provided with the details of your connection.

In the URI Section enter the following : 

```
mongodb://cloud.mindsdb.com/
```

Click on the Advanced Connection Options dropdown. Here your host will be detected as MindsDB Cloud. 

In the Authentication option enter your MindsDB Username and Password. Then click on Save and Connect, give your connection a name and select and color. 


![MongoDB Compass](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdjwx58l86xi17sh4ikc.png)

If you successfully create a connection you will be displayed a page similar to this : 


![MongoDB Compass  Connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhj19wvfcbzc9l0aer2h.png)

In the bottom panel of this page, you will see the the Mongo Shell bar, enlarge it and type the following queries and click Enter.

```
> use mindsdb
> show collections
```

![Mongo Shell Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwjf3uw4vas3a4mc14vn.png)

If you get a result like this, it indicates that you have successfully integrated MindsDB with MongoDB. Whew..!! That was quite a task. But don’t worry, the exciting part is yet to come.

## :ballot_box_with_check: Part 2 : Generating ML Models

:point_right: **Preparing the database**

We will be preparing our database on which we can run our queries and perform our forecasts. On the MindsDB Cloud console, click on the last icon in the left navigation bar. You will see a 'Select Your Data Sources' page. We can add a variety of data sources, however, for this tutorial we will be working with .csv files.

Go to the files section and click on Import File. Import your csv file and provide a name for your database table in which the contents of the .csv file will be stored. Click on Save and Continue. 

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

Now we have to save this database that we have just created in our MongoDB database. We will be using the databases.insertOne() command to do so

To do so, go to the Mongo Shell and type the following command : 

```
db.databases.insertOne({
    name: "WeatherDB", // database name
    engine: "mongodb", // database engine 
    connection_args: {
        "port": 27017, // default connection port
        "host": "mongodb://cloud.mindsdb.com:27017", // connection host
        "database": "files" // database connection          
    }
});
```
On clicking Enter, you must receive the following response : 


![database insert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsumaez6xx9umg7qlymk.png)

If you get such a response, that means your database is all prepped to explore!

:pushpin: **Understanding our Problem Statement**

Our main goal is to predict the weather and we are going to be using [this Kaggle dataset](https://www.kaggle.com/datasets/ananthr1/weather-prediction) that we saw earlier. Our database consists of various parameters or fields as seen below :

**date** : The dates at which the weather was recorded

**precipitation** : All forms of precipitation such as rain or snow that was recorded during the day

**temp_max** : The maximum temperature recorded on the day

**temp_min** : The minimum temperature recorded on the day

**wind** : The wind speed on the particular date

**weather** : The weather that was finally recorded on that day

To have a glimpse at all our database fields we can run the following query in the MindsDB Console :

```
SELECT * FROM files.weather LIMIT 10;
```

We will be given the following output :

![Database Fields](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjn55qk3r8mt51f8on75.png)

Now let us get familiar with our problem statement and what exactly are we trying to predict. As we can see our database contains several fields such as the date, precipitation, temperature and wind speed. And our final field is the weather that was recorded and that we wish to predict. What we want to do is to create an ML model to tell us what the weather on a particular day will be if we provide the remaining details like the date, precipitation, temperature and wind speed. But how will our ML model be able to achieve this task? That is where the training phase kicks in. On the basis of our given database, we will train our ML Model to _**learn**_ how the final output of the weather varies according to the values of the remaining fields. And once we have trained our model, we can input the fields in our database and our ML Model will predict what the estimated weather would be in accordance to those parameters. 

Sounds overwhelming? Let us see how MindsDB can do that for us in just a few seconds!

:point_right: **Creating the Predictor Model**

Now that our database is ready, we can go ahead and create our very first Predictor Model. The Predictor Model is basically a trained Machine Learning Model that can be used to predict or forecast a particular value known as the target variable or target value.

We use the CREATE PREDICTOR command to create and train a ML model. Enter the following command in your MindsDB console : 

```
CREATE PREDICTOR mindsdb.weather_predictor
FROM files
(SELECT * FROM weather)
PREDICT weather;
```

Here weather_predictor is the name of our Predictor Model and weather is our target value that we want to predict. Click on Run or press Shift+Enter to run our query. If we don't encounter any issues, we will get a Query Successfully Completed message. 


![MindsDB Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ritha7kppfosvaprrzrx.png)

And that’s it! We have created and trained a Machine Learning model in just a few lines of code! That is the magic of MindsDB!

:point_right: **Querying the Predictor Model**

We can see the details of our machine learning model by typing the following command in our Mongo Shell : 

```
 db.predictors.find({name:"weather_predictor"})
```

When we press Enter we get all the details of our Predictor Model like its status, accuracy, target value and errors. 

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

To see the same in our MindsDB Console, enter the following query : 

```
SELECT *
FROM mindsdb.predictors
WHERE name='weather_predictor';
```

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

Now finally we can query our ML model to predict the target value of a particular entry. 

The syntax for the same is : 

```
SELECT weather, weather_explain
FROM mindsdb.weather_predictor
WHERE precipitation = 0.8
AND date = "2012-01-03"
AND temp_max = 11.7
AND temp_min = 7.2
AND wind = 2.3;
```

We will obtain the following prediction for the weather according to our parameters entered :


![Weather Predict](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czwqdc2uz45on6odt9f5.png)

We can also query our ML model through the Mongo Shell and start generating predictions.

Use the following command :

```
db.weather_predictor.find({
date:"2012-01-03", 
precipitation:"0.8", 
temp_max:"11.7", 
temp_min:"7.2", 
wind:"2.3"})
```

We are displayed this output:

![Querying the model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32yzowmketere46ec5qg.png)

## :ballot_box_with_check: Conclusion : 

Using MindsDB we have successfully created and trained a Machine Learning model in our database and unlocked the ability to generate in-database forecasts. You can visit the [MindsDB Documentation](https://docs.mindsdb.com/) to know the various features of MindsDB.

## :ballot_box_with_check: What’s Next?

If you enjoyed following along to this tutorial, make sure to [Sign Up](https://cloud.mindsdb.com/) for a free MindsDB Cloud account and continue exploring! [Kaggle](https://www.kaggle.com/) is a great resource for you to find similar datasets and you can create and train an ML model of your own with the help of MindsDB. You can also check them out on [GitHub](https://github.com/mindsdb/mindsdb).