Numpy array broadcasting rules

Well, the meaning of trailing axes is explained on the linked documentation page. If you have two arrays with different dimensions number, say one 1x2x3 and other 2×3, then you compare only the trailing common dimensions, in this case 2×3. But if both your arrays are two-dimensional, then their corresponding sizes have to be either … Read more

How to find the pairwise differences between rows of two very large matrices using numpy?

Here’s another way to perform : (a-b)^2 = a^2 + b^2 – 2ab with np.einsum for the first two terms and dot-product for the third one – import numpy as np np.einsum(‘ij,ij->i’,A,A)[:,None] + np.einsum(‘ij,ij->i’,B,B) – 2*np.dot(A,B.T) Runtime test Approaches – def loopy_app(A,B): m,n = A.shape[0], B.shape[0] out = np.empty((m,n)) for i,a in enumerate(A): out[i] = … Read more

NumPy Broadcasting: Calculating sum of squared differences between two arrays

You can use np.einsum after calculating the differences in a broadcasted way, like so – ab = a[:,None,:] – b out = np.einsum(‘ijk,ijk->ij’,ab,ab) Or use scipy’s cdist with its optional metric argument set as ‘sqeuclidean’ to give us the squared euclidean distances as needed for our problem, like so – from scipy.spatial.distance import cdist out … Read more

Vectorized NumPy linspace for multiple start and stop values

Here’s an approach using broadcasting – def create_ranges(start, stop, N, endpoint=True): if endpoint==1: divisor = N-1 else: divisor = N steps = (1.0/divisor) * (stop – start) return steps[:,None]*np.arange(N) + start[:,None] Sample run – In [22]: # Setup start, stop for each row and no. of elems in each row …: start = np.array([1,4,2]) …: … Read more

Numpy – create matrix with rows of vector

Certainly possible with broadcasting after adding with m zeros along the columns, like so – np.zeros((m,1),dtype=vector.dtype) + vector Now, NumPy already has an in-built function np.tile for exactly that same task – np.tile(vector,(m,1)) Sample run – In [496]: vector Out[496]: array([4, 5, 8, 2]) In [497]: m = 5 In [498]: np.zeros((m,1),dtype=vector.dtype) + vector Out[498]: … Read more

Change sign of elements with an odd sum of indices

np.negative is silghtly faster than multiplying (as it is a ufunc) N = 5 arr = np.arange(N ** 3).reshape(N, N, N) %timeit arr.ravel()[1::2] *= -1 %timeit np.negative(arr.ravel()[1::2], out = arr.ravel()[1::2]) The slowest run took 8.74 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: … Read more