Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy...

306
Practical Machine Learning with R @MatthewRenze #Microsoft

Transcript of Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy...

Page 1: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Practical Machine Learning with R

@MatthewRenze#Microsoft

Page 2: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 3: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 4: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 5: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Human

Cat

Dog

Car

Page 6: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Job Postings for Machine Learning

Source: Indeed.com

Page 7: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: Stack Overflow 2017

Average Salary by Job Type (USA)

$108,000

$101,000

$100,000

Page 8: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

70%

60%

40%

30%

20%

10%

0%

50%

SQ

L

Exc

el

Pyth

on

MySQ

LR

Pyth

on

to

ols

gg

plo

t

SQ

L S

erv

er

Tab

leau

Java

Scr

ipt

Matp

lotl

ib

Java

Po

stg

reSQ

L

Ora

cle

D3

Ho

meg

row

n

Hiv

e

Sp

ark

Clo

ud

era

Vis

ual B

asi

c

Mo

ng

oD

B

Had

oo

p

SA

S

C+

+

Sca

la

Po

werP

ivo

t

SQ

Lit

e C

Pig

Red

Sh

ift

Weka

Hb

ase

(EM

R)

Perl

SP

SS

Tera

data

Tool: language, platform, analytics

Sh

are

of

Resp

on

den

ts

Source: O’Reilly 2015 Data Science Salary Survey

Page 9: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 10: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Overview

1. Intro to ML and R

2. Classification

3. Regression

4. Clustering

5. ML in Practice

Page 11: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

How Does This Apply to Me?

Make decisions using data

Make predictions using data

Make recommendations using data

Automate these with code

Page 12: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Conceptual Model

Data PredictionMachine

Learning

𝑓 𝑥

Page 13: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 14: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 15: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 16: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

About Me

Data Science Consultant

EducationB.S. in Computer Science

B.A. in Philosophy

Data Science specializations

CommunityPublic speaker

Pluralsight author

Microsoft MVP

Open source

Page 17: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Schedule

Lectures (10 min)

Demos (10 min)

Labs (20 min)

Breaks (5 min)

Page 18: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Logistics

Pairing for labs is optional

Ask questions if needed

Come and go as needed

Feedback at the end

Page 19: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Labs

Page 20: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Labs

A(Easy)

Page 21: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Labs

A(Easy)

B(Hard)

Page 22: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Labs

A(Easy)

B(Hard)

Page 23: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Workshop URL

http://www.matthewrenze.com/workshops/practical-machine-learning-with-r/

Page 24: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Introduction to Machine Learning

Page 25: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

What is machine learning?

Page 26: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

ArtificialIntelligence

StatisticsMachineLearning

Page 27: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

Page 28: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

Data Function Prediction

Page 29: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

𝑓 𝑥

Page 30: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

𝑓 𝑥

Page 31: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

Cat Not cat

𝑓 𝑥

Page 32: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

Cat

𝑓 𝑥

Not cat

Page 33: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

Cat Is cat?

𝑓 𝑥

Not cat

Page 34: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

Cat Is cat?

𝑓 𝑥

Not cat

Page 35: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

DataPredictionFunction

Cat Is cat? Yes

𝑓 𝑥

Not cat

Page 36: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

What types of machine learning exist?

Page 37: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Types of Machine Learning

Supervised Learning Unsupervised Learning

Page 38: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Types of Machine Learning

Supervised Learning

Page 39: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Types of Machine Learning

Unsupervised Learning

Page 40: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

How does machine learning work?

Page 41: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Data

Page 42: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Data

Training

Test

Page 43: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Training

Algorithm

Data

Training

Test

Page 44: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Training

AlgorithmModel

Data

Training

Test

Page 45: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Training

AlgorithmModel

Data

Training

Test

Page 46: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Training

AlgorithmModel

Data

Training

Test

New Data

Page 47: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Training

AlgorithmModel

Data

Training

Test

New Data

Prediction

Page 48: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Super simplified version of machine learning!

Page 49: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

What can machine learning do?

Page 50: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

1.23

Page 51: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

1.23

Page 52: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

1.23

Page 53: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: YOLO: Real-Time Object Detection

