Question: Use cross - validation to choose a hyperparameter value Suppose you want to only learn decision trees that are limited in depth. This can be
Use crossvalidation to choose a hyperparameter value
Suppose you want to only learn decision trees that are limited in depth. This can be achieved using a maximum depth hyperparameter in the decision tree learning algorithm implemented in sklearn. But how
should you choose this hyperparameter value? Using holdout crossvalidation is one possibility.
To implement holdout crossvalidation, you will first need to randomly partition the training data into two parts. The following code does exactly this.
from sklearn.modelselection import traintestsplit
Xtrain, Xval, ytrain, yval traintestsplitcovtypetraindata'
covtypetrainlabels' testsize randomstate
You will also need to decide on which values to consider for the maximum depth hyperparameter. Lets
fixed this to the following ten values:
Problem Use holdout crossvalidation to pick a value for the maximum depth hyperparameter. You may want to use sklearn.metrics.accuracyscore, and also save some of the intermediate
results for Problem below. What is the chosen hyperparameter value?
Problem Use matplotlib to plot the validation accuracy or validation error rate, if you prefer as a function of the maximum depth hyperparameter value. Label the axes of the plot and give the plot an appropriate title.
Problem Now use the value for the maximum depth hyperparameter chosen in Problem to learn another decision tree, using the entirety of the training data covtypetraindata' and covtypetrainlabels' not just Xtrain and ytrain What is the test error rate of this new learned decision tree?
For Problem the matplotlib Simple Plot example may be helpful if you are new to matplotlib.
all python code and calculations are need
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
