Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help code this in python Part Three: Implement cart [Graded] In this section, you will implement the function cart , which returns a regression

Please help code this in pythonimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Part Three: Implement cart [Graded] In this section, you will implement the function cart , which returns a regression tree based on the minimum squared loss splitting rule. You should use the function sqsplit to make your splits. Use the provided TreeNode class below to represent your tree. Note that the nature of CART trees implies that every node has exactly 0 or 2 children. Tree Structure We've provided a tree structure for you with distinct leaves and nodes. Leaves have two fields, parent (another node) and prediction (a numerical value). Nodes have six fields: 1. left: node describing left subtree 2. right: node describing right subtree 3. feature: index of feature to cut 4. cut: cutoff value c (c: right) 5. prediction: prediction at this node This should be the average of the labels at this node. In [26]: class TreeNode (object): """Tree class. (You don't need to add any methods or fields here but feel free to if you like. The tests will only reference the fields defined in the constructor below, so be sure to set these correctly.) def init _(self, left, right, feature, cut, prediction): self.left = left self.right right self.feature = feature self.cut = cut self.prediction prediction The following cell contains some examples of trees. In [27]: # The following is a tree that predicts everything as zero ==> prediction 0 # In this case, it has no left or right children (it is a leaf node) ==> left = None, right = None # The tree also do not split at any feature at any value ==> feature = None, cut = None, root = TreeNode (None, None, None, None, 0) # The following that a tree with depth 2 # or a tree with one split # The tree will return a prediction of 1 if an example falls under the left subtree # Otherwise it will return a prediction of 2 # To start, first create two leaf node left_leaf TreeNode (None, None, None, None, 1) right_leaf = TreeNode (None, None, None, None, 2) # Now create the parent or the root # Suppose we split at feature 0 and cut of 1 # and the average prediction is 1.5 root2 = TreeNode (left_leaf, right_leaf, 0, 1, 1.5) # Now root2 is the tree we desired In [ ]: def cart(xTr,yTr): """Builds a CART tree. one split). The maximum tree depth is defined by "maxdepth" (maxdepth=2 means Each example can be weighted with "weights". Args: xTr: yTr: n x d matrix of data n-dimensional vector Returns: tree: root of decision tree n,d = xTr.shape # YOUR CODE HERE raise NotImplementedError() Part Four: Implement evaltree [Graded] Implement the function evaltree , which evaluates a decision tree on a given test data set. In [ ]: def evaltree (root, xTe): "" "Evaluates xTe using decision tree root. Input: root: TreeNode decision tree xTe: n x d matrix of data points Output: pred: n-dimensional vector of predictions # YOUR CODE HERE raise NotImplementedError()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions