The way this function is written confuses me from the notebook titled, C1_W2_Lab02_Multiple_Variable_Soln,

def predict_single_loop(x, w, b):
"""
single predict using linear regression
Args:
x (ndarray): Shape (n,) example with multiple features
w (ndarray): Shape (n,) model parameters
b (scalar): model parameter
Returns:
p (scalar): prediction
"""
n = x.shape[0]
p = 0
for i in range(n):
p_i = x[i] * w[i]
p = p + p_i
p = p + b
return p

The name of this function is predict_single_loop which may be fine, but I am confused by some of the variable names inside. For example, we see the presence of p in a variable name indicating the idea of a “prediction.” However, look at this section,

p = 0
for i in range(n):
p_i = x[i] * w[i]
p = p + p_i

I find this confusing because we are not dealing with a prediction here are we? I thought the point of this code was to implement a loop function for calculating a single prediction where we loop over \vec{x} and \vec{w} with x_{j} and w_{j}? (Note that I am using j here instead of i because of the convention in the class where iterating over samples uses i).

Maybe I am wrong and the p stands for “parameter?” I hope I am not being too nit-picky about the function here.

In this notebook you calculating a single prediction in multiple linear regression so you doing a for loop over the columns(features) do you have to calculate b0+w1x1 + w2x2 …to number of features do you have like this photo but in this photo w1 ,w2 … to wn called b1 ,b2…to bn

all these calculations to calculate only one prediction as it is multiple linear regression …so we make a new variable called p as it is equal y and doing for loop over features (n) we have

Note you can do it (implement)by vector implementation you will learn it also in this specialization

Hi Jesse. That’s great, and as the previous answer suggest p refers to the prediction, usually when you write code you want to have a good reference of the variables that you are using so this type of confusion don’t happen, so it is a good thing that you want to dive deep into what’s going on with the code!

I suppose my emphasis is especially on this variable, p_i. This variable never represents a prediction. And more confusing, it is never a prediction at sample i. I suppose this is getting too picky, but reading the code was confusing for me. Maybe a name like wx_j would be better?

Yes! I think I agree with you. This is the weights at time i or w[i] multiply by the x at time i or x[i] so using the same picture from above, p_i will represent the calculation marked as red at time i, so is not the prediction of the full model, but it could be seen as the prediction component at time i (if we want to justify the use of that name). But maybe is not so clear.

The confusion can be cleared by looking at the descriptions of x and w given at the beginning of the function under Args.

As you can see here, x has a shape of “n” which stands for “n” features. Likewise, w also has a shape of “n” corresponding to the “n” features. The loop “i” over n features is done so as to get: w_1.x_1 + w_2.x_2 +....+ w_n.x_n. Finally, outside the loop we add b. So, p = w_1.x_1 + w_2.x_2 +....+ w_n.x_n + b