Page 54: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: http://grail.cs.washington.edu/projects/AudioToObama/ Source: Nvidia

Page 55: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 56: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: http://grail.cs.washington.edu/projects/AudioToObama/

Page 57: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: Pouff - Grocery TripSource: Google Deep Mind

Page 58: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: Boston Dynamics

Page 59: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: Boston Dynamics

Page 60: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥 1.23

Page 61: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Disclaimer

Page 62: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Introduction to R

Page 63: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

What is R?

Open source

Language and environment

Numerical and graphical

Cross platform

Page 64: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

What is R?

Active development

Large user community

Modular and extensible

10,000+ extensions

Page 65: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

FREE

Page 66: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

FREE

Page 67: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Source: http://redmonk.com/sogrady/2016/07/20/language-rankings-6-16/

Page 68: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

70%

60%

40%

30%

20%

10%

0%

50%

SQ

L

Exc

el

Pyth

on

MySQ

LR

Pyth

on

to

ols

gg

plo

t

SQ

L S

erv

er

Tab

leau

Java

Scr

ipt

Matp

lotl

ib

Java

Po

stg

reSQ

L

Ora

cle

D3

Ho

meg

row

n

Hiv

e

Sp

ark

Clo

ud

era

Vis

ual B

asi

c

Mo

ng

oD

B

Had

oo

p

SA

S

C+

+

Sca

la

Po

werP

ivo

t

SQ

Lit

e C

Pig

Red

Sh

ift

Weka

Hb

ase

(EM

R)

Perl

SP

SS

Tera

data

Tool: language, platform, analytics

Sh

are

of

Resp

on

den

ts

Source: O’Reilly 2015 Data Science Salary Survey

Page 69: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 70: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 71: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 72: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Demo 1 R Language Basics

Page 73: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 1 R Language Basics

Page 74: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Classification

Page 75: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 76: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 77: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 78: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 79: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 80: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 81: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 82: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

Data Function Category

Page 83: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Classification Algorithms

k-Nearest Neighbors

Decision Tree Classifier

Naïve Bayes Classifier

Support Vector Machine

Neural Network Classifier

x1

x2

Page 84: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Classification Algorithms

?

is sex male?

is age > 9.5?

is family > 2.5?

SurvivedDied

Died

Survived

k-Nearest Neighbors Decision Tree Neural Network

Page 85: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

k-Nearest Neighbors Classifier

Page 86: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 87: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

?

Page 88: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

?

Page 89: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

?

Page 90: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

?

Page 91: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

?

Page 92: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

K-Nearest Neighbors Classifier

Supervised learning

?

Page 93: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

K-Nearest Neighbors Classifier

Supervised learning

Uses class of neighbors ?

Page 94: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

K-Nearest Neighbors Classifier

Supervised learning

Uses class of neighbors

k specifies how many?

Page 95: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

K-Nearest Neighbors Classifier

Supervised learning

Uses class of neighbors

k specifies how many

Simple and easy

?

Page 96: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Decision Tree Classifier

Page 97: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Page 98: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Is count of spam words > 5?

Not Spam?

Page 99: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Is count of spam words > 5?

Is correct-spelling ratio > 50%?

Not Spam

Not Spam

?

Page 100: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Is count of spam words > 5?

Is correct-spelling ratio > 50%?

Is known contact?

SpamNot spam

Not Spam

Not Spam

Page 101: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Count of Spam Words

Co

rrect

Sp

ellin

g R

ati

o

Has count of spam words > 5?

Has

corr

ect

-sp

ellin

g r

ati

o >

50%

?

Page 102: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Decision Tree Classifier

Supervised learning

is sex male?

is age > 9.5?

is family > 2.5?

SurvivedDied

Died

Survived

Page 103: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Decision Tree Classifier

Supervised learning

Tree of decisions

is sex male?

is age > 9.5?

is family > 2.5?

SurvivedDied

Died

Survived

Page 104: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Decision Tree Classifier

Supervised learning

Tree of decisions

Information gain

is sex male?

is age > 9.5?

is family > 2.5?

SurvivedDied

Died

Survived

Page 105: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Decision Tree Classifier

Supervised learning

Tree of decisions

Information gain

Simple and easy

is sex male?

