Week 3 Logistic Regression Assignment Exercise 4

Exercise 4

Please complete the predict function to produce 1 or 0 predictions given a
dataset and a learned parameter vector 𝑀w and 𝑏b.

  • First you need to compute the prediction from the model 𝑓(π‘₯(𝑖))=𝑔(𝑀⋅π‘₯(𝑖))f(x(i))=g(wβ‹…x(i)) for every example
    • You’ve implemented this before in the parts above
  • We interpret the output of the model (𝑓(π‘₯(𝑖))f(x(i))) as the probability that 𝑦(𝑖)=1y(i)=1 given π‘₯(𝑖)x(i) and parameterized by 𝑀w.
  • Therefore, to get a final prediction (𝑦(𝑖)=0y(i)=0 or 𝑦(𝑖)=1y(i)=1) from the logistic regression model, you can use the following heuristic -if 𝑓(π‘₯(𝑖))>=0.5f(x(i))>=0.5, predict 𝑦(𝑖)=1y(i)=1if 𝑓(π‘₯(𝑖))<0.5f(x(i))<0.5, predict 𝑦(𝑖)=0y(i)=0

If you get stuck, you can check out the hints presented after the cell below to help you with the implementation.

def predict(X, w, b):
β€œβ€"
Predict whether the label is 0 or 1 using learned logistic
regression parameters w

Args:
X : (ndarray Shape (m, n))
w : (array_like Shape (n,))      Parameters of the model
b : (scalar, float)              Parameter of the model

Returns:
p: (ndarray (m,1))
    The predictions for X using a threshold at 0.5
"""
# number of training examples
m, n = X.shape   
p = np.zeros(m)

### START CODE HERE ### 
# Loop over each example
for i in range(m):   
    z_wb = None
    # Loop over each feature
    for j in range(n): 
        # Add the corresponding term to z_wb
        z_wb += None
    
    # Add bias term 
    z_wb += None
    
    # Calculate the prediction for this example
    f_wb = None

    # Apply the threshold
    p[i] = None
    
### END CODE HERE ### 
return p

Test your predict code

np.random.seed(1)
tmp_w = np.random.randn(2)
tmp_b = 0.3
tmp_X = np.random.randn(4, 2) - 0.5

tmp_p = predict(tmp_X, tmp_w, tmp_b)
print(f’Output of predict: shape {tmp_p.shape}, value {tmp_p}')

Can someone solve this question.

Hi @Ali_Nawab On which step are you stuck?
You can click on check hints, if you are not getting any Idea of how to solve a step, which you will find in green letters.

You need to replace the β€œNone” statements with the correct code to implement the predict function.

Since this is a programming assignment, you need to implement the code yourself - not by asking others to solve it for you.

can you solve this exercise for me, I will be very thankful to you dear…

No, I cannot do that. If you don’t do it yourself, then there is no point of doing it.

Hi @Ali_Nawab, lets review this exercise:
You are receiving an input X which is a matrix with m rows and n columns, where each row is a sample and each column is an attribute of the sample.

Then you are receiving the w parameters which is an array n this case, and the b parameter which is a scalar.

The goal is to predict, for each row in X, if it is 1 (lets say True or class X) or 0 (lets say False or not class X). How do we go about it?

Well, since you have to do this prediction for each sample in X, then you have to navigate each and every row, and for that you are given the first FOR LOOP.

Then, for each row, you have to come up with a prediction. For that you need to see how each attribute contributes to the prediction and to do this you are given the second FOR LOOP, to traverse each attribute of the current row.

On this second loop, remember the basic formula z = (X*W) + b. So basically you’ll want to apply the w parameters to the attributes of the row, and at the end you’ll want to add the b parameter.

Once you get the β€˜z’ value as a result of the basic formula above, you have to apply an activation function that can help yo do that. There are several, but up to this point in the course you should be familiar with the sigmoid function.

When you apply the sigmoid, you’ll get as a result a value between 0 and 1. At that point you can apply the indicated threshold and determine if the result is TRUE (or class X) or FALSE (or not class X).

Since you have to do this for every row, you’ll want to store each result in the vector β€˜p’.

At the end, your vector P will contain the predictions for each and every sample in the input X.

Check this out step by step and you’ll be able to complete the code.

Good luck!

Juan

1 Like

Actually this was the most tricky exercie i faced. Its so easy and direct that i wasted 30 minutes to just understand what is required, because it was not clear what i should do. You need to check the first hint without expanding it, and use the function you created earlier. Check the output of the function whether its greater or smaller than 0.5 and return this value. You dont need to follow what is written exactly, e.g., you can remove the bias term.

1 Like