Compute model output

def compute_model_output(x, w, b):
Computes the prediction of a linear model
x (ndarray (m,)): Data, m examples
w,b (scalar) : model parameters
y (ndarray (m,)): target values
m = x.shape[0]
f_wb = np.zeros(m)
for i in range(m):
f_wb[i] = w * x[i] + b

return f_wb

Please can somebody help me explain further.
why defining m = x.shape[0]

Hello @esssyjr

Welcome to our community.

In this case, x is a 1-d array, having m elements in it, each representing a sample.

For each element in x, there is a corresponding value of the target/output variable y.

So, we do m = x.shape[0] to first find out the length of x, which corresponds to the number of samples, m.

Once we have this value m, we then proceed to find the predicted value of the model \hat {y} for each of the m samples. The expression inside the FOR loop does just that.

okay but if i run this code
compute_model_output(5, 200, 100)
this is what it is giving me

AttributeError Traceback (most recent call last)
----> 1 compute_model_output(5, 200, 100)

in compute_model_output(x, w, b)
8 y (ndarray (m,)): target values
9 “”"
—> 10 m = x.shape[0]
11 f_wb = np.zeros(m)
12 for i in range(m):

AttributeError: ‘int’ object has no attribute ‘shape’

Since we have made the assumption that x is an array (by using x.shape[0]) inside compute_model_output, we will always have to provide x in the form of an array, while invoking this function.

In this case, you are passing a scalar value “5” as the value of x, which is causing the error. Replace 5 with an array and it will work fine.

Yeah I got it
Thank you very much

You are most welcome @esssyjr :blush: