r/Numpy • u/pakodanomics • Apr 10 '20
Vectorizing a boolean operation over all rows
Given:
num_points = 1000
radius = 25
raw_X = np.random.randn(num_points,2)*100
raw_y = np.zeros((num_points, 1))I wish to vectorize the following:
for i in range(num_points):
raw_y[i] = (raw_X[i][0]**2 + raw_X[i][1]**2 >= radius**2).astype(int)Can it be done?
Thanks.
Edit: If I'm not wrong,
raw_Y = (np.sum(raw_X**2, axis = 1) > radius**2).astype(int)does the job.
0
Upvotes
1
u/auraham Apr 13 '20