1 / 49

Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorithms | Edureka

** Machine Learning with Python : https://www.edureka.co/machine-learning-certification-training ** <br>This Edureka tutorial on Decision Tree Algorithm in Python will take you through the fundamentals of decision tree machine learning algorithm concepts and its demo in Python. Below are the topics covered in this tutorial: <br><br>1. What is Classification? <br>2. Types of Classification <br>3. Classification Use Case <br>4. What is Decision Tree? <br>5. Decision Tree Terminology <br>6. Visualizing a Decision Tree <br>7 Writing a Decision Tree Classifier fro Scratch in Python using CART Algorithm <br><br>Check out our Python Machine Learning Playlist: https://goo.gl/UxjTxm

EdurekaIN
Download Presentation

Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorithms | Edureka

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Machine Learning Using Python

  2. Agenda for Today’s Session ▪ What is Classification? ▪ Types of Classification ▪ Classification Use case ▪ What is Decision Tree? ▪ Terminologies associated to a Decision Tree ▪ Visualizing a Decision Tree ▪ Writing a Decision Tree Classifier form Scratch in Python using CART Algorithm Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  3. What is Classification? Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  4. “Classification is the process of dividing the datasets into different categories or groups by adding label” What is Classification? ▪ Note: It adds the data point to a particular labelled group on the basis of some condition”

  5. Decision Tree ▪ Graphical representation of all the possible solutions to a decision ▪ Decisions are based on some conditions Types of Classification ▪ Decision made can be easily explained Decision Tree Random Forest Naïve Bayes KNN

  6. Random Forest ▪ Builds multiple decision trees and merges them together ▪ More accurate and stable prediction ▪ Random decision forests correct for decision trees' habit Types of Classification of overfitting to their training set ▪ Trained with the “bagging” method Decision Tree Random Forest Naïve Bayes KNN

  7. Naïve Bayes ▪ Classification technique based on Bayes' Theorem ▪ Assumes that the presence of a particular feature in a class is unrelated to the presence of any other feature Types of Classification Decision Tree Random Forest Naïve Bayes KNN

  8. K-Nearest Neighbors ▪ Stores all the available cases and classifies new cases based on a similarity measure ▪ The “K” is KNN algorithm is the nearest neighbors we wish Types of Classification to take vote from. Decision Tree Random Forest Naïve Bayes KNN

  9. What is Decision Tree? Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  10. “A decision tree is a graphical representation of all the possible solutions to a decision based on certain conditions” What is Decision Tree?

  11. Machine Leaning Training Using Python Understanding a Decision Tree Copyright © 2018, edureka and/or its affiliates. All rights reserved.

  12. This is how our dataset looks like! Colour Diameter Label Dataset Green 3 Mango Yellow 3 Mango Red 1 Grape Red 1 Grape Yellow 3 Lemon Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  13. ` Color Diam Label Green Yellow Red Yellow Red 3 3 1 3 1 Mango Lemon Grape Mango Grape Decision Tree Information Gain = 0.37 Gini Impurity = 0 is diameter > = 3? Gini Impurity = 0.44 R R 1 1 Grape Grape G Y Y 3 Mango Mango Lemon 3 3 Information Gain = 0.11 is colour = = Yellow? G 3 Mango 100% Grape Y Y 3 3 Mango Lemon 50% Mango 50% Lemon 100% Mango Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  14. ` Green 3 Mango Is the colour green? Yellow 3 Lemon Is the diameter >=3 What is Decision Tree? Yellow 3 Mango Is the colour yellow TRUE False Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  15. Machine Leaning Training Using Python Decision Tree Terminologies Copyright © 2018, edureka and/or its affiliates. All rights reserved.

  16. Decision Tree Terminology Pruning Branch/SubTree Formed by splitting the tree/node Opposite of Splitting, basically removing unwanted branches from the tree Splitting Splitting is dividing the root node/sub node into different parts on the basis of some condition. Parent/Child Node Root node is the parent node and all the other nodes branched from it is known as child node Root Node It represents the entire population or sample and this further gets divided into two or more homogenous sets. Leaf Node Node cannot be further segregated into further nodes Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  17. Color Diam Label Green Yellow Red Yellow Red 3 3 Mango Lemon Grape Mango Grape 1 3 1 is diameter > = 3? R R 1 1 Grape Grape G Y Y 3 Mango Mango Lemon 3 3 is colour = = Yellow? 100% Grape Y Y 3 3 Mango Lemon 50% Mango 50% Lemon 100% Mango Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  18. Machine Leaning Training Using Python CART Algorithm Copyright © 2018, edureka and/or its affiliates. All rights reserved.

  19. Let’s First Visualize the Decision Tree Which Question to ask and When? Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  20. Let’s First Visualize the Decision Tree Outlook Humidity Windy Yes High Normal Strong Weak Yes No No Yes Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  21. Learn about Decision Tree Which one among them should you pick first? Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  22. Learn about Decision Tree Answer: Determine the attribute that best classifies the training data Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  23. Learn about Decision Tree But How do we choose the best attribute? Or How does a tree decide where to split? Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  24. How Does A Tree Decide Where To Split? Gini Index Information Gain The information gain is the decrease in entropy after a dataset is split on the basis of an attribute. Constructing a decision tree is all about finding attribute that returns the highest information gain The measure of impurity (or purity) used in building decision tree in CART is Gini Index Chi Square Reduction in Variance Reduction in variance is an algorithm used for continuous target variables (regression problems). The split with lower variance is selected as the criteria to split the population It is an algorithm to find out the statistical significance between the differences between sub-nodes and parent node Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  25. Let’s First Understand What is Impurity Impurity = 0 Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  26. Let’s First Understand What is Impurity Impurity ≠ 0 Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  27. ▪ Defines randomness in the data ▪ Entropy is just a metric which measures the impurity or ▪ The first step to solve the problem of a decision tree What is Entropy?

  28. What is Entropy? - P(yes) log2P(yes) − P(no) log2P(no) Entropy(s) = Where, ▪ S is the total sample space, ▪ P(yes) is probability of yes If number of yes = number of no ie P(S) = 0.5  Entropy(s) = 1 If it contains all yes or all no ie P(S) = 1 or 0  Entropy(s) = 0

  29. What is Entropy? E(S) = -P(Yes) log2?(???) When P(Yes) =P(No) = 0.5 ie YES + NO = Total Sample(S) E(S) = 0.5 log20.5− 0.5 log20.5 E(S) = 0.5( log20.5 - log20.5) E(S) = 1

  30. What is Entropy? E(S) = -P(Yes) log2?(???) When P(Yes) = 1 ie YES = Total Sample(S) E(S) = 1 log21 E(S) = 0 E(S) = -P(No) log2?(??) When P(No) = 1 ie No = Total Sample(S) E(S) = 1 log21 E(S) = 0

  31. ▪ Measures the reduction in entropy ▪ Decides which attribute should be selected as the decision node What is Information Gain? If S is our total collection, Information Gain = Entropy(S) – [(Weighted Avg) x Entropy(each feature)]

  32. Machine Leaning Training Using Python Let’s Build Our Decision Tree Copyright © 2018, edureka and/or its affiliates. All rights reserved.

  33. Step 1: Compute the entropy for the Data set Out of 14 instances we have 9 YES and 5 NO D1 So we have the formula, D2 D3 E(S) = -P(Yes) log2?(???)− P(No) log2?(??) D4 E(S) = - (9/14)* log29/14 - (5/14)* log25/14 D5 D6 E(S) = 0.41+0.53 = 0.94 D7 D8 D9 D10 D11 D12 D13 D14 Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  34. Which Node To Select As Root Node? Outlook? Temperature? Humidity? Windy? Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  35. Which Node To Select As Root Node: Outlook Outlook? Rainy Yes Yes Yes No No Overcast Yes Yes Yes Yes Sunny Yes Yes No No No Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  36. Which Node To Select As Root Node: Outlook E(Outlook = Sunny) = -2/5 log22/5 − 3/5 log23/5 = 0.971 E(Outlook = Overcast) = -1 log21 − 0 log20 = 0 E(Outlook = Sunny) = -3/5 log23/5 − 2/5 log22/5 = 0.971 Information from outlook, I(Outlook) = 5/14 x 0.971 + 4/14 x 0 + 5/14 x 0.971 = 0.693 Information gained from outlook, Gain(Outlook) = E(S) – I(Outlook) 0.94 – 0.693 = 0.247 Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  37. Which Node To Select As Root Node: Outlook Windy? True Yes Yes Yes No No No False Yes Yes Yes Yes Yes Yes No No Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  38. Which Node To Select As Root Node: Windy E(Windy = True) = 1 E(Windy = False) = 0.811 Information from windy, I(Windy) = 8/14 x 0.811+ 6/14 x 1 = 0.892 Information gained from outlook, Gain(Windy) = E(S) – I(Windy) 0.94 – 0.892 = 0.048 Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  39. Machine Leaning Training Using Python Similarly We Calculated For Rest Two Copyright © 2018, edureka and/or its affiliates. All rights reserved.

  40. Which Node To Select As Root Node Outlook: Info Gain: 0.940-0.693 Temperature: Info Gain: 0.940-0.911 0.693 0.247 0.911 0.029 Humidity: Info Gain: 0.940-0.788 Windy: Info Gain: 0.940-0.982 0.788 0.892 0.152 0.048 Since Max gain = 0.247, Outlook is our ROOT Node Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  41. Which Node To Select Further? Outlook Overcast Yes ? ? You need to recalculate things Outlook = overcast Contains only yes Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  42. This Is How Your Complete Tree Will Look Like Outlook Overcast Humidity Windy Yes High Normal Strong Weak Yes No No Yes Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  43. Machine Leaning Training Using Python What Should I Do To Play - Pruning Copyright © 2018, edureka and/or its affiliates. All rights reserved.

  44. “A decision tree is a graphical representation of all the possible solutions to a decision based on certain conditions” What is Pruning?

  45. Pruning: Reducing The Complexity Outlook Humidity Windy Yes Normal Weak Yes Yes Copyright © 2018, edureka and/or its affiliates. All rights reserved. Machine Leaning Training Using Python

  46. Machine Leaning Training Using Python Are tree based models better than linear models? Copyright © 2018, edureka and/or its affiliates. All rights reserved.

More Related