In [ ]:

%matplotlib notebook
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt




In [ ]:

t1a, t1b, t2a, t2b = -1, 3, -1.5, 1.5

t1s = np.linspace(t1a, t1b, 500)
t2s = np.linspace(t2a, t2b, 500)

# t1, t2 are both (500, 500) array
t1, t2 = np.meshgrid(t1s, t2s)

# it is (250000, 2) array
# from bottom left, to top right
T = np.c_[t1.ravel(), t2.ravel()]

Xr = np.array([[-1, 1], [-0.3, -1], [1, 0.1]])
Xr

yr = 2 * Xr[:, :1] + 0.5 * Xr[:, 1:]
yr



$X_r$ are the samples, $y_r$ are the outputs. The model is $y_r = X_r \cdot (2, 0.5)^T$

$$X_r = \begin{pmatrix} -1 & 1 \\ -0.3 & -1 \\ 1 & 0.1 \\ \end{pmatrix} \\ y_r = \begin{pmatrix} -1 & 1 \\ -0.3 & -1 \\ 1 & 0.1 \\ \end{pmatrix} \cdot \begin{pmatrix} 2 \\ 0.5 \end{pmatrix} = \begin{pmatrix} -1.5 \\ -1.1 \\ -2.05 \end{pmatrix}$$

### Now define the cost function, just MSE

• It's the MSE on each mesh grid.


In [ ]:

(T.dot(Xr.T) - yr.T)




In [ ]:

(T.dot(Xr.T) - yr.T)**2




In [ ]:

np.sum((T.dot(Xr.T) - yr.T)**2, axis=1)




In [ ]:

J = (1. / len(Xr) * np.sum((T.dot(Xr.T) - yr.T)**2, axis=1)).reshape(t1.shape)
J



### Now we get the MSE cost function @ each meshgrid

• And it's shape is the mesh shape


In [ ]: