A Guide to Predicting Car Prices with MindsDB and MongoDB

A Guide to Predicting Car Prices with MindsDB and MongoDB

MindsDB is a tool that helps you leverage machine learning techniques on the data stored inside your database. By bringing machine learning into the database, MindsDB reduces 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 also 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.

Setting up the requirements

This tutorial will use MongoDB as the database and MongoDB Compass as the tool for interacting with the database and MindsDB. MongoDB is a NoSQL database that can store high volumes of data, organizing it into collections of documents containing key:value pairs instead of tables and rows.

MongoDB Compass is the graphical user interface (GUI) for MongoDB, simplifying database interaction for beginners. We will work with the free Community Edition of MongoDB, which is sufficient for testing database features. To ensure correct installation, download the Community Edition and follow the installation steps, selecting the option to download MongoDB Compass during the process.

Alternatively, download MongoDB Compass separately and install it. Once both MongoDB and MongoDB Compass are successfully downloaded, we can proceed to the next step.

Getting started with MindsDB

In this tutorial, we will be using MindsDB Cloud, which is a free version of MindsDB that allows all users to access and generate predictions on their databases. To sign up for the MindsDB Cloud version, follow the setup guide, verify your email, and log into your account.

Alternatively, you can also choose to install MindsDB on your local system using a docker image or PyPI, but for this tutorial, we will be using MindsDB Cloud.

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.

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

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

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.

Generating ML Models

To perform forecasts and run queries, we need to create a database. On the MindsDB Cloud console, navigate to the left navigation bar and click on the last icon. This will take you to the 'Select Your Data Sources' page where you can add different types of data sources. However, for this tutorial, we will be using .csv files.

Navigate to the files section and select Import File. Choose the .csv file you want to import and give a name for your database table where its contents will be stored. Afterward, click on Save and Continue to complete the process.

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: "carPrice", // 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 :

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

Creating the Predictor Model

Our main goal is to predict the car price and we are going to be using this Kaggle dataset.

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.car_price_prediction
FROM files
(SELECT * FROM carPrice)
PREDICT Present_Price;

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!

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:"car_price_prediction"})

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

SELECT *
FROM mindsdb.predictors
WHERE name='car_price_prediction';

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

The syntax for the same is :

SELECT Driven_kms,Year, Present_Price
FROM mindsdb.car_price_prediction
WHERE Driven_kms =  20000
AND Fuel_Type = "Petrol"
AND Year = 2013

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

db.car_price_prediction.find({
Driven_kms:"20000", 
Fuel_Type:"Petrol", 
Year:"2013"})

Conclusion

We have accomplished the creation and training of a Machine Learning model within our database by utilizing MindsDB, which has enabled us to produce in-database predictions. To learn more about the functionalities of MindsDB, please refer to the MindsDB Documentation.