Gergő Horváth
Gergő Horváth

Reputation: 3705

Numpy - is there a way to specify broadcasting dimension?

Broadcasting is only possible (as far as I know) with matrices matching shape from the end (shape [4,3,2] is broadcastable with shapes [2], [3,2], [4,3,2]). But why?

Consider the following example:

np.zeros([4,3,2])


[[[0 0]
  [0 0]
  [0 0]]

 [[0 0]
  [0 0]
  [0 0]]

 [[0 0]
  [0 0]
  [0 0]]

 [[0 0]
  [0 0]
  [0 0]]]

Why broadcasting with [1,2,3], or [1,2,3,4] isn't possible?

Adding with [1,2,3] (shape: [3], target shape: [4,3,2]) expected result:

[[[1 1]
  [2 2]
  [3 3]]

 [[1 1]
  [2 2]
  [3 3]]

 [[1 1]
  [2 2]
  [3 3]]

 [[1 1]
  [2 2]
  [3 3]]]

Adding with [1,2,3,4] (shape: [4], target shape: [4,3,2]) expected result:

[[[1 1]
  [1 1]
  [1 1]]

 [[2 2]
  [2 2]
  [2 2]]

 [[3 3]
  [3 3]
  [3 3]]

 [[4 4]
  [4 4]
  [4 4]]]

Or, if there would be concerns about multi dimensional broadcasting this way, adding with:

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

(shape: [4,3], target shape: [4,3,2]) expected result:

[[[ 1  1]
  [ 2  2]
  [ 3  3]]

 [[ 4  4]
  [ 5  5]
  [ 6  6]]

 [[ 7  7]
  [ 8  8]
  [ 9  9]]

 [[10 10]
  [11 11]
  [12 12]]]

So basically what I'm saying is that I can't see a reason why it couldn't find the matching shape, and do the operations respectively. If there's multiple dimensions matching in the target matrix, just select the last one automatically, or have the option to specify which dimension we want to perform the operation.

Any ideas/suggestions?

Upvotes: 0

Views: 206

Answers (1)

hpaulj
hpaulj

Reputation: 231335

The broadcasting rules are simple and unambiguous.

  • add leading size 1 dimension as needed to match total number of dimensions
  • adjust all size 1 dimensions as needed to match

With (4,3,2)

(2,) => (1,1,2) => (4,3,2)
(3,2) => (1,3,2) => (4,3,2)

(3,) => (1,1,3) => (4,3,3) ERROR
(4,) => (1,1,4)
(4,3) => (1,4,3)

With reshape or np.newaxis we can add explicit new dimensions in the right place:

(3,1) => (1,3,1) => (4,3,2)
(4,1,1) => (4,3,2)
(4,3,1) => (4,3,2)

Why doesn't it do the last stuff automatically? Potential ambiguity. Without those rules, especially the 'add only leading', it would be possible to add the extra dimension in several different places.

e.g.

(2,3,3) + (3,) => is that (1,1,3) or (1,3,1)?
(2,3,3,3) + (3,3)

Upvotes: 3

Related Questions