Using the matrix inverse method
In this recipe, we will use TensorFlow to solve two-dimensional linear regression with the matrix inverse method.
Getting ready
Linear regression can be represented as a set of matrix equations, say

. Here, we are interested in solving the coefficients in matrix x. We have to be careful if our observation matrix (design matrix) A is not square. The solution to solving x can be expressed as

. To show that this is indeed the case, we will generate two-dimensional data, solve it in TensorFlow, and plot the result.
How to do it...
We proceed with the recipe as follows:
- First, we load the necessary libraries, initialize the graph, and create the data. See the following code:
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf sess = tf.Session() x_vals = np.linspace(0, 10, 100) y_vals = x_vals + np.random.normal(0, 1, 100)
- Next, we create the matrices to use in the inverse method. We create the
A
matrix first, which will be a column ofx
data...