Home > Net >  Is there a Numpy array broadcasting expression simpler than this list comprehension?
Is there a Numpy array broadcasting expression simpler than this list comprehension?

Time:05-09

In this code snippet:

triangles = np.float32([[[0, -2], [-2, 3], [1, 1]], [[0, -1], [-1, 3], [1, 1]]])
centers = np.average(triangles, axis=1)
samples = np.float32([t-centers[i] for i, t in enumerate(triangles)])

I would like to express samples as an array broadcast subtraction, i.e. something similar to triangles-centers, which doesn't work due to:

ValueError: operands could not be broadcast together with shapes (2,3,2) (2,2) 

Is there a simpler way to define samples than a list comprehension?

CodePudding user response:

Use numpy.mean with keepdims=True to keep a length-1 dimension in the axis you're taking the mean over:

samples = triangles - np.mean(triangles, axis=1, keepdims=True)

CodePudding user response:

I believe this does what you want. Just reshape the centers to match the row size.

samples = triangles - centers.reshape(triangles.shape[0],-1,triangles.shape[2])

CodePudding user response:

Inspired by an answer above from @Tim Roberts:

triangles = np.float32([[[0, -2], [-2, 3], [1, 1]], [[0, -1], [-1, 3], [1, 1]]])
centers = np.average(triangles, axis=1)
samples = triangles-centers[:, None, :]
  • Related