Complete the code to calculate the mean squared error (MSE) between predictions and true values.
mse = ((predictions - true_values) [1] 2).mean()
The mean squared error is calculated by squaring the difference between predictions and true values. The operator for element-wise exponentiation is **.
Complete the code to split data into training and testing sets using scikit-learn.
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=[1], random_state=42)
The test_size parameter expects a float between 0 and 1 representing the proportion of the dataset to include in the test split. 0.2 means 20% test data.
Fix the error in the code to train a linear regression model and make predictions.
from sklearn.linear_model import LinearRegression model = LinearRegression() model.[1](X_train, y_train) predictions = model.predict(X_test)
train instead of fit.predict before training.The method to train a model in scikit-learn is fit. Using train or predict here is incorrect.
Fill both blanks to create a dictionary comprehension that maps model complexity to training error.
errors = {complexity: model_errors[complexity] [1] for complexity in model_errors if model_errors[complexity] [2] 0}The comprehension multiplies errors by 100 to convert to percentage and filters only positive errors using >.
Fill the blanks to calculate bias, variance, and total error from predictions.
bias = (([1] - true_values) ** 2).mean() variance = predictions.var(axis=0).mean() total_error = bias + [2]
Bias is the squared difference between the average prediction and true values. Total error is the sum of bias and variance.
