In this tutorial, we will be learning to:
👉 Connect a MongoDB database to MindsDB.
👉 Train a model to predict the genre of books based on the titles and descriptions.
👉 Get a prediction from the model given certain input parameters.
We will be using the Books genre dataset 📖 that can be downloaded from here. You are also free to use your own dataset and follow along the tutorial.
#️⃣Pre-requisites
- This tutorial is primarily going to be about
MindsDB
so the reader is expected to have some level of familiarity with MongoDB Atlas. - In short, MongoDB Atlas is a Database as a Service(DaaS), which we will be using to spin up a MongoDB database cluster and load our dataset.
- Download a copy of the Books genre dataset from here.
- You are also expected to have an account on
MindsDB Cloud
. If not, head over to https://cloud.mindsdb.com/ and create an account. It hardly takes a few minutes. ⚡ - We also need MongoDB Compass to load the dataset into the collection. It can be downloaded from here.
#️⃣About MindsDB
MindsDB
is a predictive platform that makes databases intelligent and machine learning easy to use. It allows data analysts to build and visualize forecasts in BI dashboards without going through the complexity of ML pipelines, all through SQL. It also helps data scientists to streamline MLOps by providing advanced instruments for in-database machine learning and optimize ML workflows through a declarative JSON-AI syntax.
Although only SQL is mentioned, MongoDB is also supported.
#️⃣Dataset Overview
The dataset contains information about the title, genre and summary of books.
#️⃣Setting up a Cluster on MongoDB Atlas
Head over to https://cloud.mongodb.com/ and create a new project named
booksdb
and within it a new database cluster namedcluster0
. Typically, it takes a minute or two to provision a cluster. Once it is done, you should have something like this:
Click on "Connect" button. In pop-up modal, you will be asked to select a tool to interact with your cluster. For this tutorial I'm choosing MongoDB Compass for connection.
After that, you will be asked to create a new database user. After providing a username and password, click on the "Create Database User" button.
In the next step, select "Connect using MongoDB Compass". Copy the connection string which should look like this:
mongodb://cloud.mindsdb.com/
And from Advanced Connection Option, select Authentication method as "Username/Password" and enter your MindsDB cloud credentials.
Now we will connect to our database from MongoDB Compass and load our dataset.
#️⃣Loading the dataset with MongoDB Compass
Head over to https://cloud.mindsdb.com/ and after login click on "Add data" to import data from csv file.
This imported data will be available in files database.Open MongoDB Compass. Paste the connection string and click on "Connect". On successful authentication, you will be welcomed by this screen.
Now open Mongosh terminal in MongoDB Compass, enter the command below to insert a database in MindsDB.
db.databases.insertOne({
name: "BooksDB", // name of database
engine: "mongodb", // databaase engine to use,
connection_args: {
"port": 27017, // connection port
"host": "mongodb://cloud.mindsdb.com:27017", // connection host
"database": "files" // connecting database
}
});
We are now ready to train an ML model to predict the books_genre
using MindsDB
.
#️⃣Training the ML Model
- Head over to MindsDB cloud to create predictor. Enter the command below and execute.
CREATE PREDICTOR mindsdb.books_genre_predictor
FROM files
(SELECT * FROM books)
PREDICT genre;
- That's how simple training an ML model is with MindsDB. Now all you have to do is wait for a few minutes for the model to get trained after which you will be able to run queries and get predictions on the books_genre.
#️⃣Running Queries to get predictions
Once the status changes to COMPLETE, it means that our model is now ready and we can start getting predictions.
We can see that the model has an accuracy of 98.8%, which is impressive!To start getting prediction, enter the query below in the terminal:
db.books_genre_predictor.find({title:"Yendi", summary:"Six months after he took control of his own territory in the criminal"})
Here we wanted to know the genre of a book with following details.
- We can see that the model predicted with 99.9% confidence that the genre for the given book details is "fantasy".
You can play with the inputs and run a few more queries and observe the results.
Thank you for reading this article. If you liked it, please like and share it with others. If you want to learn more about MindsDB
, visit their official documentation and/or talk to the team behind it on Slack.
See you in my next article, Happy Querying! 📉
Top comments (0)