AWS Machine Learning Blog
Use Amazon DocumentDB to build no-code machine learning solutions in Amazon SageMaker Canvas
We are excited to announce the launch of Amazon DocumentDB (with MongoDB compatibility) integration with Amazon SageMaker Canvas, allowing Amazon DocumentDB customers to build and use generative AI and machine learning (ML) solutions without writing code. Amazon DocumentDB is a fully managed native JSON document database that makes it straightforward and cost-effective to operate critical document workloads at virtually any scale without managing infrastructure. Amazon SageMaker Canvas is a no-code ML workspace offering ready-to-use models, including foundation models, and the ability to prepare data and build and deploy custom models.
In this post, we discuss how to bring data stored in Amazon DocumentDB into SageMaker Canvas and use that data to build ML models for predictive analytics. Without creating and maintaining data pipelines, you will be able to power ML models with your unstructured data stored in Amazon DocumentDB.
Solution overview
Let’s assume the role of a business analyst for a food delivery company. Your mobile app stores information about restaurants in Amazon DocumentDB because of its scalability and flexible schema capabilities. You want to gather insights on this data and build an ML model to predict how new restaurants will be rated, but find it challenging to perform analytics on unstructured data. You encounter bottlenecks because you need to rely on data engineering and data science teams to accomplish these goals.
This new integration solves these problems by making it simple to bring Amazon DocumentDB data into SageMaker Canvas and immediately start preparing and analyzing data for ML. Additionally, SageMaker Canvas removes the dependency on ML expertise to build high-quality models and generate predictions.
We demonstrate how to use Amazon DocumentDB data to build ML models in SageMaker Canvas in the following steps:
- Create an Amazon DocumentDB connector in SageMaker Canvas.
- Analyze data using generative AI.
- Prepare data for machine learning.
- Build a model and generate predictions.
Prerequisites
To implement this solution, complete the following prerequisites:
- Have AWS Cloud admin access with an AWS Identity and Access Management (IAM) user with permissions required to complete the integration.
- Complete the environment setup using AWS CloudFormation through either of the following options:
- Deploy a CloudFormation template into a new VPC – This option builds a new AWS environment that consists of the VPC, private subnets, security groups, IAM execution roles, Amazon Cloud9, required VPC endpoints, and SageMaker domain. It then deploys Amazon DocumentDB into this new VPC. Download the template or quick launch the CloudFormation stack by choosing Launch Stack:
- Deploy a CloudFormation template into an existing VPC – This option creates the required VPC endpoints, IAM execution roles, and SageMaker domain in an existing VPC with private subnets. Download the template or quick launch the CloudFormation stack by choosing Launch Stack:
- Deploy a CloudFormation template into a new VPC – This option builds a new AWS environment that consists of the VPC, private subnets, security groups, IAM execution roles, Amazon Cloud9, required VPC endpoints, and SageMaker domain. It then deploys Amazon DocumentDB into this new VPC. Download the template or quick launch the CloudFormation stack by choosing Launch Stack:
Note that if you’re creating a new SageMaker domain, you must configure the domain to be in a private VPC without internet access to be able to add the connector to Amazon DocumentDB. To learn more, refer to Configure Amazon SageMaker Canvas in a VPC without internet access.
- Follow the tutorial to load sample restaurant data into Amazon DocumentDB.
- Add access to Amazon Bedrock and the Anthropic Claude model within it. For more information, see Add model access.
Create an Amazon DocumentDB connector in SageMaker Canvas
After you create your SageMaker domain, complete the following steps:
- On the Amazon DocumentDB console, choose No-code machine learning in the navigation pane.
- Under Choose a domain and profile¸ choose your SageMaker domain and user profile.
- Choose Launch Canvas to launch SageMaker Canvas in a new tab.
When SageMaker Canvas finishes loading, you will land on the Data flows tab.
- Choose Create to create a new data flow.
- Enter a name for your data flow and choose Create.
- Add a new Amazon DocumentDB connection by choosing Import data, then choose Tabular for Dataset type.
- On the Import data page, for Data Source, choose DocumentDB and Add Connection.
- Enter a connection name such as demo and choose your desired Amazon DocumentDB cluster.
Note that SageMaker Canvas will prepopulate the drop-down menu with clusters in the same VPC as your SageMaker domain.
- Enter a user name, password, and database name.
- Finally, select your read preference.
To protect the performance of primary instances, SageMaker Canvas defaults to Secondary, meaning that it will only read from secondary instances. When read preference is Secondary preferred, SageMaker Canvas reads from available secondary instances, but will read from the primary instance if a secondary instance is not available. For more information on how to configure an Amazon DocumentDB connection, see the Connect to a database stored in AWS.
- Choose Add connection.
If the connection is successful, you will see collections in your Amazon DocumentDB database shown as tables.
- Drag your table of choice to the blank canvas. For this post, we add our restaurant data.
The first 100 rows are displayed as a preview.
- To start analyzing and preparing your data, choose Import data.
- Enter a dataset name and choose Import data.
Analyze data using generative AI
Next, we want to get some insights on our data and look for patterns. SageMaker Canvas provides a natural language interface to analyze and prepare data. When the Data tab loads, you can start chatting with your data with the following steps:
- Choose Chat for data prep.
- Gather insights about your data by asking questions like the samples shown in the following screenshots.
To learn more about how to use natural language to explore and prepare data, refer to Use natural language to explore and prepare data with a new capability of Amazon SageMaker Canvas.
Let’s get a deeper sense of our data quality by using the SageMaker Canvas Data Quality and Insights Report, which automatically evaluates data quality and detects abnormalities.
- On the Analyses tab, choose Data Quality and Insights Report.
- Choose
rating
as the target column and Regression as the problem type, then choose Create.
This will simulate model training and provide insights on how we can improve our data for machine learning. The complete report is generated in a few minutes.
Our report shows that 2.47% of rows in our target have missing values—we’ll address that in the next step. Additionally, the analysis shows that the address line 2
, name
, and type_of_food
features have the most prediction power in our data. This indicates basic restaurant information like location and cuisine may have an outsized impact on ratings.
Prepare data for machine learning
SageMaker Canvas offers over 300 built-in transformations to prepare your imported data. For more information on transformation features of SageMaker Canvas, refer to Prepare data with advanced transformations. Let’s add some transformations to get our data ready for training an ML model.
- Navigate back to the Data flow page by choosing the name of your data flow at the top of the page.
- Choose the plus sign next to Data types and choose Add transform.
- Choose Add step.
- Let’s rename the
address line 2
column tocities
.- Choose Manage columns.
- Choose Rename column for Transform.
- Choose
address line 2
for Input column, entercities
for New name, and choose Add.
- Additionally, lets drop some unnecessary columns.
- Add a new transform.
- For Transform, choose Drop column.
- For Columns to drop, choose
URL
andrestaurant_id
. - Choose Add.
[
- Our
rating
feature column has some missing values, so let’s fill in those rows with the average value of this column.- Add a new transform.
- For Transform, choose Impute.
- For Column type, choose Numeric.
- For Input columns, choose the
rating
column. - For Imputing strategy, choose Mean.
- For Output column, enter
rating_avg_filled
. - Choose Add.
- We can drop the
rating
column because we have a new column with filled values. - Because
type_of_food
is categorical in nature, we’ll want to numerically encode it. Let’s encode this feature using the one-hot encoding technique.- Add a new transform.
- For Transform, choose One-hot encode.
- For Input columns, choose
type_of_food
. - For Invalid handling strategy¸ choose Keep.
- For Output style¸ choose Columns.
- For Output column, enter
encoded
. - Choose Add.
Build a model and generate predictions
Now that we have transformed our data, let’s train a numeric ML model to predict the ratings for restaurants.
- Choose Create model.
- For Dataset name, enter a name for the dataset export.
- Choose Export and wait for the transformed data to be exported.
- Choose the Create model link at the bottom left corner of the page.
You can also select the dataset from the Data Wrangler feature on the left of the page.
- Enter a model name.
- Choose Predictive analysis, then choose Create.
- Choose
rating_avg_filled
as the target column.
SageMaker Canvas automatically selects a suitable model type.
- Choose Preview model to ensure there are no data quality issues.
- Choose Quick build to build the model.
The model creation will take approximately 2–15 minutes to complete.
You can view the model status after the model finishes training. Our model has an RSME of 0.422, which means the model often predicts the rating of a restaurant within +/- 0.422 of the actual value, a solid approximation for the rating scale of 1–6.
- Finally, you can generate sample predictions by navigating to the Predict tab.
Clean up
To avoid incurring future charges, delete the resources you created while following this post. SageMaker Canvas bills you for the duration of the session, and we recommend logging out of SageMaker Canvas when you’re not using it. Refer to Logging out of Amazon SageMaker Canvas for more details.
Conclusion
In this post, we discussed how you can use SageMaker Canvas for generative AI and ML with data stored in Amazon DocumentDB. In our example, we showed how an analyst can quickly build a high-quality ML model using a sample restaurant dataset.
We showed the steps to implement the solution, from importing data from Amazon DocumentDB to building an ML model in SageMaker Canvas. The entire process was completed through a visual interface without writing a single line of code.
To start your low-code/no-code ML journey, refer to Amazon SageMaker Canvas.
About the authors
Adeleke Coker is a Global Solutions Architect with AWS. He works with customers globally to provide guidance and technical assistance in deploying production workloads at scale on AWS. In his spare time, he enjoys learning, reading, gaming and watching sport events.
Gururaj S Bayari is a Senior DocumentDB Specialist Solutions Architect at AWS. He enjoys helping customers adopt Amazon’s purpose-built databases. He helps customers design, evaluate, and optimize their internet scale and high performance workloads powered by NoSQL and/or Relational databases.
Tim Pusateri is a Senior Product Manager at AWS where he works on Amazon SageMaker Canvas. His goal is to help customers quickly derive value from AI/ML. Outside of work, he loves to be outdoors, play guitar, see live music, and spend time with family and friends.
Pratik Das is a Product Manager at AWS. He enjoys working with customers looking to build resilient workloads and strong data foundations in the cloud. He brings expertise working with enterprises on modernization, analytical and data transformation initiatives.
Varma Gottumukkala is a Senior Database Specialist Solutions Architect at AWS based out of Dallas Fort Worth. Varma works with the customers on their database strategy and architect their workloads using AWS purpose built databases. Before joining AWS, he worked extensively with relational databases, NOSQL databases and multiple programming languages for the last 22 years.