0% found this document useful (0 votes)
3 views14 pages

Machine Learning - Course Notes

The document provides course notes on machine learning, focusing on key principles and techniques such as k-means clustering, decision trees, and naïve Bayes classification. It includes a practical exercise that involves simulating data and applying these algorithms to classify items based on their characteristics. The notes emphasize the importance of understanding the effectiveness of different machine learning methods in solving classification problems.

Uploaded by

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

Machine Learning - Course Notes

The document provides course notes on machine learning, focusing on key principles and techniques such as k-means clustering, decision trees, and naïve Bayes classification. It includes a practical exercise that involves simulating data and applying these algorithms to classify items based on their characteristics. The notes emphasize the importance of understanding the effectiveness of different machine learning methods in solving classification problems.

Uploaded by

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

CS2B‐21: Machine learning – Course Notes Page 1

Machine learning

Course Notes

Syllabus objectives
5.1 Explain and apply elementary principles of Machine Learning

5.1.1 Explain the main branches of machine learning and describe examples of the
types of problems typically addressed by Machine Learning.
5.1.4 Explain in detail and use appropriate software to apply Machine Learning
techniques (eg penalised regression and decision trees) to simple problems.

The Actuarial Education Company © IFE: 2019 Examinations


Page 2 CS2B‐21: Machine learning – Course Notes

0 Introduction

0.1 Contents
There is very little material in the Core Reading for this chapter that relates specifically to R.

Therefore we do not provide detailed Course Notes for this topic. Instead, we have provided an
exam‐style worked example below that covers some areas that we think could be examined in the
practical exam.

0.2 Data requirements


These exercises do not require you to download any data files.

© IFE: 2019 Examinations The Actuarial Education Company


CS2B‐21: Machine learning – Course Notes Page 3

Machine learning – Exercise


In this exercise we will investigate, for a simulated data set consisting of three types of items with
different characteristics:

 how effective the k‐means algorithm is at distinguishing between the three types
 how effective a decision tree is at classifying these items by type
 how effective the naïve Bayes method is at classifying these items by type.

A scientist is analysing a large number of items, which are known to be of one of three types (1, 2
or 3). She has measured two factors for each one in order to help identify which type each item
belongs to. She has found that the values of the factors for each of the types conform to the
distributions shown in the table below. Here U(a , b) indicates the discrete uniform distribution,
which is equally likely to take any of the values a, a  1, , b .

TYPE FACTOR1 FACTOR2


1 U(40,100) U(40,100)
2 U(10,70) U(30,90)
3 U(0,80) U(0,60)

(i) (a) Illustrate the ranges of values of the two factors for each type on a graph.

(b) Comment on your illustration. [8]

(ii) Using a seed value of 19, produce a matrix of 30 simulated items (10 of each type) with
the characteristics shown in the table. [6]

(iii) (a) Apply the k‐means algorithm with 3 clusters to the simulated data in (ii).

(b) Interpret and comment on the results.

(c) Determine the type that this model would predict for an item where both factors
have the value 50. [12]

The Actuarial Education Company © IFE: 2019 Examinations


Page 4 CS2B‐21: Machine learning – Course Notes

As an alternative method for analysing these items, the scientist has also produced the decision
tree shown below.

START

Y
FACTOR 1 < 10 or FACTOR 2 < 30? TYPE 3

Y
FACTOR 1 > 70 or FACTOR 2 > 90? TYPE 1

TYPE 2

(iv) (a) Write a function to allocate an item in your simulated data to one of the three
types based on the decision tree shown above.
(b) Test your function for an item where both factors have the value 50.

(c) Apply your function to your simulated data.

(d) Assess the effectiveness of this decision tree model.

Hint: Calculate the Gini index. [16]

The scientist is also considering using a naïve Bayes approach.

(v) (a) Write down an expression for the likelihood of obtaining a value x from a discrete
uniform distribution that can take the values a , a  1, , b . Your expression
should be valid for all values of x .
(b) Write a function to calculate the likelihood that an item belongs to each of the
three types, based on the prior probabilities p1  0.5 , p2  0.3 and p3  0.2 ,
and assuming that the factors operate independently.

