Download Proj0-AY1617-report.pdf and more Study notes Machine Learning in PDF only on Docsity!
ACTUARIAL STUDENTS’ GUIDE TO
INFORMATION-BASED MACHINE LEARNING
July 17, 2017
Professor Mentor: Dr. Runhuan Feng Graduate Mentor: Siva Karthik Boddapati Students: Pranav Bhardwaj, Tyler Steele, Wayne Tao
Contents
- 1 Introduction
- 1.1 Introduction to Machine Learning
- 1.2 Introduction to Information Based Learning
- 1.3 Entropy
- 1.4 Information Gain
- 1.5 The ID3 Algorithm (Iterative Dichotomizer 3)
- 1.6 Vegetation Dataset
- 2 Gini Index
- 3 Information Gain Ratio
- 4 Continuous Descriptive Features
- 5 Continuous Target Features
- 6 Pruning
- 7 Model Ensembles
- 8 Insurance Data
- 8.1 General Intuition of Real World Applications
- 8.2 Prediction
- 9 Conclusion
- A Notes on Modules
- References
- Glossary
This is an example of a potential ABT from a credit agency. As we can see, there are various
ways in which the descriptive features are represented. Some are numerical, while others are
categorical. Other data types found in ABTs include textual, interval, binary, or ordinal.
The quality of the data is an integral part of creating a successful model. Before descriptive
features are placed into the ABT, they are often analyzed alone by measures of central ten-
dency (mean, median, and mode) and variation (variance, percentiles). Once the descriptive
features are individually analyzed and outliers, missing values, and other miscellaneous irreg-
ularities are accounted for, the descriptive feature is placed in the ABT. After this, relationships
between features in the ABT should be analyzed as well.
Multiple models can be constructed from the variety of patterns that occur between the
descriptive features. Therefore, machine learning is often denoted as an “ill-posed problem”.
This is referring to the idea that it is quite difficult to choose the optimal model from a
machine learning program since a variety of models can potentially fit the data well.
Since fitting the data well is common among many different models, it cannot be used to
select a given model. Different programmers or companies have their own ideas on how an
analysis should go, therefore the way in which a model is selected is by the assumptions or
inductive bias that is made by the selector.
1.2 Introduction to Information Based Learning
The general idea behind information-based machine learning algorithms is to deter-
mine the descriptive features that carry the most information and chronologically test these
features in order to create a decision tree.
Intuitively, this makes sense because to find a credible solution to a problem, we would
first start by asking a question that significantly narrows down our options. From there we
can continue to ask questions that will further narrow our options and finally we can arrive at
a reasonable solution.
Creating a decision tree requires an algorithm, and in this report we will use the ID
algorithm. We will discuss the ID3 algorithm in more detail later, however, the goal of the
algorithm is to create the shallowest decision tree through a process of testing the descriptive
features and choosing the descriptive feature that carries the most information at each stage
in the process.
We would prefer shallower trees because this will allow our program to flow nicely and
obtain our solution fast and effectively. In order to produce shallow trees we need to break
down our descriptive features and use those features that are the most informative. But how
do we break down our descriptive features? We use what are known as impurity measures.
The most common impurity measures that are found in information based learning are
entropy and gini index.
As stated, the goal of the impurity measure is to break down the descriptive features so
that we can learn some information about them. But once we have all of this information
about the descriptive features from the impurity measures, what do we do with it? We need
some way of selecting the feature that holds the most information. We accomplish this task
by using what is known as the selection criteria.
The selection criteria allows us to choose the descriptive feature with the most information
based on the impurity measure. The most common selection criteria are information gain
and information gain ratio.
Once we use our selection criteria to select the first descriptive feature, the first part of
the decision tree is created, and the process starts over again, partitioning the data from this
chosen descriptive feature. This process continues until the decision tree is completed.
Here is an example of a simple decision tree. The goal is to predict if a policyholder will
file a claim within the year given how old he or she is and if he or she was in an accident the
prior year.
1.3 Entropy
In the majority of this report, we will focus our attention on the impurity measure entropy
and the selection criteria information gain. In general, entropy is the amount of uncertainty
within the result of an action or event. For example, if you knew for sure (probability = 1)
our code below:
1 from future import d i v i s i o n 2 import numpy as np 3 import math 4 5 card = np. zeros ( 5 2 ) 6 f o r i in range ( 5 2 ) : 7 card [ i ] = 1/ 8 9 def entropy ( prob ) : 10 sum = 0 11 f o r i in range ( ( prob. s i z e ) ) : 12 sum = sum + prob [ i ] * ( math. log ( prob [ i ]+ 1. e-20 ) /math. log ( 2 ) ) 13 return ( -sum) 14 15 p r i n t ( entropy ( card ) )
output:
The idea behind our code is that we first create an array of 52 zeroes. Next for each
element in the array we assign the value 1 / 52 because that is the associated probability
of choosing the correct card from the deck. Next we move onto our entropy function. The
function takes an input named “prob” which is an array of probabilities. The function begins
by creating a variable named sum and initializing it to 0. Next we use a for loop to iterate over
all the elements in the array called “prob”. With each iteration, we are adding the entropy
of the particular event to sum. Then after iterating through all the events within the set, we
return (-sum) to account for the fact that the entropy equation has a negative on the outside
due to the negative log component. Note that we have a +1.e-20 term inside the function.
This is used to prevent a domain error in the case of prob[i]=0, which results in log(0), an
undefined term.
If we run the code we see that the returned output is 5.7. Keep this value in mind as we
move onto the next example. Now we are still going to choose a card randomly out of the 52
but we are only interested in the suit of that card. The probability of choosing the correct suit
is ¼ since there are 4 suits in a deck of cards. This is a higher probability than choosing one
exact card, and our intuition tells us then that the entropy of this event should be lower than
that of the previous example.
1 from future import d i v i s i o n 2 import numpy as np
3 import math 4 5 s u i t = np. zeros ( 4 ) 6 f o r i in range ( 4 ) : 7 s u i t [ i ] =. 8 9 def entropy ( prob ) : 10 sum = 0 11 f o r i in range ( ( prob. s i z e ) ) : 12 sum = sum + prob [ i ] * ( math. log ( prob [ i ] + 1. e-20) /math. log ( 2 ) ) 13 return ( -sum) 14 15 p r i n t ( entropy ( s u i t ) )
output:
Coding this example is analogous to coding the previous example. We will start with
creating an array of zeros and placing the value ¼ in each element in the array. Then we call
the entropy function with this array of probabilities.
Our output is now 2. This is exactly what we expected! When we choose one exact card
out of a deck of cards, we are very uncertain that we will choose the correct card. However, if
we are interested in just the suit, we are less uncertain that we will choose the correct suit
relative to choosing the correct card. This is why we obtain a value of 2 from our calculation of
finding the suits entropy and a value of 5.7 when analyzing the entropy of choosing a specific
card.
It is important to note that we are not interested in the exact values of the entropies but
rather the relationship between them. We did this example to show that our intuition was
correct in reasoning that choosing a card at random has more entropy (5.7) than choosing a
suit (2.0). Since we are not interested in the exact value, some would reason that it is strange
that we are using such a specific value for our log base (2 in this case). It might make more
sense to just use the natural logarithm. Although we understand this critique, we have chosen
to use log base 2 in our calculations because it is the most commonly used log base in the
literature and we prefer to be consistent with the literature.
There are 22 total instances in partitioning on this descriptive feature, therefore the
weighted probability of Low, Medium, and High would be (5/22), (10/22), and (7/22), respec-
tively. Now, according to the equation, the remainder would be calculated as follows:
Intuitively, this means that after we test this descriptive feature, there is still uncertainty
left in predicting the target values. This uncertainty (or entropy) has a value of 1.981.
Now that we have an understanding of remainder, we can move on to information gain.
In order to calculate information gain, we subtract the entropy of the dataset after testing
the descriptive feature (this is referred to as the remainder) from the entropy of the original
dataset. This makes sense intuitively, as the entropy of the original dataset is the amount of
uncertainty in the original dataset, and the entropy of the dataset after testing the descriptive
feature is the amount of uncertainty left after testing a descriptive feature. So the difference in
entropies, or the amount of uncertainty eliminated, is the information gained. This process
is summarized by the following equation:
IG ( d , D ) = H ( t , D ) − r em ( d , D ).
Source (Kelleher et al. 2015)
IG(d, D) is the information gain from testing a descriptive feature, d, for a dataset, D. H(t,
D) is the entropy of the dataset, D, with respect to the target feature, t. Lastly, rem(d,D) is the
remaining entropy after partitioning the original dataset based on the descriptive feature.
Below is an example of python code finding remainder and information gain. Note,
prob_type(dataset) returns the entropy of a given dataset. We will analyze this function in
detail in the vegetation example.
1 def remainder ( t o t a l s e t , group ) : 2 sum = 0 3 f o r i in range ( len ( group ) ) : 4 sum = sum + ( len ( group [ i ] ) / len ( t o t a l s e t ) ) * prob_type ( group [ i ] ) 5 return sum
The remainder of entropy left in a dataset after it is split into partitions based on the
descriptive feature is a sum of each partitions weighted entropy contribution. We initialize
our variable, sum, to 0. Then we iterate through each partition of the group and add the
entropy of the group multiplied by the fraction (len(group[i])/len(totalset)) which represents
how much of the entire dataset is represented in each partition. We sum up these weighted
entropies and return the value as remaining entropy.
1 def Info ( t o t a l s e t , group ) : 2 return prob_type ( t o t a l s e t ) - remainder ( t o t a l s e t , group )
For information gain, we subtract remaining entropy from original entropy. Here totalset
is referring to the dataset we are analyzing, and group refers to the descriptive feature being
tested from that dataset. Therefore, prob_type(totalset) returns the entropy of the original
dataset and remainder(totalset, group) calculates the entropy remaining after testing the
descriptive feature.
1.5 The ID3 Algorithm (Iterative Dichotomizer 3)
Before jumping into the ID3 algorithm, it is advantageous to examine the glossary section
of the report in order to understand many of the terms that are found in the literature.
The ID3 algorithm is widely-used in practice.=Created by Ross Quinlan, the algorithm’s
primary goal is to create shallow decision trees. The first part of the process is to calculate
the information gain for each possible descriptive feature. This is done using an impurity
measure such as entropy. In an attempt to create the shallowest possible tree, the data set
is partitioned based on the descriptive feature that carries the most information gain. The
tree grows, with a new path being created for each possible value of the descriptive feature.
Along each of these paths, the process repeats itself. The information gain of each remaining
descriptive feature is computed, and the descriptive feature with the highest information
gain is selected to partition the dataset. This process repeats until a leaf node is created. This
happens in one of three ways. The first is the case where all instances in a partition have the
same target value. In this case, we say the partition set is pure and we label this leaf node with
the corresponding target value. The second case is when there are simply no more descriptive
features to test. In this case, a leaf node is labeled with the target value that occurs most along
its path. Lastly, if there are no data instances associated with a path, a leaf node is labeled
with the target value that occurs most often within the entire dataset.
The following steps generalize the intuition of the algorithm:
1. Calculate the information gain for each descriptive feature
2. Partition the dataset on the descriptive feature with the highest information gain
3. Determine if a leaf node should be created along each path of the descriptive feature
4. Repeat steps (1-3) until there is a leaf node along every path
develop a decision tree that is consistent with the data, and provides a prediction for type of
vegetation based on an input of slope, elevation, and stream. Note that the target feature has
three values: chapparal, conifer, and riparian.
1 from future import d i v i s i o n 2 import numpy as np 3 import csv 4 import math 5 6 def entropy ( prob ) : 7 sum = 0 8 f o r i in range ( ( prob. s i z e ) ) : 9 sum = sum + prob [ i ] * ( math. log ( prob [ i ] + 1. e-20) /math. log ( 2 ) ) 10 return ( -sum) 11 12 13 with open ( ’ vegetation. csv ’ ) as c s v f i l e : 14 reader = csv. reader ( c s v f i l e , d el imi ter = ’ , ’ ) 15 f o r row in reader : 16 p r i n t ( row ) 17 vegi = l i s t ( reader ) 18 19 def prob_type ( s e t ) : 20 count_ch = 0 21 count_ri = 0 22 count_co = 0 23 t o t a l = len ( s e t ) 24 f o r i in range ( t o t a l ) : 25 i f s e t [ i ] [ 4 ] == " chapparal " : 26 count_ch = count_ch + 1 27 i f s e t [ i ] [ 4 ] == " r i p a r i a n " : 28 count_ri = count_ri + 1 29 i f s e t [ i ] [ 4 ] == " co ni fe r " : 30 count_co = count_co + 1 31 32 prob_ch = count_ch / ( t o t a l +1. e-20) 33 prob_ri = count_ri / ( t o t a l +1. e-20) 34 prob_co = count_co / ( t o t a l +1. e-20) 35 prob_total = np. array ( [ prob_ch , prob_ri , prob_co ] ) 36 return entropy ( prob_total ) 37 38 p r i n t ’ Total entropy i s ’ , prob_type ( vegi ) 39 p r i n t ’ \n ’
output:
1 Total entropy i s : 1.
The first part of our code lists different modules that we import (division, numpy, csv, and
math). Refer to Appendix A for Notes on Modules. This allows us to use functions that others
have made in our own code, thereby making our process a little easier.
The first function is entropy which we discussed in detail in a previous section. In short,
this function will return the entropy of an array of probabilities. The next block of code is the
process by which we import the data into Python. We will not go into detail here.
The function named prob_type takes one input, set, which is short for dataset. The
goal of this function is to find the probabilities of the target feature associated with the
dataset inputted. We find that the first part of the code is initializing three variables to 0
and determining the length of the set. This will allow us to divide each respective feature by
the total length of the set in order to find the associated probability of getting that feature.
The main block of this function is the for loop, which loops through the dataset, and assigns
the number of instances of a specific target value to a variable. For example, if we find
that when we loop through our dataset and find 2 instances of “conifer”, the value 2 will be
assigned to the variable count_co (which stands for counting the number of instances of
conifer). The function then divides each counting variable by the total length of the set to
obtain the associated probability of obtaining that feature. The final step is formatting these
probabilities in an array so we can input it into our other critical function; entropy.
After developing our main function, we would like to use it to find the total entropy of the
dataset. In line 38, this value is calculated as prob_type(vegi) and printed to the screen as
“Total entropy is 1.55665670746”.
Now that we have found the total entropy of the dataset we need to analyze the descriptive
features and find the information gained from each descriptive feature. In order to do this,
we need to analyze each instance within a descriptive feature and see how they relate to the
target feature.
1 t r u e _ s e t = [ ] 2 f a l s e _ s e t = [ ] 3 4 f o r i in range ( len ( vegi ) ) : 5 i f vegi [ i ] [ 1 ] == "TRUE" : 6 t r u e _ s e t. append ( vegi [ i ] ) 7 i f vegi [ i ] [ 1 ] == "FALSE" : 8 f a l s e _ s e t. append ( vegi [ i ] ) 9
16 p r i n t ( ’ \n ’ ) 17 18 slope = [ ] 19 slope. append ( f l a t _ s e t ) 20 slope. append ( mod_set ) 21 slope. append ( steep_set ) 22 23 low_set = [ ] 24 med_set = [ ] 25 high_set = [ ] 26 highest_set = [ ] 27 28 f o r i in range ( len ( vegi ) ) : 29 i f vegi [ i ] [ 3 ] == "low" : 30 low_set. append ( vegi [ i ] ) 31 i f vegi [ i ] [ 3 ] == "medium" : 32 med_set. append ( vegi [ i ] ) 33 i f vegi [ i ] [ 3 ] == " high " : 34 high_set. append ( vegi [ i ] ) 35 i f vegi [ i ] [ 3 ] == " highest " : 36 highest_set. append ( vegi [ i ] ) 37 38 p r i n t ( ’ P a r t i t i o n Entropy of low in elevation i s ’ , prob_type ( low_set ) ) 39 p r i n t ( ’ P a r t i t i o n Entropy of medium in elevation i s ’ , prob_type ( med_set ) ) 40 p r i n t ( ’ P a r t i t i o n Entropy of high in elevation i s ’ , prob_type ( high_set ) ) 41 p r i n t ( ’ P a r t i t i o n Entropy of highest in elevation i s ’ , prob_type ( highest_set ) ) 42 p r i n t ( ’ \n ’ ) 43 44 elevation = [ ] 45 elevation. append ( low_set ) 46 elevation. append ( med_set ) 47 elevation. append ( high_set ) 48 elevation. append ( highest_set )
output:
1 P a r t i t i o n Entropy of f l a t in slope i s 0. 0 2 P a r t i t i o n Entropy of moderate in slope i s 0. 0 3 P a r t i t i o n Entropy of steep in slope i s 1. 4 5 P a r t i t i o n Entropy of low in elevation i s 0. 0 6 P a r t i t i o n Entropy of medium in elevation i s 1. 7 P a r t i t i o n Entropy of high in elevation i s 0.
8 P a r t i t i o n Entropy of highest in elevation i s 0. 0
Our next step is to determine the remainder of each descriptive feature. Remainder is the
information that remains required to organize the instances into pure sets after we have split
them using the descriptive feature. From our remainder function, we see that it is calculated
as the weighted average of the instance multiplied by the instance’s partitioned entropy.
1 def remainder ( t o t a l s e t , group ) : 2 sum = 0 3 f o r i in range ( len ( group ) ) : 4 sum = sum + ( len ( group [ i ] ) / len ( t o t a l s e t ) ) * prob_type ( group [ i ] ) 5 return sum 6 7 p r i n t ( ’ Remainder of stream i s ’ , remainder ( vegi , stream ) ) 8 p r i n t ( ’ Remainder of slope i s ’ , remainder ( vegi , slope ) ) 9 p r i n t ( ’ Remainder of elevation i s ’ , remainder ( vegi , elevation ) ) 10 p r i n t ( ’ \n ’ ) 11 12 def Info ( t o t a l s e t , group ) : 13 return prob_type ( t o t a l s e t ) - remainder ( t o t a l s e t , group ) 14 15 16 p r i n t ( ’ IG of stream i s ’ , Info ( vegi , stream ) ) 17 p r i n t ( ’ IG of slope i s ’ , Info ( vegi , slope ) ) 18 p r i n t ( ’ IG of elevation i s ’ , Info ( vegi , elevation ) ) 19 p r i n t ( ’ \n ’ )
output:
1 Remainder of stream i s 1. 2 Remainder of slope i s 0. 3 Remainder of elevation i s 0. 4 5 IG of stream i s 0. 6 IG of slope i s 0. 7 IG of elevation i s 0.
Finally we can calculate the information gain of each descriptive feature by subtracting
the remainder of the descriptive feature from the total entropy of the data set.
By printing the information gain values, we observe that elevation has the highest value
and therefore should begin our decision tree. Since elevation is a categorical variable with
four instances, it will have four branches coming out of it. To analyze these branches we
look back at our partitioned entropy for elevation. We notice that the partitioned entropy for
5 med_moderate. append ( t o t a l _ s e t [ i ] ) 6 i f t o t a l _ s e t [ i ] [ 2 ] == " steep " : 7 med_steep. append ( t o t a l _ s e t [ i ] ) 8 9 p r i n t ( ’ P a r t i t i o n Entropy of f l a t at medium l e v e l of e l e v t a t i o n i s ’ , prob_type ( med_flat ) ) 10 p r i n t ( ’ P a r t i t i o n Entropy of moderate at medium l e v e l of e l e v t a t i o n i s ’ , prob_type ( med_moderate ) ) 11 p r i n t ( ’ P a r t i t i o n Entropy of steep at medium l e v e l of e l e v t a t i o n i s ’ , prob_type ( med_steep ) ) 12 p r i n t ( ’ \n ’ ) 13 14 med_slope = [ ] 15 med_slope. append ( med_flat ) 16 med_slope. append ( med_moderate ) 17 med_slope. append ( med_steep )
output:
1 P a r t i t i o n Entropy of f l a t at medium l e v e l of e l e v t a t i o n i s 0. 0 2 P a r t i t i o n Entropy of moderate at medium l e v e l of e l e v t a t i o n i s 0. 0 3 P a r t i t i o n Entropy of steep at medium l e v e l of e l e v t a t i o n i s 1.
Notice that we do not get 0 entropy for all instances of slope when analyzing the medium
elevation. When slope is steep, we have entropy of 1 therefore there is some uncertainty of
the vegetation state if we are in the medium elevation and have a steep slope.
1 p r i n t ( ’ Remainder of stream in medium elevation i s ’ , remainder ( t o t a l _ s e t , med_stream ) ) 2 p r i n t ( ’ IG of stream in medium elevation i s ’ , Info ( t o t a l _ s e t , med_stream ) ) 3 p r i n t ( ’ \n ’ ) 4 5 p r i n t ( ’ Remainder of slope in medium elevation i s ’ , remainder ( t o t a l _ s e t , med_slope ) ) 6 p r i n t ( ’ IG of slope in medium elevation i s ’ , Info ( t o t a l _ s e t , med_slope ) ) 7 p r i n t ( ’ \n ’ )
output:
1 Remainder of stream in medium elevation i s 0. 2 IG of stream in medium elevation i s 1. 3 4 Remainder of slope in medium elevation i s 1. 5 IG of slope in medium elevation i s 0.
After finding partitioned entropy we can find the remainder and information gain in
order to further develop our decision tree. The information gain of stream is greater than the
information gain of slope. This is not a surprise because there was no entropy with either
instance (true or false) in stream when we found their partitioned entropy. Therefore stream
is chosen for the next branch in the tree. Since the remainder is 0, there is no uncertainty left
along this path and two leaf nodes are created; the prediction of riparian is placed along the
path elevation medium –> stream true, and the prediction of chapparal is placed along the
path elevation medium –> stream false.
1 high_true = [ ] 2 h i g h _ f a l s e = [ ] 3 h i g h _ f l a t = [ ] 4 high_moderate = [ ] 5 high_steep = [ ] 6 t o t a l _ s e t = elevation [ 2 ] 7 8 f o r i in range ( len ( t o t a l _ s e t ) ) : 9 i f t o t a l _ s e t [ i ] [ 1 ] == "TRUE" : 10 high_true. append ( t o t a l _ s e t [ i ] ) 11 i f t o t a l _ s e t [ i ] [ 1 ] == "FALSE" : 12 h i g h _ f a l s e. append ( t o t a l _ s e t [ i ] ) 13 14 p r i n t ( ’ P a r t i t i o n Entropy of TRUE at high l e v e l in elevation i s ’ , prob_type ( high_true ) ) 15 p r i n t ( ’ P a r t i t i o n Entropy of FALSE at high l e v e l in elevation i s ’ , prob_type ( h i g h _ f a l s e ) ) 16 p r i n t ( ’ \n ’ ) 17 18 high_stream = [ ] 19 high_stream. append ( high_true ) 20 high_stream. append ( h i g h _ f a l s e ) 21 22 23 f o r i in range ( len ( t o t a l _ s e t ) ) : 24 i f t o t a l _ s e t [ i ] [ 2 ] == " f l a t " : 25 h i g h _ f l a t. append ( t o t a l _ s e t [ i ] ) 26 i f t o t a l _ s e t [ i ] [ 2 ] == "moderate" : 27 high_moderate. append ( t o t a l _ s e t [ i ] ) 28 i f t o t a l _ s e t [ i ] [ 2 ] == " steep " : 29 high_steep. append ( t o t a l _ s e t [ i ] ) 30 31 p r i n t ( ’ P a r t i t i o n Entropy of f l a t at high l e v e l of e l e v t a t i o n i s ’ , prob_type ( h i g h _ f l a t ) ) 32 p r i n t ( ’ P a r t i t i o n Entropy of moderate at high l e v e l of e l e v t a t i o n i s ’ , prob_type (