Is it possible to write all loops as vector operation?

Hi,
Specifically I mean loops introduced in the labs,

    cost = 0.
    for i in range(m):
        f_wb_i = np.dot(X[i], w) + b                                   #(n,)(n,)=scalar, see np.dot
        cost = cost + (f_wb_i - y[i])**2                               #scalar 

here, the dot product is used, however it is inside a loop. Is it possible to write this loop as a vector product?

1 Like

Yes. Just omit the indexing from X and don’t use a for loop.

Turns out I’m pretty bad at math!
I’m just out of shape as the line:
z_wb = np.dot(w, X) + b
with shapes:
ValueError: shapes (2,) and (100,2) not aligned: 2 (dim 0) != 100 (dim 0)

Ok but eventually I managed to solve it. The trick is, that np.dot() can compute 2d array against a 1d array only if the 2D one is passed as the first argument and 1d as the second one.