(c) Test your function for an item where both factors have the value 50.

(d) Hence determine the predicted value for each item in the simulated data using
the naïve Bayes method.

(e) Comment on the results in (v)(e). [20]

[Total 62]

© IFE: 2019 Examinations The Actuarial Education Company


CS2B‐21: Machine learning – Course Notes Page 5

Machine learning – Solution


Ex 1 (i)(a) Graph of ranges

We can represent the ranges of values by rectangles on a 2‐dimensional graph of Factor 2 against
Factor 1.

To produce this, we start by setting up a blank plot by specifying NULL for the first two arguments
of the plot command and specifying the correct ranges for the axes, with an appropriate title
and labels for the axes:

plot(NULL,NULL,xlim=c(0,100),ylim=c(0,100),
xlab="Factor 1",ylab="Factor 2",main="Range for Types 1, 2 and 3")

We can then use the polygon function to add rectangles for the three ranges:

polygon(c(40,40,100,100,40),c(100,40,40,100,100),
border="red",lwd=3)
polygon(c(10,10,70,70,10),c(90,30,30,90,90),border="blue",lwd=3)
polygon(c(0,0,60,60,0),c(80,0,0,80,80),border="green",lwd=3)

We’ve drawn each of the three squares starting in the top left‐hand corner, tracing out the shape
anti‐clockwise, but you could start at any of the four corners for these. In fact, R will automatically
join up polygons, so specifying just four points in each case (ie missing out the fifth element) will
produce the same results.

We can also add some text to label the rectangles:

text(c(45,15,5),c(95,85,55),c(1,2,3))

This produces the following illustration:

Range for Types 1, 2 and 3


100

2
80
60
Factor 2

3
40
20
0

0 20 40 60 80 100

Factor 1

The Actuarial Education Company © IFE: 2019 Examinations


Page 6 CS2B‐21: Machine learning – Course Notes

(i)(b) Comment

We can see that, broadly speaking, Type 1 tends to have the highest values for both factors while
Type 3 has the lowest, with Type 2 in between.

However, there is considerable overlap. For example, the point (50,50) could belong to any of the
three types. So it will be difficult for the algorithms to distinguish conclusively between the types.

(ii) Simulate 30 values

We start by setting the seed value specified in the question:

[Link](19)

One way to simulate the 10 items of type 1 with the correct distributions for the x and y values
would be:

type1=matrix(c(1:10,sample(40:100,10,replace=TRUE),
sample(40:100,10,replace=TRUE),rep(1,10)),ncol=4)
type1

[,1] [,2] [,3] [,4]


[1,] 1 47 64 1
[2,] 2 69 67 1
[3,] 3 79 88 1
[4,] 4 44 96 1
[5,] 5 62 53 1
[6,] 6 53 95 1
[7,] 7 57 64 1
[8,] 8 74 74 1
[9,] 9 91 66 1
[10,] 10 84 91 1

This works by first producing a vector of 40 values consisting of:

 the numbers 1, 2, 3, …, 10
 10 random numbers in the range 40 to 100, sampled with replacement
 another 10 random numbers in the range 40 to 100, sampled with replacement
 the number 1 repeated 10 times.

These 40 values are then converted to a matrix with 4 columns. The default for the matrix
function is to enter the values into a matrix working down the columns, rather than going across
the rows. So the numbers 1 to 10 will appear in the first column etc.

As an alternative to the rep(1,10), we could add the type afterwards using the cbind function,
eg for type 1:

[Link](19)

type1=matrix(c(1:10,sample(40:100,10,TRUE),
sample(40:100,10,TRUE)),ncol=3)
type1=cbind(type1,1)

Note that we’ve set the seed back to its original value to make sure we get the same numbers.

© IFE: 2019 Examinations The Actuarial Education Company


CS2B‐21: Machine learning – Course Notes Page 7

We’ve also dropped the word ‘replace=’ in the sample function and just written TRUE for the
third argument.

Similarly, for the other two types:

type2=matrix(c(11:20,sample(10:70,10,TRUE),
sample(30:90,10,TRUE)),ncol=3)
type2=cbind(type2,2)

