Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the function grid _ search, which takes in a training set xTr , yTr , a validation set xVal, yVal and a list of

Implement the function grid_search, which takes in a training set xTr, yTr, a validation set xVal, yVal and a list of tree depth candidates depths. Your job here is to fit a regression tree for each depth candidate on the training set xTr, yTr, evaluate the fitted tree on the validation set xVal, yVal, and then pick the candidate that yields the lowest loss for the validation set.
Implementation Notes:
Use the square_loss function to calculate the training and validation loss for corresponding predictions against true labels yTr and yVal respectively.
In the event of a tie, return the depth that appears first in depths list (np.argmin on the list of validation losses will give you the first index in case of a tie).
def grid_search(xTr, yTr, xVal, yVal, depths):
"""
Calculates the training and validation loss for trees trained on xTr and validated on yTr with a number of depths.
Input:
xTr: nxd training data matrix
yTr: n-dimensional vector of training labels
xVal: mxd validation data matrix
yVal: m-dimensional vector of validation labels
depths: a list of len k of depths
Output:
best_depth, training_losses, validation_losses
best_depth: the depth that yields that lowest validation loss
training_losses: a list of len k. the i-th entry corresponds to the the training loss of the tree of depth=depths[i]
validation_losses: a list of len k. the i-th entry corresponds to the the validation loss of the tree of depth=depths[i]
"""
training_losses =[]
validation_losses =[]
best_depth = None
# YOUR CODE HERE
raise NotImplementedError()
return best_depth, training_losses, validation_losses
We need to replace # YOUR CODE HERE

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

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions

Question

=+(0

Answered: 1 week ago

Question

mple 10. Determine d dx S 0 t dt.

Answered: 1 week ago