Numpy Lab: Linear Functions#

Topics: elementwise operations

Linear functions
We will be using the term linear equation to mean a weighted sum of inputs plus an offset. If there is just one input \(x\), then this is a straight line:

Equation-1

(1)#\[\begin{equation}y=\beta+\omega x,\end{equation}\]

where \(\beta\) is the y-intercept of the linear and \(\omega\) is the slope of the line. When there are two inputs \(x_{1}\) and \(x_{2}\), then this becomes:

Equation-2

(2)#\[\begin{equation}y=\beta+\omega_1 x_1 + \omega_2 x_2.\end{equation}\]

Any other functions are by definition non-linear.

Type your code below#

# Define a linear function with just one input, x (Eq 1)
def linear_function_1D(x, beta, omega):

  return y
# Compute y using the function you filled in above

x = np.arange(0.0,10.0, 0.01)
beta = 0.0; omega = 1.0

y = linear_function_1D(x,beta,omega)

# Plot this function
fig, ax = plt.subplots()
ax.plot(x,y,'r-')
ax.set_ylim([0,10]);ax.set_xlim([0,10])
ax.set_xlabel('x'); ax.set_ylabel('y')
plt.show

# TODO -- experiment with changing the values of beta and omega
# to understand what they do.  Try to make a line
# that crosses the y-axis at y=10 and the x-axis at x=5

Now let’s investigate a 2D linear function#

# We will use this function to draw 2D contour plots. Nothing to do here
def draw_2D_function(x1_mesh, x2_mesh, y):

    fig, ax = plt.subplots()

    pos = ax.contourf(x1_mesh, x2_mesh, y, levels=256 ,cmap = 'hot', vmin=-10,vmax=10.0)
    fig.colorbar(pos, ax=ax)
    
    ax.contour(x1_mesh, 
               x2_mesh, 
               y, 
               levels= np.arange(-10,10,1.0), 
               cmap='winter')
    
    ax.set_xlabel('x1')
    ax.set_ylabel('x2')
    
    plt.show()
# Define a linear function with two inputs, x1 and x2
def linear_function_2D(x1,x2,beta,omega1,omega2):


  return y

Using numpy to write general linear functions in compact form#

Often we will want to compute many linear functions at the same time. For example, we might have three inputs, \(x_1\), \(x_2\), and \(x_3\) and want to compute two linear functions giving \(y_1\) and \(y_2\). Of course, we could do this by just running each equation separately,

(3)#\[\begin{align}y_1 &=& \beta_1 + \omega_{11} x_1 + \omega_{12} x_2 + \omega_{13} x_3\\ y_2 &=& \beta_2 + \omega_{21} x_1 + \omega_{22} x_2 + \omega_{23} x_3. \end{align}\]

However, we can write it more compactly with vectors and matrices:

(4)#\[\begin{equation} \begin{bmatrix} y_1\\ y_2 \end{bmatrix} = \begin{bmatrix}\beta_{1}\\\beta_{2}\end{bmatrix}+ \begin{bmatrix}\omega_{11}&\omega_{12}&\omega_{13}\\\omega_{21}&\omega_{22}&\omega_{23}\end{bmatrix}\begin{bmatrix}x_{1}\\x_{2}\\x_{3}\end{bmatrix}, \end{equation}\]

$$\begin{equation} \mathbf{y} = \boldsymbol\beta +\boldsymbol\Omega\mathbf{x}. \end{equation}

Here, lowercase bold symbols are used for vectors. Upper case bold symbols are used for matrices.

# Define a linear function with three inputs, x1, x2, and x_3
def linear_function_3D(x1,x2,x3,beta,omega1,omega2,omega3):
  # TODO -- replace the code below with formula for a single 3D linear equation
  y = x1

  return y
# Define a linear function using vector input x=[x_1, x_2, x_3], and omega = [omega_1, omega_2, omega_3]
def linear_function_3D_la(x, beta, omega):
  # TODO -- replace the code below with formula for a single 3D linear equation
  y = x1

  return y

Check that both functions agree#