Editor’s Image
AWS, or Amazon Web Services, is a cloud computing service used in many businesses for storage, analytics, applications, deployment services, and many others. It is a platform that uses various services to support serverless businesses with pay-as-you-go schemes.
The machine learning modeling activity is also one of the activities supported by AWS. With various services, modeling activities can be supported, such as developing the model into production. AWS has demonstrated versatility, which is essential for any business that needs scalability and speed.
This article will discuss deploying a machine learning model to the AWS cloud in production. How could we do that? Let’s explore further.
Before starting this tutorial, you need to create a AWS account, since we would need them to access all AWS services. I assume that the reader will use the free tier to follow this article. Also, I assume that the reader already knows how to use the Python programming language and has a basic understanding of machine learning. Furthermore, we will focus on the model implementation part and will not focus on other aspects of data science activity, such as data preprocessing and model evaluation.
With that in mind, we’ll begin our journey toward deploying your machine learning model on AWS cloud services.
In this tutorial, we will develop a machine learning model to predict churn from the given data. The training data set is acquired from Kaggle, which you can download here.
Once we have acquired the data set, we would create an S3 bucket to store the data set. Find the S3 in AWS services and create the bucket.
Image by author
In this article, I called the repository “telecom churn dataset” and located it in Singapore. You can change them if you want, but let’s go with this one for now.
Once you have finished creating the bucket and loading the data into it, we will go to the AWS SageMaker service. In this service, we will use Studio as our work environment. If you’ve never used Studio, let’s create a domain and user before continuing.
First, choose Domains within the Amazon SageMaker management settings.
Image by author
Under Domains, you will see many buttons to select from. On this screen, select the Create Domain button.
Image by author
Choose quick settings if you want to speed up the creation process. Once 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.
Image by author
Next, you must name the user profile according to your preferences. For the run function, you can leave it default for now as it is the one that was created during the Domain creation process.
Image by author
Simply click Next to the canvas settings. In this section, I disable several settings that we don’t need, such as Time Series Forecasting.
After everything is set up, go to the study selection and select the Open Study button with the username you just created.
Image by author
Inside Studio, navigate to the sidebar that looks like a folder icon and create a new notebook there. We can leave them as default, like in the image below.
Image by author
Using the new notebook, we would work to create a churn prediction model and implement the model into API inferences that we can use in production.
First, let’s import the necessary package and read the abandonment data.
import boto3
import pandas as pd
import sagemaker
sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()
df = pd.read_csv('s3://telecom-churn-dataset/telecom_churn.csv')
Image by author
Next, we would split the above data into training data and test data with the following code.
from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size = 0.3, random_state = 42)
We set the test data to be 30% of the original data. With our data split, we would load it back into the S3 bucket.
bucket="telecom-churn-dataset"
train.to_csv(f's3://{bucket}/telecom_churn_train.csv', index = False)
test.to_csv(f's3://{bucket}/telecom_churn_test.csv', index = False)
You can view the data within your S3 bucket, which currently consists of three different data sets.
Image by author
With our data set ready, we would now develop a churn prediction model and deploy it. At AWS, we often use a scripted training method for machine learning training. That’s why we would develop a script before starting the training.
For the next step, we need to create an additional Python file, which I named train.py, in the same folder.
Image by author
Within this file, we would configure our model development process to create the churn model. For this tutorial, I would adopt some code Ram Vegiraj.
First, we would import all the packages necessary to develop the model.
import argparse
import os
import io
import boto3
import json
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib
Next, we would use the parser method to control the variable that we can input into our training process. The general code that we would include in our script to train our model is in the following code.
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--estimator', type=int, default=10)
parser.add_argument('--sm-model-dir', type=str, default=os.environ.get('SM_MODEL_DIR'))
parser.add_argument('--model_dir', type=str)
parser.add_argument('--train', type=str, default=os.environ.get('SM_CHANNEL_TRAIN'))
args, _ = parser.parse_known_args()
estimator = args.estimator
model_dir = args.model_dir
sm_model_dir = args.sm_model_dir
training_dir = args.train
s3_client = boto3.client('s3')
bucket="telecom-churn-dataset"
obj = s3_client.get_object(Bucket=bucket, Key='telecom_churn_train.csv')
train_data = pd.read_csv(io.BytesIO(obj('Body').read()))
obj = s3_client.get_object(Bucket=bucket, Key='telecom_churn_test.csv')
test_data = pd.read_csv(io.BytesIO(obj('Body').read()))
X_train = train_data.drop('Churn', axis =1)
X_test = test_data.drop('Churn', axis =1)
y_train = train_data('Churn')
y_test = test_data('Churn')
rfc = RandomForestClassifier(n_estimators=estimator)
rfc.fit(X_train, y_train)
y_pred = rfc.predict(X_test)
print('Accuracy Score: ',accuracy_score(y_test, y_pred))
joblib.dump(rfc, os.path.join(args.sm_model_dir, "rfc_model.joblib"))
Finally, we must include four different functions that SageMaker requires to make inferences: model_fn, input_fn, output_fn and predict_fn.
#Deserialized model to load them
def model_fn(model_dir):
model = joblib.load(os.path.join(model_dir, "rfc_model.joblib"))
return model
#The request input of the application
def input_fn(request_body, request_content_type):
if request_content_type == 'application/json':
request_body = json.loads(request_body)
inp_var = request_body('Input')
return inp_var
else:
raise ValueError("This model only supports application/json input")
#The prediction functions
def predict_fn(input_data, model):
return model.predict(input_data)
#The output function
def output_fn(prediction, content_type):
res = int(prediction(0))
resJSON = {'Output': res}
return resJSON
With our script ready, we would run the training process. In the next step, we would pass the script we created earlier to 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 similar parameters to the code below.
from sagemaker.sklearn import SKLearn
sklearn_estimator = SKLearn(entry_point="train.py",
role=role,
instance_count=1,
instance_type="ml.c4.2xlarge",
py_version='py3',
framework_version='0.23-1',
script_mode=True,
hyperparameters={
'estimator': 15})
sklearn_estimator.fit()
If the training is successful, you will get the following report.
Image by author
If you want to check the Docker image for SKLearn training and the artifact location of your model, you can access them using the following code.
model_artifact = sklearn_estimator.model_data
image_uri = sklearn_estimator.image_uri
print(f'The model artifact is saved at: {model_artifact}')
print(f'The image URI is: {image_uri}')
Once the model is deployed, we would deploy it to an API endpoint that we can use for prediction. To do that, we can use the following code.
import time
churn_endpoint_name="churn-rf-model-"+time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime())
churn_predictor=sklearn_estimator.deploy(initial_instance_count=1,instance_type="ml.m5.large",endpoint_name=churn_endpoint_name)
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.
Image by author
Now you can make predictions with this endpoint. To do that, you can test the endpoint with the following code.
client = boto3.client('sagemaker-runtime')
content_type = "application/json"
#replace with your intended input data
request_body = {"Input": ((128,1,1,2.70,1,265.1,110,89.0, 9.87,10.0))}
#replace with your endpoint name
endpoint_name = "churn-rf-model-2023-09-24-12-29-04"
#Data serialization
data = json.loads(json.dumps(request_body))
payload = json.dumps(data)
#Invoke the endpoint
response = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType=content_type,
Body=payload)
result = json.loads(response('Body').read().decode())('Output')
result
Congratulations. You have already successfully implemented your model in the AWS cloud. Once you have finished the testing process, don’t forget to clean the endpoint. You can use the following code to do it.
from sagemaker import Session
sagemaker_session = Session()
sagemaker_session.delete_endpoint(endpoint_name="your-endpoint-name")
Don’t forget to shut down the instance you are using and clean up the S3 storage if you no longer need it.
To read more, you can read more about the SKLearn Estimator and Batch transformation inferences if you prefer not to have an endpoint model.
The AWS Cloud platform is a multipurpose platform that many companies use to support their businesses. One of the services that is frequently used is for data analysis purposes, especially model production. In this article, we will learn how to use AWS SageMaker and how to deploy the model to the endpoint.
Cornellius Yudha Wijaya He is an assistant data science manager and data writer. While working full-time at Allianz Indonesia, she loves sharing Python tips and data through social media and print media.