Introduction to NumPy and Matplotlib
Chapter 8: NumPy recap and exercises
Perform linear algebra
Try practising your NumPy
skills a bit more with the following set of problems!
Challenge #1
Use NumPy
to solve the following equation to find w given X and y:
w = (X^{T} X) X^{T} y
where X^T is the transpose of X.
You are given x
and y
in the code below.
import numpy as np
x = np.array([[1, 2, 3], [2, 4, 5]])
y = np.array([3, 5])
w = ?????
assert np.all(w == np.array([ 767, 1534, 2001]))
import numpy as np
x = np.array([[1, 2, 3], [2, 4, 5]])
y = np.array([3, 5])
w = (x.T @ x) @ x.T @ y
# or use np.matmul() and np.transpose()
w = np.matmul(
np.matmul(
np.matmul(x.transpose(), x),
x.transpose()
),
y
)