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.