Build, train and deploy a machine learning model using Amazon Sagemaker

Pronay Ghosh
Accredian
Published in
5 min readApr 29, 2022

--

by Pronay Ghosh and Hiren Rupchandani

  • Taking machine learning models from concept to production is often difficult and time-consuming.
  • To train the model, one must first manage enormous amounts of data, then determine the optimal technique for training it.
  • After that, one must manage computing capacity while training it, and then deploy it into a production environment.
  • Amazon SageMaker simplifies the process of creating and deploying machine learning models.
  • SageMaker controls all of the underlying infrastructures to train your model at petabyte size and deploy it to production.
  • Here, in this article, we will learn more about Amazon Sagemaker.
  • We will learn how to create a SageMaker notebook instance.
  • Next, we will see how to prepare the data.
  • Then, we will train the model to learn from the data.
  • At last, we will Deploy our machine learning model.
  • The Bank Marketing Data Set will be used to train the model.

Steps to Build, train and Deploy a Machine Learning model into Amazon SageMaker

1. For data preparation, create an Amazon SageMaker notebook instance:

  • You build the notebook instance in this stage, which you’ll use to download and process your data.
  • You also build an Identity and Access Management (IAM) role that allows Amazon SageMaker to access data in Amazon S3 as part of the setup procedure.
  • Sign in to the Amazon SageMaker interface and choose your desired AWS Region in the top right corner.
  • Choose Notebook instances from the left navigation window, then Create a notebook instance.
  • Fill in the following fields in the Notebook instance setting the box on the Create notebook instance page:
  • Type <Name of the notebook> in the Notebook instance name field.
  • Choose ml.t2.medium as the Notebook instance type.
  • Keep the default selection -> none for elastic inference.
  • Choose to Create a new role in the Permissions and encryption section for the IAM role.
  • Then in the Create an IAM role dialogue box, pick Any S3 bucket and Create role.

2. Data Preparation:

  • Choose Open Jupyter after the status of your SageMaker notebook instance changes to InService.
  • Choose New in Jupyter, and then conda python3.
  • Copy and paste the following code into a new code cell in your Jupyter notebook, then pick Run.
  • To save your data, create an S3 bucket.
  • Choose Run after copying and pasting the following code into the next code box.
  • Load the data into a data frame after downloading it to your SageMaker instance.
  • Choose Run after copying and pasting the above code into the next code box.
  • After that one can perform a train test split with the help of the following command.

Keep Note that in Amazon Sagemaker one should do train test split with the help of np.split() method. The traditional train_test_split doesn’t work here. In order to learn more you can refer to the following block of code.

3. Train the ML Model:

  • Copy and paste the following code into a new code cell in your Jupyter notebook, then pick Run.
  • Set up the Amazon SageMaker session, create an instance of the XGBoost model (an estimator), and define the model’s hyperparameters.
  • Copy and paste the above code into the next code cell and choose Run.
  • Start the training job.

4. Model Deployment:

  • Copy and paste the above code into a new code cell in your Jupyter notebook, then pick Run.
  • Copy the following code into the next code box and pick Run to forecast whether clients in the test data enrolled for the bank product or not.

Conclusion:

  • So far in this article, we covered a high-level overview of how to train, test and deploy a machine learning model using Amazon Sagemaker.
  • In the next article, we will learn about the basics of Amazon Augmented AI.

Final Thoughts and Closing Comments

Final Thoughts and Closing Comments

There are some vital points many people fail to understand while they pursue their Data Science or AI journey. If you are one of them and looking for a way to counterbalance these cons, check out the certification programs provided by INSAID on their website. If you liked this story, I recommend you to go with the Global Certificate in Data Science & AI because this one will cover your foundations, machine learning algorithms, and deep neural networks (basic to advance).

Follow us for more upcoming future articles related to Data Science, Machine Learning, and Artificial Intelligence.

Also, Do give us a Clap👏 if you find this article useful as your encouragement catalyzes inspiration for and helps to create more cool stuff like this.

--

--

Pronay Ghosh
Accredian

Data Scientist at Aidetic | Former Data Science researcher at The International School of AI and Data Science