0% found this document useful (0 votes)
8 views32 pages

Machine Learning for Purchase Orders

Uploaded by

howrayan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views32 pages

Machine Learning for Purchase Orders

Uploaded by

howrayan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Chapter 2

Should you send a purchase


order to a technical approver?
CPIS 483
Spring 2023 – Third Term
Dr. Ghada Amoudi
Outline
• Identifying a machine learning opportunity
• Identifying what and how much data is required
• Building a machine learning system
• Using machine learning to make decisions

2
Recap
• Karen, the person who works in the purchasing
department.
• Receives requisitions from staff to buy a product or
service.
• For each request, Karen decides which approver
needs to review and approve the order.
• After getting approval, she sends the request to the
supplier.
• Karen 🡪 decision maker
3
Recap
• For some products (computers) Karen needs to
send the request to a technical advisor
• Determines if the specification is suitable for the person
buying the computer.
• So, does Karen need to send this order to a
technical approver, or not?
• This is the decision we’ll work on in this chapter.

4
5
The process
• The approach Karen takes:
• if a product looks like an IT product, she’ll send it to a
technical approver.
• With an exception:
• if it’s something that can be plugged in and used, such as a
mouse or a keyboard, she doesn’t send it for technical
approval.
• Nor does she send it for technical approval if the requester is
from the IT department.

6
The data Features

Target
variable

7
Categorical features Continuous features
Before we begin
To do machine learning:
• The end goal is to be able to submit an order to the
machine learning model and have it return a result that
recommends sending the order to a technical approver
or not.
• We have identified the features to use to make the
decision (the type of product and whether the
requester is from the IT department).
• We have created the labelled historical dataset (the
dataset shown in the previous slide).
8
Training phase
• Recap, what is machine learning model?
• It is a mathematical function that is rewarded for
guessing right and punished for guessing wrong.
• To get more guesses right, the function associates
certain values in each feature with right guesses or
wrong guesses.
• As it works through more and more samples, it gets
better at guessing.
• When it’s run through all the samples 🡪 the model is
trained.
9
Machine learning algorithm
• Is the mathematical function that underlies a
machine learning model.
• Here we will focus on one only, XGBoost, why?
1. works well across a wide range of problems without
significant tuning.
2. doesn’t require a lot of data to provide good results.
3. easy to explain: why it returns a particular prediction in a
certain scenario.
4. high-performing algorithm and the choice for many
participants in machine learning competitions with small
datasets.
10
The steps
• Load and examine the data.
• Get the data into the right shape.
• Create training, validation, and test datasets. 
Train the machine learning model.
• Host the machine learning model.
• Test the model and use it to make decisions.
Note: it is preferable that you complete the tutorials in
Appendices A-D before continuing these slides.

11
Explaining the code

I called my data_backet
drghada-mlforbusiness

12
Explaining the code

13
Setting up the notebook (1/2)
1. Imports: we import the Python libraries
required by the notebook:
• pandas: A Python library used in data science projects.
load pandas as pd. we will preface any use of any
module in the pandas library with pd.
• boto3 and sagemaker: The libraries created by
Amazon to help Python users interact with AWS
resources:
• boto3 is used to interact with S3
• sagemaker is used to interact with SageMaker.
• s3fs makes it easier to use boto3 with S3.
• sklearn: short for scikit-learn, which is a
comprehensive library of machine learning algorithms
that is used widely in both the commercial and
scientific communities. 14
Setting up the notebook (2/2)

2. Ceate a role on SageMaker that allows the


sagemaker library to use the resources it needs
to build and serve the machine learning
application.
• call the sagemaker function get_execution_role.

15
Explaining the code

16
Explaining the code

• shape property of a DataFrame provides information about the


number of rows and the number of columns.
• [Link][0] shows the number of rows
• [Link][1] shows the number of columns
• The value_counts property of the df DataFrame shows the
number of rows in the dataset where the order was sent to
a technical approver.
• It contains a 1 if it was sent for technical approval and a 0 17
if it was not.
Getting the data into the right
shape
• Machine learning models work with numbers
rather than text-based data.
• We’ll use the pandas get_dummies function to
convert all of the text data into numbers

18
Applying get_dummies
Example from
our dataset
Before applying
get_dummies

19
Relevant columns
• As you can imagine applying get_dummies, will create
more columns
• Our sample dataset in the SageMaker Jupyter notebook
goes to 111 columns
• This is not a problem for the machine learning algorithm
• ML can handle datasets with thousands of columns
• BUT: a problem for you because it becomes more difficult to
reason about the data
• Solution: reduce the number of columns to only the
most relevant ones
20
Relevant columns
• A relevant column: a column that contains values
that are correlated to the value you are trying to
predict

21
Relevant columns

After identifying the


most highly
correlated columns,
we need to filter the
encoded_data table
to contain just those
columns.

22
Creating training, validation, and
test datasets
• Train: to train the model.
• Validate: used by the algorithm to determine whether
the algorithm is improving
• Test: to determine how well the algorithm performs

Note: The random_state argument ensures that repeating the


command 23
splits the data in the same way.
Converting the data to CSV
• XGBoosts deals with the data in CSV format
• So, we need to convert it..

• The None argument: indicates that you do not want to save to


a file.
• The header argument indicates whether the column names
will be included in the CSV file or not.
• For the train_data and val_data datasets, don’t include the
column headers (header=False) ML algorithm expect only
numbers.
• For the test_data dataset: include headers, helpful to have
column names
• The index=False argument tells the function to not include a
column with the row numbers. 24
Saving the CSV file to S3
s3, see slide with...open syntax
12,when we indicate the filename
imported s3fs and location

use 'wb' when creating the file to indicate that you


are writing the contents of the file in binary mode
rather than text mode.

25
Training the model (1/4)

The function TrainingInput loads your CSV


data into SageMaker.

The TrainingInput files are called train_input


and val_input.

26
Training the model (2/4)
• The next code listing will:
• Create your model
• Start a server to run your model
• Train the model on the data
• We will learn more about each of these steps in
upcoming chapters.

27
Training the model (3/4)
Define in which
container AWS will
store the model The variable sess stores the
SageMaker session.

Create the
model which
is stored in
the variable
estimator

28
Training the model (4/4)

• The training step takes about 5 minutes to finish.


• Note that we didn’t have to manually configure a server
and install the software to train the ML model.
• The server only runs for about a minute, so you will only
be charged for about a minute of compute time.
• At the time of writing of this book, the m5-large server was
priced at under US$0.10 per hour.
• Once you have stored the model on S3, you can use it
again whenever you like without retraining the model.
More on this in later chapters. 29
Hosting the model
• We will launch another server to host the model.
This is the server that you will use to make
predictions from the trained model.
The type of server you are using, in this
case, an [Link] server. This is a
smaller server than the [Link]

30
Testing the model

31
Summary
• To find machine learning opportunities identify
decision points.
• You learned how to apply ML using AWS SageMaker
and Jupyter notebooks.
• You send data to the machine learning endpoints to
make predictions.
• You can test the predictions by sending the data to
a CSV file for viewing.

32

You might also like