Week 2 How to vectorize code in predict function?

I had a solution for this in Matlab that worked in the Intro to Machine Learning course (from Prof Ng), but I never really understood it–at least not well enough to translate it into Python. Any hints on how to go about vectorizing the for loop step?

Just as in MATLAB, you can do direct Boolean comparisons on arrays. Try creating a new cell in your notebook and running the following code and watch what happens:

A = np.random.rand(3,4)
print("A = " + str(A))
B = (A > 0.7)
print("B = " + str(B))
C = B.astype(float)
print("C = " + str(C))
D = (A > 0.7).astype(float)
print("D = " + str(D))

Note that is not exactly a solution to your problem, but just a demonstration of the idea. It should be easy to adapt that idea to write the whole thing in one pretty simple and clear line of python.

1 Like

I think the guide @paulinpaloalto gave is a great demonstration for the problem you might be having.
Just to add, the key here to this that he uses

A = np.random.rand(3,4)

which returns a np.array. It means that you can use the broadcasting operation of it. (so it eliminates the need for explicit for-loop). i.e.,

a = np.array([1.0, 2.0, 3.0])
b = 2.0
a * b
#array([ 2., 4., 6.])

hope that helps.

1 Like

Thank you @suki and @paulinpaloalto !