Get startedGet started for free

Making predictions with matrix multiplication

In later chapters, you will learn to train linear regression models. This process will yield a vector of parameters that can be multiplied by the input data to generate predictions. In this exercise, you will use input data, features, and a target vector, bill, which are taken from a credit card dataset we will use later in the course.

\(features = \begin{bmatrix} 2 & 24 \\ 2 & 26 \\ 2 & 57 \\ 1 & 37 \end{bmatrix}\), \(bill = \begin{bmatrix} 3913 \\ 2682 \\ 8617 \\ 64400 \end{bmatrix}\), \(params = \begin{bmatrix} 1000 \\ 150 \end{bmatrix}\)

The matrix of input data, features, contains two columns: education level and age. The target vector, bill, is the size of the credit card borrower's bill.

Since we have not trained the model, you will enter a guess for the values of the parameter vector, params. You will then use matmul() to perform matrix multiplication of features by params to generate predictions, billpred, which you will compare with bill. Note that we have imported matmul() and constant().

This exercise is part of the course

Introduction to TensorFlow in Python

View Course

Exercise instructions

  • Define features, params, and bill as constants.
  • Compute the predicted value vector, billpred, by multiplying the input data, features, by the parameters, params. Use matrix multiplication, rather than the element-wise product.
  • Define error as the targets, bill, minus the predicted values, billpred.

Hands-on interactive exercise

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

# Define features, params, and bill as constants
features = ____([[2, 24], [2, 26], [2, 57], [1, 37]])
params = ____([[1000], [150]])
bill = ____([[3913], [2682], [8617], [64400]])

# Compute billpred using features and params
billpred = ____

# Compute and print the error
error = ____ - ____
print(error.numpy())
Edit and Run Code