is age > 9.5?

is family > 2.5?

SurvivedDied

Died

Survived

Page 106: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Classifier

Page 107: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 108: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 109: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

inputs neuron outputs

Artificial Neuron𝑥1

𝑥2

𝑥3

𝑦

Page 110: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neuron

Σ

Page 111: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neuron

Σ

Page 112: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neuron

𝜔1

𝜔2

𝜔3

Page 113: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neuron𝜔0

Page 114: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neuron

𝜔1𝜔2

𝜔3

𝜔0

Page 115: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neuron𝑥1

𝑥2

𝑥3

𝑦

𝜔0

𝜔1

𝜔2

𝜔3

𝜑

𝑦𝑘 = 𝜑

𝑗=0

𝑚

𝑤𝑘𝑗𝑥𝑗

Σ

Page 116: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neural Network

Page 117: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neural Network

input outputhidden

Page 118: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neural Network

Forward propagation

Page 119: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neural Network

Backward propagation

Forward propagation

Page 120: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Artificial Neural Network

Page 121: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Classifier

Supervised learning

Page 122: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Classifier

Supervised learning

Neurons in a brain

Page 123: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Classifier

Supervised learning

Neurons in a brain

Weighted connections

Page 124: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Classifier

Supervised learning

Neurons in a brain

Weighted connections

Complex

Page 125: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Real-World Examples

Should we approve this loan?

Will this customer buy from us?

Should we replace this part?

Does this person have cancer?

x1

x2

Page 126: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Iris Data Set

Iris Setosa Iris Versicolor Iris Virginica

Photos by Radomił Binek, Danielle Langlois, and Frank Mayfield

Page 127: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Fisher’s Iris Data

Species Petal Length Petal Width Sepal Length Sepal Width

setosa 1.1 0.1 4.3 3

setosa 1.4 0.2 4.4 2.9

setosa 1.3 0.2 4.4 3

setosa 1.3 0.2 4.4 3.2

setosa 1.3 0.3 4.5 2.3

… … … …

Iris Data Set

Page 128: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Goal: Predict species based on

petal and sepal measurements

Demo 2 - Classification

Page 129: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Insurance Policy Risk Data Set

Insurance Policy Risk

Gender State State Rate Height Weight BMI Age Risk

Male MA 0.01 184 67.8 20.0 77 High

Male VA 0.14 163 89.4 33.6 82 High

Female NY 0.09 170 81.2 28.1 31 Low

Male TN 0.12 175 99.7 32.6 39 Low

Female FL 0.11 184 72.1 21.3 68 High

… … … … … … …

Page 130: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Insurance Policy Rates Data Set

Insurance Policy Rates

Gender State State Rate Height Weight BMI Age Rate

Male MA 0.01 184 67.8 20.0 77 0.33

Male VA 0.14 163 89.4 33.6 82 0.87

Female NY 0.09 170 81.2 28.1 31 0.01

Male TN 0.12 175 99.7 32.6 39 0.02

Female FL 0.11 184 72.1 21.3 68 0.15

… … … … … … …

Page 131: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 2A – Classification (Easy)

Goal: Predict species based on

petal and sepal measurements

Page 132: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 2B – Classification (Hard)

Goal: Predict the risk of

an insurance policy

Page 133: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Regression

Page 134: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 135: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Area

Sale

Pri

ce

Page 136: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Area

Sale

Pri

ce

Page 137: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Sale

Pri

ce

Area

Page 138: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Sale

Pri

ce

Area

Page 139: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Area

Sale

Pri

ce

Page 140: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥 1.23

Data Function Number

Page 141: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Regression Algorithms

Linear Regression

Polynomial Regression

Lasso Regression

ElasticNet Regression

Neural Network Regression

x1

x2

Page 142: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Regression Algorithms

Simple Linear Multiple Linear Neural Network

Page 143: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Simple Linear Regression

Relationship

Page 144: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Simple Linear Regression

Relationship

Linear model

Page 145: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Simple Linear Regression

Relationship

Linear model

y = m · x + b

Page 146: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Simple Linear Regression

Relationship

Linear model

y = m · x + b

Parameters estimated

Page 147: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Multiple Linear Regression

