# 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

z_wb += None

# Calculate the prediction for this example
f_wb = None

# Apply the threshold
p[i] = None

### END CODE HERE ###
return p
``````

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.