☑️ 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.
☑️ What will we be learning in this tutorial?
📚 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
📚 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.
- Preparing the database
- Understanding our Problem Statement
- Creating the Predictor Model
- Querying the Predictor Model
☑️ 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.
📌 Download MongoDB and MongoDB Compass
To get started we must have both MongoDB Community Edition and MongoDB 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.
📌 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 by following the setup guide. Verify your email and log into your account and you are ready to go. Once done, you should be seeing a page like this :
If you wish, you can choose to install MindsDB on your local system using docker image or by using PyPI. However, we will be working with Minds DB Cloud in this tutorial.
📌 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 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.
☑️ Part 2 : Generating ML Models
📌 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.
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 :
If you get such a response, that means your database is successfully created!
📌 Understanding our Problem Statement
We saw earlier that we will be predicting the housing prices of Amsterdam using this Kaggle dataset. 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 :
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!
📌 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.
And that’s it! We have created and trained a Machine Learning model by a single query! That is the magic of MindsDB!
📌 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.
To see the same in our MindsDB Console, enter the following query :
SELECT *
FROM mindsdb.predictors
WHERE name='house_pricing_predictor';
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 :
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 :
☑️ 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 to know the various features of MindsDB.
☑️ What’s Next?
If you enjoyed following along to this tutorial, make sure to Sign Up for a free MindsDB Cloud account and continue exploring! Kaggle 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.
Top comments (0)