dantes_419
dantes_419

Reputation: 189

Python, Numpy, method of adding arrays together in multiple dimensions (broadcasting)

I have a number of different size arrays with a common index.

For example,

Arr1 = np.arange(0, 1000, 1).reshape(100, 10)
Arr2 = np.arange(0, 500, 1).reshape(100,5)

Arr1.shape = (100, 10)
Arr2.shape = (100, 5)

I want to add these together into a new Array, Arr3 which is three dimensional. e.g.

Arr3 = Arr1 + Arr2
Arr3.shape = (100, 10, 5)

Note, in this instance the values should allign e.g.

Arr3[10, 3, 2] =  Arr1[10, 3] + Arr2[10, 2]

I have been attempting to use the following method

test = Arr1.copy()
test = test[:, np.newaxis] + Arr2

Now, I've been able to make this work when adding two square matrices together.

m = np.arange(0, 100, 1)
[x, y] = np.meshgrid(x, y)
x.shape = (100, 100)

test44 = x.copy()
test44 = test44[:, np.newaxis] + x
test44.shape = (100, 100, 100)
test44[4, 3, 2] = 4
x[4, 2] = 2
x[3, 2] = 2

However, in my actual program I will not have square matrices for this issue. In addition this method is extremely memory intensive as evidenced when you begin moving up the number of dimensions as follows.

test44 = test44[:, :, np.newaxis] + x
test44.shape = (100, 100, 100, 100)

# Note this next command will fail with a memory error on my computer.
test44 = test44[:, :, :, np.newaxis] + x

So my question has two parts:

  1. Is it possible to create a 3D array from two differently shaped 2D array with a common "shared" axis.
  2. Is such a method extensible at higher order dimensions?

Any assistance is greatly appreciated.

Upvotes: 4

Views: 4930

Answers (1)

Bi Rico
Bi Rico

Reputation: 25833

Yes what you're trying to do is called broadcasting, it's done automatically by numpy if the inputs have the right shapes. Try this:

Arr1 = Arr1.reshape((100, 10, 1))
Arr2 = Arr2.reshape((100, 1, 5))
Arr3 = Arr1 + Arr2

I've found this to be a really good introduction to broadcasting which should show you how to extend this kind of behavior to n dimensions.

Upvotes: 4

Related Questions