Practice lab, logistic regression: predict method has an inner loop over the features, seems unnecessary

For the logistic lab at the end of week 3 in the predict method for exercise 4, the stubbed out code has an inner loop that, per the comment, will “loop over each feature,” and “add the corresponding term to z_wb.”

This seems unnecessary, and I can’t see how it is needed in the method.

Hi @doss,

A loop-based approach is used because it helps beginners to understand the mechanics before optimizing the implementation.
The inner loop is inefficient and unnecessary because vector-vector multiplication can replace it entirely. The outer loop is also can be vectorized by using matrix-vector multiplication (np.dot(), np.matmul(), @), eliminating the need for explicit iteration over training examples.

2 Likes

Thanks for the reply, @conscell . I think my confusion came from them wanting the more verbose style with an inner loop as we had long since started using np.dot(). All is clear now. First I’ve seen of np.matmul and @, so thanks also for mentioning those.