Question
To add a bias term, we will add a new column to our X matrix that is full of constants. Does it matter what constant
To add a bias term, we will add a new column to our X matrix that is full of constants.
Does it matter what constant term we choose?
The simplest way to do this is to use hstack which takes in a list of matrices and horizontally concatenates them (i.e. adds on new columns -- there exists a vstack that adds new rows). The simpleest way to construct a constant term is to use np.ones which takes in a list with the number of ones to make for each dimension.
e.g. np.ones([4,2]) will make
1 1
1 1
1 1
1 1
#TODO construct an X matrix with a bias term.
X_with_bias = np.zeros(10)
X_validation_with_bias = np.zeros(10)
#TODO replace the np.zeros() with the correct code
B_with_bias = calculate_weights_with_library(X_with_bias,Y)
Yhat_with_bias = calculate_yhat(X_with_bias, B_with_bias)
Yhat_validation_with_bias = calculate_yhat(X_validation_with_bias, B_with_bias)
residuals_with_bias = calculate_residuals(Y, Yhat_with_bias)
residuals_validation_with_bias = calculate_residuals(Y_validation, Yhat_validation_with_bias)
rmse_with_bias = calculate_rmse(residuals_with_bias)
rmse_validation_with_bias = calculate_rmse(residuals_validation_with_bias)
print('RMSE with bias term:',rmse_with_bias)
print('RMSE Validation with bias term:',rmse_validation_with_bias)
plt.plot(Y,residuals_with_bias,'x')
plt.plot(Y_validation,residuals_validation_with_bias,'ro')
plt.show()
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