DEV Community

Deploy a Regression model through AWS SageMaker studio using Autopilot

In this article we are going to create a Machine Learning model on AWS SageMaker using Amazon SageMaker Studio and Autopilot to predict house price.

1. Upload dataset to Amazon S3

First, we need to upload the dataset for house pricing. You can download the dataset for train and test, here.

From the search bar in our AWS Management Console, search S3.

Image description

Create a bucket and upload the train dataset.

Image description

Image description

Image description

Image description

Image description

2. Setup SageMaker domain

Now, from the search bar, search for SageMaker Studio.

Image description

We must have a SageMaker domain, so create one. Click on "Create a SageMaker domain".

Image description

Choose Quick setup and type a domain name.

Image description

Below the same page, you must specify a profile name and create a Execution role.

Image description

Select the before role created a click on Submit.

Image description

Once the domain and profile are created, now you can launch SageMaker Studio.

Image description

3. Import the dataset on SageMaker and add some preprocessing steps

From SageMaker Studio, click on Data Wrangler from the left sidebar.

Image description

Click on Import data.

Image description

From Data sources, choose Amazon S3.

Image description

Select the bucket where you uploaded the dataset.

Image description

Select the dataset and then click on Import.

Image description

In the Data types * Transform section, we can add some steps for preprocessing. Click on + Add step.

Image description

In the search bar, type "remove col" and click on the first result.

Image description

Select Drop column in Transform and Id in Columns to drop. Then, click on Add.

Image description

Click on Export and train.

Image description

In the Training tab, click on Export and train.

Image description

4. Create an Autopilot experiment

Type an experiment name, toggle on Auto split data and click on Next.

Image description

Select SalePrice as Target and the rest of the columns as Features. Then, click on Next.

Image description

Choose Auto and click on Next.

Image description

Type the endpoint name, select Regression as problem type and MSE as objective metric. Then, click on Next.

Image description

Review if the configuration details are correct and click on Create experiment.

Image description

Now, wait until the experiment finishes. You can see the best trial at the top. In this case, the experiment made 100 trials.

Image description

In the Job profile tab you can see how much time it took the experiment to finish. In this case, it took 46 minutes.

Image description

Image description

You can see the model details for the best trail. In Trails tab, click on View model details. On this page, there is a chart about the feature importance.

Image description

5. Deploy ML model

Go back to Trials tab, check the best model from the table and click on Deploy model.

Image description

Choose Make real-time predictions and type the endpoint name. Then, click on Deploy.

Image description

Now, wait until the endpoint is deployed. In this case, it took 19 minutes to finish the deployment.

Image description

6. Invoke deployed ML model

In AWS Settings tab you can copy the deployed URL.

In order to invoke the url, we need the Access Key and Secret Key. So, we can create an user and asign the AmazonSageMakerFullAccess permission policy. To do that, follow these steps:

  • Go to IAM Management Console
  • Users
  • Click on Add Users
  • Type de username
  • In Set permissions step, check Use a permissions boundary to control the maximum permissions and search AmazonSageMakerFullAccess in the permissions policies list
  • Review the details for the user and click on Create user

Image description

Once the user is created, from the users list, click on this user. Then, go to Security credentials tab and create an access key. Copy the access key and secret key.

We're going to invoke the url from Postman, but you can do it from other similar software as well.

Select POST as HTTP method. In the Authorization tab, select AWS Signature as type. Then, paste the access key and secret key. Also, type sagemaker as Service Name.

In the Headers tab, change the Content-Type to text/csv.

Image description

In the Body tab, select raw and Text. Paste any record from the test dataset. Don't forget to remove the id and sale price from the record. Now it's ready to invoke the url.

Image description

NOTE: After this lab if you want to delete the created endpoint, visit this page.

Thanks for reading

Thank you very much for reading, I hope you found this article interesting and may be useful in the future. If you have any questions or ideas that you need to discuss, it will be a pleasure to be able to collaborate and exchange knowledge together.

Top comments (0)