Question
Part 8: Random Forest Models In this section, we will create and evaluate several random forest models. Create a markdown cell that displays a level
Part 8: Random Forest Models In this section, we will create and evaluate several random forest models. Create a markdown cell that displays a level 2 header that reads: "Part 8: Random Forest Models". Also add some text briefly describing the purpose of your code in this part. We will now create several random forest models, each with a different value for the max_depth parameter. For each parameter value, we will calculate the training and validation accuracy. We will use the validation scores to select the optimal value for max_depth. Perform the following steps in a single code cell: 1. Create empty lists named rf_train_acc and rf_valid_acc. These lists will store the accuracy scores that we calculate for each model. 2. Loop over the values in depth_range (from Part 7). Every time the loop executes, perform the following steps. a. Use NumPy to set a random seed of 1. This should be done inside the loop. b. Create a decision tree model named temp_forest with max_depth equal to the current value from depth_range that is being considered. Set the parameter n_estimators to 100. c. Fit the model to the training data. d. Calculate the training and validation accuracy for temp_forest, appending the resulting values to the appropriate lists. 3. Use np.argmax to determine the index of the maximum value in rf_valid_acc. Store the result in rf_idx. 4. Use rf_idx and depth_range to find the optimal value for the max_depth hyperparameter. Store the result in rf_opt_depth. 5. Use rf_idx with the lists rf_train_acc and rf_valid_acc to determine the training and validation accuracies for the optimal model found. 6. Display the values found in Steps 4 and 5 with messages as shown below. Add spacing to ensure that the values replacing the xxxx symbols are left-aligned. Round the accuracy scores to 4 decimal places. Optimal value for max_depth: xxxx Training Accuracy for Optimal Model: xxxx Validation Accuracy for Optimal Model: xxxx Create a markdown cell explaining that you will plot the training and validation curves as a function of max_depth. Create a figure with two line plots on the same set of axes. One line plot should plot values of rf_train_acc against depth_range and the other should plot values of rf_valid_acc against depth_range. The x-axis should be labeled "Max Depth" and the y-axis should be labeled "Accuracy". The plot should contain a legend with two items that read "Training" and "Validation".
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started