Similar to SLR

Page 148: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Multiple Linear Regression

Similar to SLR

Multiple variables

Page 149: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Multiple Linear Regression

Similar to SLR

Multiple variables

Multiple slopes

Page 150: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Multiple Linear Regression

Similar to SLR

Multiple variables

Multiple slopes

Categorical variables

Page 151: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Regression

Similar to NN classifier

Page 152: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Neural Network Regression

Similar to NN classifier

Numeric output

Page 153: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Real-World Examples

How much profit will we make?

What will the price be tomorrow?

How many units will they buy?

How long until this part fails?

x1

x2

Page 154: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Demo 3 - Regression

Goal: Predict petal width

Page 155: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 3A – Regression (Easy)

Goal: Predict petal width

Page 156: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 3B – Regression (Hard)

Goal: Predict mortality rate

Page 157: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Clustering

Page 158: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 159: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Ag

e

Income

Page 160: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

??

??

Ag

e

Income

Page 161: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

??

??

???

?A

ge

Income

Page 162: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

??

??

???

?A

ge

Income

Page 163: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Income

Ag

e

22

22

111

11

2

Page 164: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

Data Function Group

Page 165: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Clustering Algorithms

K-means

Hierarchical clustering

Expectation maximization

x1

x2

??

??

???

?

Page 166: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

k-Means Clustering

Page 167: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Income

Ag

e

Page 168: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Income

Ag

e

Page 169: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Income

Ag

e

Page 170: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 171: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

k-Means Clustering

Unsupervised learning

Source: Wikipedia

Page 172: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

k-Means Clustering

Unsupervised learning

