def compute_model_output(x, w, b):
“”"
Computes the prediction of a linear model
Args:
x (ndarray (m,)): Data, m examples
w,b (scalar) : model parameters
Returns
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]
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.
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.