type3=matrix(c(21:30,sample(0:80,10,TRUE),
sample(0:60,10,TRUE)),ncol=3)
type3=cbind(type3,3)

We can now combine the three sets of items into one array using the rbind function and name
the columns using the colnames function to produce a neat table:

simData=rbind(type1,type2,type3)
colnames(simData)=c("Number","Factor1","Factor2","Type")
simData

Number Factor1 Factor2 Type


[1,] 1 47 64 1
[2,] 2 69 67 1
[3,] 3 79 88 1
[4,] 4 44 96 1
[5,] 5 62 53 1
[6,] 6 53 95 1
[7,] 7 57 64 1
[8,] 8 74 74 1
[9,] 9 91 66 1
[10,] 10 84 91 1
[11,] 11 52 54 2
[12,] 12 68 68 2
[13,] 13 59 47 2
[14,] 14 62 78 2
[15,] 15 54 86 2
[16,] 16 42 78 2
[17,] 17 35 72 2
[18,] 18 41 72 2
[19,] 19 55 44 2
[20,] 20 39 43 2
[21,] 21 74 42 3
[22,] 22 27 43 3
[23,] 23 12 27 3
[24,] 24 40 17 3
[25,] 25 40 59 3
[26,] 26 53 8 3
[27,] 27 69 3 3
[28,] 28 15 53 3
[29,] 29 12 36 3
[30,] 30 9 29 3

The Actuarial Education Company © IFE: 2019 Examinations


Page 8 CS2B‐21: Machine learning – Course Notes

(iii)(a) k‐means

The k‐means algorithm is very easy to apply using the kmeans function (which is included in the
basic R commands). We just need to specify the data for each variable and the number of clusters
to look for:

model1=kmeans(simData[,2:3],3)
model1

We’ve given the output the name model1 so that we can use the values from the output in
subsequent calculations.

This gives the following results:

K-means clustering with 3 clusters of sizes 9, 11, 10

Cluster means:
Factor1 Factor2
1 30.66667 28.77778
2 51.27273 59.00000
3 67.80000 80.90000

Clustering vector:
[1] 2 3 3 3 2 3 2 3 3 3 2 3 2 3 3 2 2 2 2 1 2 1 1 1 2 1 1 1 1 1

Within cluster sum of squares by cluster:


[1] 5831.556 2808.182 3218.500
(between_SS / total_SS = 62.1 %)

(iii)(b) Interpretation and comment

The results tell us that the algorithm has allocated the 30 simulated items to 3 clusters.

It also shows the number of items in each cluster and the coordinates of the centroids (which R
calls ‘cluster means’).

Looking at the centroids, it appears that the clusters R has identified are as follows:

 Cluster 1 corresponds to Type 3 (which tend to have the lowest data values)
 Cluster 2 corresponds to Type 2 (which tend to have medium data values)
 Cluster 3 corresponds to Type 1 (which tend to have the highest data values).

In other words, R has classified them in the reverse order. This means that we can convert the
cluster numbers in the k‐means result to our three types by subtracting from 4:

4-model1$cluster

[1] 2 1 1 1 2 1 2 1 1 1 2 1 2 1 1 2 2 2 2 3 2 3 3 3 2 3 3 3 3 3

The actual types were:

simData[,4]

[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

© IFE: 2019 Examinations The Actuarial Education Company


CS2B‐21: Machine learning – Course Notes Page 9

R tells us in the kmeans results that this algorithm has divided the items roughly equally between
the 3 clusters, with clusters of size 9, 11 and 10.

If we compare the two rows of numbers above, we find that the classifications are correct for 21
out of the 30 items. Considering the amount of overlap between the three types, this is not too
bad.

If you wanted to automate this counting exercise for a larger sample, you could use the following
R code, which tells us in this case that 21 were correct, corresponding to an accuracy of 70%:

correct=length(which(4-model1$cluster==simData[,4]))
c(correct,correct/length(simData[,4]))

[1] 21.0 0.7

R also tells us that, using a measure based on the sum of squares, this model has an effectiveness
of 62.1%.

(iii)(c) Prediction

We can work out the distances between the point (50,50) and each of the three centroids:

sqrt((model1$centers[,1]-50)^2+(model1$centers[,2]-50)^2)

1 2 3
28.708196 9.089545 35.660202

So the point (50,50) is nearest to Cluster 2 (which has the smallest distance of 9.089545) and this
corresponds to Type 2.

(iv)(a) Function

The rows of our matrix of simulated data contain 4 elements. So we can use the following
function (using the | operator for ‘or’) to predict the type using the decision tree:

f=function(x){
ifelse(x[2]<10|x[3]<30,3,
ifelse(x[2]>70|x[3]>90,1,2))}

Here x[2] and x[3] correspond to the second and third entries in the row, ie the values of
FACTOR1 and FACTOR2.

(iv)(b) Prediction

We can test this function for an item with the factor values (50,50):

f(c(NA,50,50,NA))

[1] 2

The values of the first and last arguments aren’t used by the function, so we’ve just set these to
NA (not available). This also acts as a check that we haven’t accidentally used one of these
arguments in our function definition (which would then give the answer NA).

We can see that the answer to both questions in the tree would be NO, leading to a prediction of
Type 2. So our function has given the correct answer.

The Actuarial Education Company © IFE: 2019 Examinations


Page 10 CS2B‐21: Machine learning – Course Notes

(iv)(c) Predictions for the simulated data

We can now apply our function to the rows in our matrix of simulated data using the apply
function:

model2=apply(simData,1,f)
model2

[1] 2 2 1 1 2 1 2 1 1 1 2 2 2 2 2 2 2 2 2 2 1 2 3 3 2 3 3 2 2 3

The second argument of 1 in this function tells R to apply the function across each row of the
data, rather than down each column (which it would do if you set the second argument to 2).

(iv)(d) Effectiveness

We can compare the predicted types with the actual types:

model2 #Predicted

[1] 2 2 1 1 2 1 2 1 1 1 2 2 2 2 2 2 2 2 2 2 1 2 3 3 2 3 3 2 2 3

simData[,4] #Actual

[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

We can see that:

 6 of the Type 1 items were correctly classified as Type 1, but 4 were incorrectly classified
as Type 2
 all 10 of the Type 2 items were correctly classified as Type 2
 5 of the Type 3 items were correctly classified as Type 3, but 1 was incorrectly classified as
Type 1, and 4 were incorrectly classified as Type 2.

To quantify the accuracy, we can calculate the Gini index (as suggested in the hint). To do this, we
need to look at the mix of items that ended up in each of the three boxes (final nodes) for Type 1,
Type 2 and Type 3 in the diagram, and determine how ‘pure’ the contents of each box is.

If we look at how each item was classified above, we can see that the items ending up in each of
the three boxes were as follows:

Box 1’s 2’s 3’s Total

Type 1 111111  p1  67  3  p3  17  7

Type 2 1111  p1  184  2222222222  p2  1018  3333  p3  184  18

Type 3 33333  p3  55  5

Total 30

The fractions p1 , p2 , p3 show the proportions of each type that are present.

© IFE: 2019 Examinations The Actuarial Education Company


CS2B‐21: Machine learning – Course Notes Page 11

So the Gini index for the Type 1 node is:

 
2 2 12
1  p12  p22  p32  1  6  02  1 
7 7
  0.2449 
49

The Gini index for the Type 2 node is:

     
2 2 2 16
1  4  10  4 
18 18 18
  0.5926 
27

If this node had equal numbers of the three types, the Gini index would be:

     
2 2 2 2
1  6  6  6    0.6667 
18 18 18 3
So 0.5926 is quite a high value, indicating that this node is quite ‘impure’.

  
2 2 2 1
With n different types, the maximum value of the Gini index is 1  1  1    1  1  .
n n n n

Node 3, on the other hand, is ‘pure’, as it contains only 3’s, and so has a Gini index of 0.

You can check this by calculating:

5
2
1 5  0

The Gini index for the whole tree can then be calculated as a weighted average over all the items:

7 12 18 16 5 26
G     0    0.4127 
30 49 30 27 30 63

In this example (with 3 types of items), the Gini index can take values in the range  0, 2  , with
 3
values close to 0 indicating a very effective decision tree algorithm. The overall value of 0.4127 is
not particularly good. This is mainly because the Type 2 node contains a mixture of all three
types.

The Actuarial Education Company © IFE: 2019 Examinations


Page 12 CS2B‐21: Machine learning – Course Notes

(v)(a) Likelihood

There are b  a  1 equally likely outcomes. Each of these has the same likelihood, but all other
values have 0 likelihood. So the likelihood of the value x is:

 1
 if x  a , a  1, , b
L(x)   b  a  1
 0 otherwise

(v)(b) Function

We can set up separate functions to calculate the likelihood for each type:

f1=function(x){
ifelse(x[2]<40,0,1/(100-40+1))*ifelse(x[3]<40,0,1/(100-40+1))}

f2=function(x){
ifelse(x[2]<10|x[2]>70,0,1/(70-10+1))*
ifelse(x[3]<30|x[3]>90,0,1/(90-30+1))}

f3=function(x){
ifelse(x[2]>80,0,1/(80-0+1))*ifelse(x[3]>60,0,1/(60-0+1))}

Remember that the naïve Bayes method assumes that the factors operate independently. So we
can just multiply the probabilities for the two factors together.

We can now define a function that applies the prior probabilities for the three types to the
likelihoods we’ve just specified:

flike=function(x){
flike1=0.5*f1(x);flike2=0.3*f2(x);flike3=0.2*f3(x);
c(flike1,flike2,flike3)}

(v)(c) Test

If we apply this to the values (50,50), we get:

flike(c(NA,50,50,NA))

[1] 1.343725e-04 8.062349e-05 4.047764e-05

We can confirm these figures are correct using a calculator.

So, in this case, the prediction would be Type 1, as this has the highest posterior probability.

© IFE: 2019 Examinations The Actuarial Education Company


CS2B‐21: Machine learning – Course Notes Page 13

(v)(d) Predicted values using naïve Bayes method

We can automate the decision process by defining a function that determines which of the three
types gives the maximum posterior probability:

fmax=function(x){
flike1=0.5*f1(x);flike2=0.3*f2(x);flike3=0.2*f3(x);
m=max(flike1,flike2,flike3);
ifelse(flike1==m,1,ifelse(flike2==m,2,3))}

model3=apply(simData,1,fmax)

model3 #Predicted

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 2 3 3 1 3 3 2 2 3

simData[,4] #Actual

[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

A slicker alternative to this function would be:

fmax2=function(x){
posterior=sum(c(0.5,0.3,0.2)*c(f1(x),f2(x),f3(x)));
m=max(posterior);
which(posterior==m)}

This form is neater and would be easier to generalise if we had more variables, but it gives exactly
the same result:

model3=apply(simData,1,fmax2)
model3

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 2 3 3 1 3 3 2 2 3

In both of these functions we’ve ignored the possibility of a tie, where two or more of the types
have exactly the same posterior likelihood.

(v)(e) Comment

We can see that this method has:

 correctly predicted the first 10 items as Type 1


 correctly predicted 2 of the next 10 items as Type 2 but incorrectly predicted 8 as Type 1
 correctly predicted 5 of the last 10 items as Type 3 but incorrectly predicted 2 as Type 1
and 3 as Type 2.

(correct=length(which(model3==simData[,4])))

[1] 17

Overall it has predicted 17 out of 30 correctly, ie 56.7%.

This is not as good as the k‐means method or the decision tree, which both predicted 21 out of
the 30 items correctly.

The Actuarial Education Company © IFE: 2019 Examinations


Page 14 CS2B‐21: Machine learning – Course Notes

1 Core reading
The Core Reading for this chapter contains a few specific references to R.

Random number seeds


To ensure reproducibility in stochastic models in R, use the same numerical seed in the
function [Link]().

k‐means algorithm
R has several machine learning packages that will achieve k-means clustering. One simple
command is kmeans.

In this unit we will only use commands that are included in the basic version of R (which includes
the kmeans function), so you do not need to download any packages.

Advanced machine learning packages


In R, there are a wide range of packages which will perform machine learning techniques.
This range changes over time. See for example:

[Link]

for an overview.

© IFE: 2019 Examinations The Actuarial Education Company

You might also like