Hello, I have issues getting my Decision tree code working utilizing a Utility class similar to the following. I am having a recursion issue towards the bottom of the learn function, with the self.tree['left'].learn( line and the following line. I need the code to fill out the decision tree and not hit a recursive state. My PYTHON code is as follows:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello, I have issues getting my Decision tree code working utilizing a Utility class similar to the following. I am having a recursion issue towards the bottom of the learn function, with the self.tree['left'].learn( line and the following line. I need the code to fill out the decision tree and not hit a recursive state. My PYTHON code is as follows:

class DecisionTree(object):
   def __init__(self, max_depth):
       # Initializing the tree as an empty dictionary or list, as preferred
       self.tree = {}
       self.max_depth = max_depth
       
    
   def learn(self, X, y, par_node = {}, depth=0):
       k = 0
       label_start = y[0]
       num_labels = len(y)
       for i in range(0, num_labels):
           if y[i] != label_start:
               k = 1
       if k == 0:
           self.tree['class'] = 'child'
           self.tree['label'] = label_start
           return
       
       split_attribute, split_value, X_left, X_right, y_left, y_right = Utility.best_split(self,X, y)
       self.tree['split_attribute'] = split_attribute
       self.tree['split_value'] = split_value
       self.tree['class'] = 'parent'
       self.tree['left'] = DecisionTree(self.max_depth)
       self.tree['right'] = DecisionTree(self.max_depth)
       par_node = self.tree
       
       self.tree['left'].learn(X_left, y_left, par_node, depth+1)
       self.tree['right'].learn(X_right, y_right, par_node, depth+1)
       if depth == self.max_depth:
           return
       #############################################


   def classify(self, record):
       #############################################
       tree_temp = self.tree
       while tree_temp['class'] == 'parent':
           if isinstance(record[tree_temp['split_attribute']],slice):
               if record[tree_temp['split_attribute']] == tree_temp['split_value']:
                   tree_temp = tree_temp['left'].tree
               else:
                   tree_temp = tree_temp['right'].tree
           else:
               if record[tree_temp['split_attribute']] < tree_temp['split_value']:
                   tree_temp = tree_temp['left'].tree
               else:
                   tree_temp = tree_temp['right'].tree
       return tree_temp['label']

       #############################################
 def best_split(self,X,y):
       split_attribute = 0
       split_val = 0
       X_left, X_right, y_left, y_right = [], [], [], []
       info_gain_current = -1
       for i in range(0, len(X[0])):
           split_values = []
           for j in range(len(X)):
               split_values.append(X[j][i])
           if isinstance(X[0][i], slice):
               split_mean = X[0][i]
           else:
               split_mean = np.mean(split_values)
           t_X_left, t_X_right, t_y_left, t_y_right = Utility.partition_classes(self,X, y, i, split_mean)
           info_gain = Utility.information_gain(self,y, [t_y_left, t_y_right])
           if info_gain >= info_gain_current:
               split_attribute = i
               split_val = split_mean
               info_gain_current = info_gain
       X_left = t_X_left
       X_right = t_X_right
       y_left = t_y_left
       y_right = t_y_right
       return (split_attribute, split_val, X_left, X_right, y_left, y_right)

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education