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 , then this is a straight line:
Equation-1
where is the y-intercept of the linear and is the slope of the line. When there are two inputs and , then this becomes:
Equation-2
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=5Now 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 yUsing 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, , , and and want to compute two linear functions giving and . Of course, we could do this by just running each equation separately,
However, we can write it more compactly with vectors and matrices:
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