-1

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?

Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72

3 Answers3

2

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)
user2357112
  • 260,549
  • 28
  • 431
  • 505
1

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])
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

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, :]
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72