Deploy a Regression model through AWS SageMaker studio using Autopilot
Tags: aws, machinelearning, sagemaker, regression
Published: 2023-02-07 03:34:16

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](https://www.kaggle.com/competitions/house-prices-advanced-regression-techniques/data).

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix9nq8ms61ga4maqjg4h.png)

Create a bucket and upload the train dataset.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm6vp9pi8q6j1j0c9bea.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvgqvtj9g8jy8l54tfpz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys5qi73jc9b2tkyvb2xa.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k671ytgvjc06pmmplx9e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k2m2jdtdti6vnu5qljk.png)

## 2. Setup SageMaker domain

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51e6k3das65bm92tgugz.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td6l3wlfmthww2yy7sqy.png)

Choose _Quick setup_ and type a domain name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0b0eaj1pd7r1ximnigq.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53vozjihhaunursv26ld.png)

Select the before role created a click on _Submit_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s4dws2m53p9hjvt92q1.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ti6p3wxu3lzkm6ildz3g.png)

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

From SageMaker Studio, click on _Data Wrangler_ from the left sidebar.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwalxmyu53zhsbnptero.png)

Click on _Import data_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egx3s7b0n6k1o0m35fxl.png)

From Data sources, choose _Amazon S3_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzk2no9r1bn8zk9fr0rw.png)

Select the bucket where you uploaded the dataset.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lt4i1wrcux9wl62duyft.png)

Select the dataset and then click on _Import_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kny4jsb3u717ccha5f4i.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/da5z59xdy4ettd0y1kzv.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0fte6ufj365mnfgxzxh.png)

Select _Drop column_ in _Transform_ and _Id_ in _Columns to drop_. Then, click on _Add_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws7b2iwzzvmd3q52nriv.png)

Click on _Export and train_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1znlrff8yratmv0sre6.png)

In the Training tab, click on _Export and train_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0bd4ooosya1egqy48ap.png)

## 4. Create an Autopilot experiment

Type an experiment name, toggle on _Auto split data_ and click on _Next_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqyr3a9o8vygm3tppri7.png)

Select _SalePrice_ as _Target_ and the rest of the columns as _Features_. Then, click on _Next_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4ysqt1ygtclx901u32w.png)

Choose _Auto_ and click on _Next_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwl1id57vwymygtfgi0f.png)

Type the _endpoint name_, select _Regression_ as _problem type_ and _MSE_ as _objective metric_. Then, click on Next.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omo5u1ndubjw6ktowyhu.png)

Review if the configuration details are correct and click on _Create experiment_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fktvkefy7lxbxki9q2s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mdv2kgxbtckpsmceihf.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwypjuy1jb2niut9ho8u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pt91rxqye3wyfqtvqvkg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjajah6njvneuknn41fh.png)

## 5. Deploy ML model

Go back to _Trials_ tab, check the best model from the table and click on _Deploy model_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljuh55hj36gzp7iymjg6.png)

Choose _Make real-time predictions_ and type the _endpoint name_. Then, click on _Deploy_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ny5qrmniiyhnnk76lid.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypxxcm6hzh7wivpnfg1l.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsmms6pysugeh5jwrvio.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mngwbhnb9ygr7ojepm3d.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enj3yh7jynojx24xryil.png)

**NOTE:** After this lab if you want to delete the created endpoint, visit this [page](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints-delete-resources.html).

**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.