Get startedGet started for free

Measure model fit

Now you will measure the regression performance on both training and testing data with two metrics - root mean squared error and mean absolute error. This is a critical step where you are measuring how "close" are the model predictions compared to actual values.

The numpy library has been loaded as np. The mean_absolute_error and mean_squared_error functions have been loaded. The training and testing target variables are loaded as train_Y and test_Y, and the predicted training and testing values are imported as train_pred_Y and test_pred_Y respectively.

This exercise is part of the course

Machine Learning for Marketing in Python

View Course

Exercise instructions

  • Calculate the root mean squared error on the training data by using the np.sqrt() function.
  • Calculate the mean absolute error on the training data.
  • Calculate the root mean squared error on the testing data.
  • Calculate the mean absolute error on the testing data.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate root mean squared error on training data
rmse_train = np.sqrt(___(train_Y, train_pred_Y))

# Calculate mean absolute error on training data
mae_train = ___(train_Y, train_pred_Y)

# Calculate root mean squared error on testing data
rmse_test = np.sqrt(___(test_Y, test_pred_Y))

# Calculate mean absolute error on testing data
mae_test = ___(test_Y, test_pred_Y)

# Print the performance metrics
print('RMSE train: {}; RMSE test: {}\nMAE train: {}, MAE test: {}'.format(rmse_train, rmse_test, mae_train, mae_test))
Edit and Run Code