Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How should the code below be filled in to recreate figure 4.6 from Introduction to Machine Learning (Alpaydin 4th Edition). Use all 100 datasets to

How should the code below be filled in to recreate figure 4.6 from Introduction to Machine Learning (Alpaydin 4th Edition).

image text in transcribed

Use all 100 datasets to compute the total error, bias error, and variance error functions by using total error equation.

image text in transcribed

Evaluate each of the three error functions with 10 equal spaced values from 0 to 5 (use np.linspace(0,5,10))

To Do: Print the average predictions, Ex [g(x)], at np.linspace(0,5,10) for each of the five polynomial models. Generate and print a DataFrame with 5 rows, one for each order and 4 columns (order, bias error, variance error, total error)

Notes: The average prediction at point x means computing the average value of the predictions of 100 models generated by 100 datasets. Point x should range between np.linspace(0,5,10). For bias error ([|])(())2, [|]=() and [()] is the average over 100 models from the 100 datasets. Then, you can approximate bias error by average over x in np.linspace(0, 5, 10) of ([|][()])2. For variance error, you need to have a nested loops (for each dataset and for x in np.linespace(0, 5, 10)) to get the average variance error. The total error is the sum of bias error and variance error.

## Insert Code # Define any variables or methods that you need here ## End Code def bias_error(avg_pred, x_eval): # For each polynomial order, computes its bias error # returns a list of length 5 five_bias = [] ## Insert Code ## End Code return five_bias def variance_error(avg_pred, models_evals): # For each polynomial order, computes its variance error # returns a list of length 5 five_variance = [] ## Insert Code ## End Code return five_variance # Fit 5 * 100 models, i.e. fit 100 models for each degree in range(1, 6). # The shape of models_list is (5, 100) models_list = [] ## Insert Code ## End Code # create evaluation x data x_eval = np.linspace(0, 5, 10) # Evaluate each of the 5 * 100 models on `x_eval` # The shape of models_evals_list is (5,100,10) which is 5 degree with 100 models and each model predict the 10 x evaluation models_evals_list = [] ## Insert Code ## End Code # For each degree compute the average predictiona at `x_eval` # The shape `ave_preds_list` isis (5,10) avg_preds_list = [] ## Insert Code ## End Code bias_lst = bias_error(avg_preds_list, x_eval) variance_lst = variance_error(avg_preds_list, models_evals_list) total_error = [x + y for x, y in zip(bias_lst, variance_lst)] # show the plot x_points = [1,2,3,4,5] plt.plot(x_points, bias_lst, linestyle='dashed',label = "Bias^2", marker='o', markersize=10) plt.plot(x_points, variance_lst, linestyle='dashed', label = "Variance", marker='o', markersize=10) plt.plot(x_points, total_error, linestyle='solid', label = "Error", marker='o', markersize=10) plt.legend() plt.xlim(0.9, 5.1) plt.xticks(np.linspace(1, 5, 5)) plt.xlabel("Order") plt.ylabel("Error") plt.title("Bias and Variance Trade-off") # Display graph plt.show()

# Error DataFrame pd.set_option("display.precision", 3) error_df = pd.DataFrame({ 'Order': range(1,6), 'Bias Error': bias_lst, 'Variance Error': variance_lst, 'Total Error': total_error }) error_df

# Average predictions pd.set_option("display.precision", 3) pd.DataFrame(avg_preds_list)

Figure 4.6 In the same setting as that of figure 4.5, using one hundred models instead of five, bias, variance, and error for polynomials of order 1 to 5 . Order 1 has the smallest variance. Order 5 has the smallest bias. As the order is increased, bias decreases but variance increases. Order 3 has the minimum error. 6) Ex[(E[rx]g(x))2x]=bias(E[rx]E[g(x)])2+varianceEx[(g(x)Ex[g(x)])2]

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

More Books

Students also viewed these Databases questions

Question

Find y'. y= |x + X (x) (x) X 1 02x+ 2x 1 O 2x + 1/3 Ex 2x +

Answered: 1 week ago