Tutorial to Predict Amsterdam Housing Prices Using MindsDB and MongoDB
Tags: mindsd, machinelearning, hacktoberfest, mongodb
Published: 2022-11-01 03:28:03

## :ballot_box_with_check: What is MindsDB?

Most businesses today utilize machine learning to power their decisions. From recommendation systems, to optimized shipping routes to customer sentiment analysis, companies have now incorporated machine learning and artificial intelligence into all aspects of business. And the data generated by the companies serves as the backbone of all these ML algorithms and models. Handling and processing data for preparing machine learning models can be expensive and cumbersome. This is where MindsDB comes into the picture. MindsDB brings machine learning to your database allowing you to leverage machine learning techniques on the data stored inside your database. With the help of MindsDB, you can create, train and optimize ML models right in your database without requiring additional platforms making machine learning more accessible and efficient. 

## :ballot_box_with_check: What will we be learning in this tutorial?

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

First and foremost, we will prepare our setup that is essential to start forecasting with MindsDB and MongoAPI.

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

:books: **Part 2 : Generating ML Models**

We will see how to create and train ML models in our database. In this tutorial, we will be predicting the Housing Prices of Amsterdam using [this dataset](https://www.kaggle.com/datasets/thomasnibb/amsterdam-house-price-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

We will be explaining this section briefly, so that we can move on to our predictions. For a more detailed explanation please refer to [this article](https://dev.to/nupoorshetye/tutorial-to-predict-the-weather-using-mindsdb-and-mongodb-3blm).

:pushpin: **Download MongoDB and MongoDB Compass**

To get started we must have both [MongoDB Community Edition](https://www.mongodb.com/try/download/community) and [MongoDB Compass](https://www.mongodb.com/try/download/compass) installed and working in our systems.  

Once you are done with the installation of both MongoDB and MongoDB Compass we can get going with our tutorial.

:pushpin: **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.

:pushpin: **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 means that we have succeeded in integrating MindsDB with MongoDB. Now let us move to the second part of our tutorial where we will be generating an ML model.

## :ballot_box_with_check: Part 2 : Generating ML Models

:pushpin: **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. 

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

Now we have to save this database that we have just created in our MongoDB database. We can use the databases.insertOne() command for this purpose.

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

```
db.databases.insertOne({
    name: "HousePricingDB", // 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 MongoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ainlkoggo7xq4h1sqt1.png)

If you get such a response, that means your database is successfully created!

:pushpin: **Understanding our Problem Statement**

We saw earlier that we will be predicting the housing prices of Amsterdam using [this Kaggle dataset](https://www.kaggle.com/datasets/thomasnibb/amsterdam-house-price-prediction). Let us take a closer look into our database that we have set up. Our database consists of the following fields :

**Id** : Gives the serial number of the data entry

**Address** : Gives the address of the particular property/house

**Zip** : Gives the Zip Code of the house

**Price** : Gives the total cost of the house

**Area** : Gives the total area of the house we are looking into

**Room** : Gives the number of rooms in the house

**Lon** : Gives the exact longitudinal co-ordinates of the property

**Lat** : Gives the exact latitudinal co-ordinates of the property

We can run the following query in our MindsDB Console to see our database where we can see all our fields :

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

This is what will be displayed :

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

Now let us understand what we are trying to predict. We have been given a database consisting of various fields and we want to predict the Price of a particular plot or house according to its features like the address, zip code, number of rooms, area, etc. We are going to train an ML model that _**learns**_ how the Price of a house varies according to the features mentioned above. And once we have trained our model, we can input the details of a house or property and our ML Model will predict what its estimated Price will be. 

Sounds like a difficult task? Let us see how MindsDB can do that for us in a simple query!

:pushpin: **Creating the Predictor Model**

Now that our database is ready, we can go ahead and create our ML Model. As we have seen, 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.house_pricing_predictor
FROM files
(SELECT * FROM housepricing)
PREDICT Price;
```

Here house_pricing_predictor is the name of our Predictor Model and Price is our target value that we want to predict. We want to predict the housing prices taking into account all the other attributes. Click on Run or press Shift+Enter to run our query. If there are no hiccups, we will get a Query Successfully Completed message. 

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

And that’s it! We have created and trained a Machine Learning model by a single query! That is the magic of MindsDB!

:pushpin: **Querying the Predictor Model**

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

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

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

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

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

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

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

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

The query for the same is : 

```
SELECT Price, Price_explain
FROM mindsdb.house_pricing_predictor
WHERE Id = 8
AND Address = "Da Costakade 32 II, Amsterdam"
AND Zip = "1053 WL"
AND Area = 80
AND Room = 2
AND Lon = 4.871555
AND Lat = 52.371041;
```

And lo and behold! Our model predicts the Price of the house according to its attributes entered by us :

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

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

Use the following command to do so :

```
db.house_pricing_predictor.find({
Id:"8", 
Address:"Da Costakade 32 II, Amsterdam", 
Zip:"1053 WL", 
Area:"80", Room:"2", 
Lon:"4.871555",
Lat:"52.371041"})
```

And this is our output :

![Querying the model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwohrj5sefq92cjyu4cs.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 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).