Deploying Your Machine Learning Model to Production in the Cloud – KDnuggets

AWS, or Amazon Web Services, is a cloud computing service used in many businesses for storage, analytics, applications, deployment services, and many others. Its a platform utilizes several services to support business in a serverless way with pay-as-you-go schemes.

Machine learning modeling activity is also one of the activities that AWS supports. With several services, modeling activities can be supported, such as developing the model to making it into production. AWS has shown versatility, which is essential for any business that needs scalability and speed.

This article will discuss deploying a machine learning model in the AWS cloud into production. How could we do that? Lets explore further.

Before you start this tutorial, you need to create an AWS account, as we would need them to access all the AWS services. I assume that the reader would use the free tier to follow this article. Additionally, I assume the reader already knows how to use Python programming language and has basic knowledge of machine learning. Also, we will focus on the model deployment part and will not concentrate on other aspects of data science activity, such as data preprocessing and model evaluation.

With that in mind, we will start our journey of deploying your machine learning model in the AWS Cloud services.

In this tutorial, we will develop a machine-learning model to predict churn from the given data. The training dataset is acquired from Kaggle, which you can download here.

After we have acquired the dataset, we would create an S3 bucket to store the dataset. Search the S3 in the AWS services and make the bucket.

In this article, I named the bucket telecom-churn-dataset and located in Singapore. You can change them if you want, but lets go with this one for now.

After you have finished creating the bucket and uploading the data into your bucket, we will go to the AWS SageMaker service. In this service, we will use the Studio as our working environment. If you have never used the Studio, lets create a domain and user before proceeding further.

First, choose the Domains within the Amazon SageMaker Admin configurations.

In the Domains, you would see a lot of buttons to select. In this screen, select the Create domain button.

Choose the quick setup if you want to speed up the creation process. After its finished, you should see a new domain created in the dashboard. Select the new domain you just created and then click the Add user button.

Next, you should name the user profile according to your preferences. For the execution role, you can leave it on default for now, as its the one that was created during the Domain creation process.

Just click next until the canvas setting. In this section, I turn off several settings that we dont need, such as Time Series Forecasting.

After everything is set, go to the studio selection and select the Open studio button with the user name you just created.

Inside the Studio, navigate to the sidebar that looks like a folder icon and create a new notebook there. We can let them by default, like the image below.

With the new notebook, we would work to create a churn prediction model and deploy the model into API inferences that we can use in production.

First, lets import the necessary package and read the churn data.

Next, we would split the data above into training data and testing data with the following code.

We set the test data to be 30% of the original data. With our data split, we would upload them back into the S3 bucket.

You can see the data inside your S3 bucket, which currently consists of three different datasets.

With our dataset ready, we would now develop a churn prediction model and deploy them. In the AWS, we often use a script training method for machine learning training. Thats why we would develop a script before starting the training.

For the next step, we need to create an additional Python file, which I called train.py, in the same folder.

Inside this file, we would set our model development process to create the churn model. For this tutorial, I would adopt some code from Ram Vegiraju.

First, we would import all the necessary packages for developing the model.

Next, we would use the parser method to control the variable that we can input into our training process. The overall code that we would put in our script to train our model is in the code below.

Lastly, we need to put four different functions that SageMaker requires to make inferences: model_fn, input_fn, output_fn, and predict_fn.

With our script ready, we would run the training process. In the next step, we would pass the script we created above into the SKLearn estimator. This estimator is a Sagemaker object that would handle the entire training process, and we would only need to pass all the parameters similar to the code below.

If the training is successful, you will end up with the following report.

If you want to check the Docker image for the SKLearn training and your model artifact location, you can access them using the following code.

With the model in place, we would then deploy the model into an API endpoint that we can use for prediction. To do that, we can use the following code.

If the deployment is successful, the model endpoint is created, and you can access it to create a prediction. You can also see the endpoint in the Sagemaker dashboard.

You can now make predictions with this endpoint. To do that, you can test the endpoint with the following code.

Congratulation. You have now successfully deployed your model in the AWS Cloud. After you have finished the testing process, dont forget to clean up the endpoint. You can use the following code to do that.

Dont forget to shut down the instance you use and clean up the S3 storage if you dont need it anymore.

For further reading, you can read more about the SKLearn estimator and Batch Transform inferences if you prefer to not have an endpoint model.

AWS Cloud platform is a multi-purpose platform that many companies use to support their business. One of the services often used is for data analytic purposes, especially model production. In this article, we learn to use AWS SageMaker and how to deploy the model into the endpoint.Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and Data tips via social media and writing media.

Visit link:

Deploying Your Machine Learning Model to Production in the Cloud - KDnuggets

Related Posts

Comments are closed.