Callahan Callahan -3 years ago 133
Python Question

Vectorizing Numpy for loops

I'm currently trying to vectorize a few operations in NumPy. s is a very large number (10000) and X represents a numpy array with around 1200000

for element1 in range(1,s+1):
d = np.zeros(s)
for element2 in range(1,s+1):
d[element2-1] = norm(np.subtract(X[0:n,element1],X[0:n,element2]))


I'm trying to rewrite this without using for loops but I can't think of a way. One method of trying involves using zip and np.tile, but that yields wrong results.

Answer Source

Those are basically euclidean distances on a slice off the input array -

from scipy.spatial.distance import cdist, pdist, squareform

X_slice = X[0:n,1:s+1]
d_all = squareform(pdist(X_slice.T))

Thus, inside the first loop, it would be just a slice from the output d_all that could be re-used as d, like so -

for element1 in range(1,s+1):
    d = d_all[element1-1, :] 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download