Help for Week 3 final assignment

I have encountered some difficulties in Exercise 4 in the final assignment of Week 3 on the course of “Supervised Machine Learning: Regression and Classification” on Coursea.

I was wondering if anyone could suggest me how to solve that question.

Details as follows.

After defining the function “Sigmoid” and “ Predict” for the Sigmoid function and the logistic regression respectively, a test is run on a combination of randomly generated w1, w2, and 4 input ( i.e. X ). The expected outcome is “shape of 4, and value of 0, 1, 1, 1.” However, the outcome is shape of 4, and value of 0, 0, 0, 0.

The code is as follows.

A. The code of the defined function “Predict” and “Sigmoid”

# Sigmoid function
  # moderator edit: code removed`
    return g
2.	Predict Function
    def predict(X, w, b): 
      # moderator edit: code removed            
      return p

B. The code to randomly generate w, b, x input, and output

     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}')

Output of predict: shape (4,), value [0. 0. 0. 0.]

C. Diagnosis

     np.random.seed(1)
     w = np.random.randn(2)
     b = 0.3
     X = np.random.randn(4, 2) - 0.5

     print(w)
     print(X)

     m,n = X.shape
     print(m)
     print(n)

     z = np.dot(X,w) + b
     pr = sigmoid(z)

     print(z)
     print(pr)

     p = predict (X, w, b)
     print(f'Output of predict: shape {p.shape}, value {p}')

[ 1.62434536 -0.61175641]
[[-1.02817175 -1.57296862]
[ 0.36540763 -2.8015387 ]
[ 1.24481176 -1.2612069 ]
[-0.1809609 -0.74937038]]
4
2
[-0.40783238 2.60740745 3.09355563 0.46448913]
[0.39943199 0.93133679 0.95662614 0.61407858]
Output of predict: shape (4,), value [0. 0. 0. 0.]

In diagnosis, the pr ( i.e. probabilities of all 4 training data predicted by the logistic regression ) are printed and highlighted in red. With the threshold set as 0.5, pr of the training data #2, 3, and 4 that are larger than 0.5 should be labelled as 1. However, they are labelled as 0 by “predict” function.

Could anyone suggest me what to do so as to obtain the expected outcome ( 0, 1, 1, 1 )?

Thank you.

1 Like

First, please do not post your code on the forum. That is not allowed by the course Honor Code.

If a mentor needs to see your code, we will contact you with instructions.

Second, in the code you posted, the indentation seems to be rather incorrect in these places:

Correct indentation is critical for Python code, because that is how it defines executable segments of code.

Note that in your second for-loop from 1 to m, every iteration through the loop is using exactly the same value from the f_wb_i variable.

This is why your code always computes the same result.

Thanks for the reminder. Will make sure codes are not included in the post next time.

About the code, does it mean the f_wb_i input is constantly at f_wb_0 in the 2nd for-loop without looping from f_wb_0 to f_wb_3. If that is the case, could you suggest what modification should be made in the code so the 2nd for-loop could loop from f_wb_0 to f_wb_3?

I recommend a vectorized method, without any for-loops.

  • f_wb can be computed using a dot product and sigmoid.
  • ‘p’ can be computed using a logical comparison.