Specify k (# of clusters)

Source: Wikipedia

Page 173: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

k-Means Clustering

Unsupervised learning

Specify k (# of clusters)

Algorithm finds centers

Source: Wikipedia

Page 174: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

k-Means Clustering

Unsupervised learning

Specify k (# of clusters)

Algorithm finds centers

Random restarts

Source: Wikipedia

Page 175: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Hierarchical Clustering

Page 176: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 177: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 178: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 179: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 180: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 181: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 182: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

a b c d e f

bc de

def

bcdef

abcdef

Page 183: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Hierarchical Clustering

Unsupervised learning

a b c d e f

bc de

def

bcdef

abcdef

Page 184: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Hierarchical Clustering

Unsupervised learning

Tree of connectedness

a b c d e f

bc de

def

bcdef

abcdef

Page 185: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Hierarchical Clustering

Unsupervised learning

Tree of connectedness

Cuts create clusters

a b c d e f

bc de

def

bcdef

abcdef

Page 186: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Real-world Examples

What are our market segments?

How to group our documents?

Which products to recommend?

x1

x2

??

??

???

?

Page 187: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Demo 4 - Clustering

Goal: Group flowers by similarity

Page 188: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 4A – Clustering (Easy)

Goal: Group flowers by similarity

Page 189: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 4B – Clustering (Hard)

Goal: Group insurance policies

Page 190: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Ensemble Learning

Page 191: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 192: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Wisdom of the Crowds

Page 193: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 194: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓2 𝑥

𝑓1 𝑥

𝑓3 𝑥

Ensemble Learning

Page 195: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Types of Ensembles

Same Type of Model Different Types of Models

Page 196: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Ensemble Creation Techniques

Bagging

Boosting

Stacking

𝑓2 𝑥𝑓1 𝑥 𝑓3 𝑥

Page 197: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Ensemble Aggregation Techniques

Averaging

Majority Vote

Weighted Average

Weighted Majority Vote 𝑓2 𝑥𝑓1 𝑥 𝑓3 𝑥

Page 198: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Random Forest Classifier

Page 199: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Random Forest Classifier

Multiple trees

Page 200: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Random Forest Classifier

Multiple trees

Created by bagging

Page 201: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Random Forest Classifier

Multiple trees

Created by bagging

Majority vote

Page 202: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Random Forest Classifier

Multiple trees

Created by bagging

Majority vote

More robust

Page 203: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Why Use Ensemble Learning?

Pros

More accurate

More robust

More stable

Page 204: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Why Use Ensemble Learning?

Pros

More accurate

More robust

More stable

Cons

More complex

More CPU time

More art than science

Page 205: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Ensemble Learning Demo

Page 206: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 207: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Rock

Mine

Time

Am

plitu

de

Am

plitu

de

Page 208: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Sonar

V1 V2 V3 … V58 V59 V60 Class

0.02 0.03 0.04 … 0.00 0.01 0.00 rock

0.04 0.05 0.08 … 0.00 0.01 0.00 mine

0.02 0.05 0.10 … 0.01 0.01 0.01 rock

0.01 0.01 0.06 … 0.00 0.00 0.01 rock

0.07 0.06 0.04 … 0.00 0.01 0.01 mine

0.02 0.04 0.02 … 0.00 0.01 0.00 rock

… … … … … … … …

Page 209: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 210: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Demo 5 – ML in Practice

Goal: Predict rock or mine

Page 211: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 5A – ML in Practice (Easy)

Goal: Predict rock or mine

Page 212: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 5B – ML in Practice (Hard)

Goal: Predict risk class

Page 213: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Learning

Page 214: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓2 𝑥𝑓1 𝑥 𝑓3 𝑥

Deep Learning

Page 215: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

Page 216: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

Page 217: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

Page 218: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

Page 219: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

Page 220: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

John

Jane

Miko

Lee

Page 221: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

input outputhidden 2

Deep Neural Network

hidden 1 hidden 3

Abstractness

Page 222: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Learning Techniques

Fully connected (DNN)

Convolutional (CNN)

Recurrent (RNN)

Generative Adversarial (GAN)

Deep Q Learning (DQN)

Page 223: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Neural Network

Page 224: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Neural Network

Neural network

Page 225: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Neural Network

Neural network

Multiple hidden layers

Page 226: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Neural Network

Neural network

Multiple hidden layers

Non-linear activation

Page 227: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Neural Network

Neural network

Multiple hidden layers

Non-linear activation

Fully connected

Page 228: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Convolutional Neural Networks (CNN)

Page 229: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Convolutional Neural Network (CNN)

Sparse

Page 230: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Convolutional Neural Network (CNN)

Sparse

Convolutions

Page 231: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Convolutional Neural Network (CNN)

Sparse

Convolutions

Filters

Page 232: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Convolutional Neural Network (CNN)

Sparse

Convolutions

Filters

Pooling

Page 233: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

Page 234: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

𝑓 𝑥

Page 235: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Why Use Deep Learning?

Pros

More powerful

More accurate

Data synthesis

Page 236: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Why Use Deep Learning?

Pros

More powerful

More accurate

Data synthesis

Cons

More complex

More training

Less transparent

Page 237: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Learning Demo

Page 238: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 239: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

5 10 15 20 25

51

01

52

02

5

1:28

1:2

8

28 x 28

Page 240: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

MNIST

Label Pixel 0 Pixel 1 Pixel 2 … Pixel 781 Pixel 782 Pixel 783

3 0 0 0 … 0 0 0

5 0 0 0 … 0 0 0

0 0 0 0 … 0 0 0

4 0 0 0 … 0 0 0

1 0 0 0 … 0 0 0

9 0 0 0 … 0 0 0

… … … … … … … …

Page 241: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

128 x

1

ReLU

64 x

1

10 x

1

ReLU

Soft

max

3

28 x 28

Page 242: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Convolution 1

5x5 stride

20 filters tanh

500 x

1

10 x

1

tanh

Soft

max

3

Pool 1

2x2

max

Convolution 2

5x5 stride

50 filters tanh Pool

2x2

max

Page 243: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Demo 6 – Deep Learning

Goal: Predict handwritten digits

with a deep neural network

Page 244: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 6A – Deep Learning (Easy)

Goal: Predict handwritten digits

with a deep neural network

Page 245: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 6B – ML in Practice (Hard)

Goal: Predict handwritten digits

with CNN (LeNet)

Page 246: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 6B – ML in Practice (Hard)

Goal: Predict handwritten digits

with CNN (LeNet)

Page 247: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 6B – ML in Practice (Hard)

Goal: Predict handwritten digits

with CNN (LeNet)

Page 248: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

Page 249: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

NOTE: Add video of RL playing video game

Page 250: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

𝑓 𝑥 ActionState

Page 251: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

EnvironmentAgent

state

action

reward

Page 252: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

WorldCar

position

drive

destination

Page 253: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

Action replay

Page 254: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

Action replay

Optimal policy

Page 255: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

Action replay

Optimal policy

Discounted reward

Page 256: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning

Action replay

Optimal policy

Discounted reward

Markov decision process

Page 257: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Reinforcement Learning Demo

Page 258: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 259: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Grid World

Page 260: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

States

s1

s2 s3

s4

Page 261: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

s1

s2 s3

s4

Actions

Page 262: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

-1

-1 -1

+10

Rewards

Page 263: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

s1

s2 s3

s4

Optimal Policy

Page 264: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

s1

s2 s3

s4

Recap

States: s1, s2, s3, s4

Actions: up, down, left, right

Rewards: s1, s3, s3 = -1;

s4 = 10

Policy: s1 = down

s2 = right

s3 = up

Page 265: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Tic-Tac-Toe

Page 266: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

ML in Practice

Page 267: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

What is the machine learning process?

Page 268: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a question

Page 269: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a question

Prepare the data

Page 270: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a question

Prepare the data

Train the model

Page 271: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a question

Prepare the data

Train the model

Evaluate the

model

Page 272: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a question

Prepare the data

Train the model

Evaluate the

model

Deploy the

model

Page 273: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a question

Prepare the data

Train the model

Evaluate the

model

Deploy the

model

Monitor the

model

Page 274: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Find a

question

Prepare

the data

Train the

model

Evaluate

the

model

Deploy

the

model

Monitor

the

model

Page 275: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Creating accurate and robust models is not easy

Page 276: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Goodness of Fit

Page 277: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Underfit

Goodness of Fit

Page 278: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Underfit Overfit

Goodness of Fit

Page 279: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Underfit Good fit Overfit

Goodness of Fit

Page 280: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Curse of Dimensionality

Page 281: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Curse of Dimensionality

Page 282: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Curse of Dimensionality

Page 283: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Curse of Dimensionality

Page 284: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Movie Break

Page 285: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Demo 8 – ML in Practice

Goal: Predict survivors

of the Titanic

Page 286: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 8A – ML in Practice (Easy)

Goal: Predict survivors

of the Titanic

Page 287: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Lab 8B – ML in Practice (Hard)

Goal: Predict risk in practice

Page 288: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

ML in Production

Page 289: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

How to Deploy to Production

Deploy to web app (Shiny)

Deploy to cloud (Azure ML)

Deploy to server (ML Server)

Deploy to any app (ONNX)

Page 290: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 291: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 292: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Conclusion

Page 293: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

This is just the tip of the iceberg!This is just the tip of the iceberg!

Page 294: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Ensemble Learning

Page 295: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Deep Learning

Page 296: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

EnvironmentAgent

state

action

reward

Reinforcement Learning

Page 297: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Where do we go from here?

Page 298: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Where to Go Next

Data Camp: https://www.datacamp.com

Pluralsight: https://www.pluralsight.com

Coursera: https://www.coursera.org

Page 299: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

www.pluralsight.com/authors/matthew-renze

Pluralsight Courses

Data Science with R

Data Science: The Big Picture

Deep Learning: The Big Picture

Exploratory Data Analysis with R

Data Visualization with R (3-part)

https://www.pluralsight.com/authors/matthew-renze

Page 300: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

www.matthewrenze.com

Page 301: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Feedback

Very important to me!

What did you like?

What could I improve?

Page 302: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Conclusion

1. Intro to ML and R

2. Classification

3. Regression

4. Clustering

5. ML in Practice

Page 303: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 304: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Are you prepared?

Is your organization?

Is our world prepared?

Page 305: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0
Page 306: Practical Machine Learning with R - Matthewrenze · Insurance Policy Risk Data Set Insurance Policy Risk Gender State State Rate Height Weight BMI Age Risk Male MA 0.01 184 67.8 20.0

Contact Info

Matthew Renze

Data Science Consultant

Renze Consulting

Twitter: @matthewrenze

Email: [email protected]

Website: www.matthewrenze.com

Thank You! : )