如何构建一个优秀的决策树(decision tree)?a decision tree has many analogies in real life and turns out, it has influenced a wide area of machine learning, covering both classification and regression. in decision analysis, a decision tree can be used to visually and explicitly represent decisions and decision making.
so the outline of what i’ll be covering in this blog is as follows.
what is a decision tree? advantages and disadvantages of a decision tree creating a decision tree what is a decision tree?
a decision tree is a map of the possible outcomes of a series of related choices. it allows an individual or organization to weigh possible actions against one another based on their costs, probabilities, and benefits.
as the name goes, it uses a tree-like model of decisions.they can be used either to drive informal discussion or to map out an algorithm that predicts the best choice mathematically.
a decision tree typically starts with a single node, which branches into possible outcomes. each of those outcomes leads to additional nodes, which branch off into other possibilities. this gives it a tree-like shape.
there are three different types of nodes: chance nodes, decision nodes, and end nodes. a chance node, represented by a circle, shows the probabilities of certain results. a decision node, represented by a square, shows a decision to be made, and an end node shows the final outcome of a decision path.
advantages & disadvantages of decision trees advantages decision trees generate understandable rules. decision trees perform classification without requiring much computation. decision trees are capable of handling both continuous and categorical variables. decision trees provide a clear indication of which fields are most important for prediction or classification. disadvantages decision trees are less appropriate for estimation tasks where the goal is to predict the value of a continuous attribute. decision trees are prone to errors in classification problems with many class and a relatively small number of training examples. decision trees can be computationally expensive to train. the process of growing a decision tree is computationally expensive. at each node, each candidate splitting field must be sorted before its best split can be found. in some algorithms, combinations of fields are used and a search must be made for optimal combining weights.pruning algorithmscan also be expensive since many candidate sub-trees must be formed and compared. creating a decision tree
let us consider a scenario where a new planet is discovered by a group of astronomers. now the question is whether it could be ‘the next earth?’ the answer to this question will revolutionize the way people live. well, literally!
there is n number of deciding factors which need to be thoroughly researched to take an intelligent decision. these factors can be whether water is present on the planet, what is the temperature, whether the surface is prone to continuous storms, flora and fauna survives the climate or not, etc.
let us create a decision tree to find out whether we have discovered a new habitat.
the habitable temperature falls into the range 0 to 100 celsius.
whether water is present or not?
whether flora and fauna flourishes?
the planet has a stormy surface?
decision tree modeling using r certification training course duration real-life case studies assignments lifetime access explore curriculum
thus, we a have a decision tree with us.
classification rules:
classification rules are the cases in which all the scenarios are taken into consideration and a class variable is assigned to each.
class variable:
each leaf node is assigned a class-variable. a class-variable is the final output which leads to our decision.
let us derive the classification rules from the decision tree created:
1. if temperature is not between 273 to 373k, -> survival difficult
2.if temperature is between 273 to 373k, and water is not present, -> survival difficult
3.if temperature is between 273 to 373k, water is present, and flora and fauna is not present -> survival difficult
4.if temperature is between 273 to 373k, water is present, flora and fauna is present, and a stormy surface is not present -> survival probable
5. if temperature is between 273 to 373k, water is present, flora and fauna is present, and a stormy surface is present -> survival difficult
decision tree
a decision tree has the following constituents :
root node: the factor of ‘temperature’ is considered as the root in this case. internal node: the nodes with one incoming edge and 2 or more outgoing edges. leaf node: this is the terminal node with no out-going edge.
as the decision tree is now constructed, starting from the root-node we check the test condition and assign the control to one of the outgoing edges, and so the condition is again tested and a node is assigned. the decision tree is said to be complete when all the test conditions lead toa leafnode. the leaf node contains the class-labels, which vote in favor or against the decision.
now, you might think why did we start with the ‘temperature’ attribute at the root? if you choose any other attribute, the decision tree constructed will be different.
correct. for a particular set of attributes, there can be numerous different trees created. we need to choose the optimal tree which is done by following an algorithmic approach. we will now see ‘the greedy approach’ to create a perfect decision tree.
“greedy approach is based on the concept of heuristic problem solving by making an optimal local choice at each node. by making these local optimal choices, we reach the approximate optimal solution globally.”
the algorithm can be summarized as :
1. at each stage (node), pick out the best feature as the test condition.
2. now split the node into the possible outcomes (internal nodes).
3. repeat the above steps till all the test conditions have been exhausted into leaf nodes.
when you start to implement the algorithm, the first question is:‘how to pick the starting test condition?’
the answer to this question lies in the values of‘entropy’ and‘information gain’. let us see what are they and how do they impact our decision tree creation.
entropy:entropy in decision tree stands for homogeneity. if the data is completely homogenous, the entropy is 0, else if the data is divided (50-50%) entropy is 1.
information gain:information gain is the decrease/increase in entropy value when the node is split.
an attribute should have the highest information gain to be selected for splitting.based on the computed values of entropy and information gain, we choose the best attribute at any particular step.
let us consider the following data:
there can be n number of decision trees that can be formulated from these set of attributes.
tree creation trial-1 :
here we take up the attribute ‘student’ as the initial test condition.
decision tree modeling using r certification training weekday / weekend batchessee batch details
tree creation trial-2 :
similarly, why to choose‘student’?we can choose‘income’as the test condition.
creating the perfect decision tree with greedy approach
let us follow the ‘greedy approach’ and construct the optimal decision tree.
there are two classes involved:‘yes’i.e. whether the person buys a computer or‘no’i.e. he does not. to calculate entropy and information gain, we are computing the value of probability for each of these 2 classes.
»positive: for ‘buys_computer=yes’ probability will come out to be :
»negative: for ‘buys_computer=no’ probability comes out to be :
entropy in d: we now put calculate the entropy by putting probability values in the formula stated above.
we have already classified the values of entropy, which are:
entropy =0: data is completely homogenous (pure)
entropy =1: data is divided into 50- 50 % (impure)
our value of entropy is0.940, which means our set is almostimpure.
let’s delve deep, to find out the suitable attribute and calculate the information gain.
what is information gain if we split on “age”?
this data represents how many people falling into a specific age bracket, buy and do not buy the product.
for example, for people with age 30 or less, 2 people buy (yes) and 3 people do not buy (no) the product, the info (d) is calculated for these 3 categories of people, that is represented in the last column.
the info (d) for the age attribute is computed by the total of these 3 ranges of age values. now, the question is what is the ‘information gain’ if we split on ‘age’ attribute.
the difference of the total information value (0.940) and the information computed for age attribute (0.694) gives the ‘information gain’.
this is the deciding factor for whether we should split at ‘age’ or any other attribute. similarly, we calculate the ‘information gain’ for the rest of the attributes:
information gain (age) =0.246
information gain (income) =0.029
information gain (student) = 0.151
information gain (credit_rating) =0.048
on comparing these values of gain for all the attributes, we find out that the ‘information gain’ for ‘age’ is the highest. thus, splitting at ‘age’ is a good decision.
similarly, at eachsplit, we compare the information gain to find out whether that attribute should be chosen for split or not.
thus, the optimal tree created looks like :
the classification rules for this tree can be jotted down as:
ifa person’sage is less than 30 and he is not a student, he will not buy the product.
age(<30) ^ student(no) = no
ifa person’sage is less than 30 and he is a student, he will buy the product.
age(<30) ^ student(yes) = yes
ifa person’sage is between 31 and 40, he is most likely to buy.
age(31…40) = yes
ifa person’sage is greater than 40 and has an excellent credit rating, he will not buy.
age(>40) ^ credit_rating(excellent) = no
if a person’s age is greater than 40, with a fair credit rating, he will probably buy.
age(>40) ^ credit_rating(fair) = yes
thus, we achieve the perfect decision tree!!
上一篇:如何用picos构建临床问题?
下一篇:什么是meta回归?