NumPy - How to broadcast arrays of different shapes
up vote
0
down vote
favorite
I have a 200 x 200 array of vectors. Its shape is (200, 200, 3)
.
I also have an array of 22 vectors. Its shape is (22,3)
.
I want to subtract all 22 vectors in the second array from each vector in the first array. The output should have shape (200, 200, 22, 3)
.
I'd like to perform an operation like
first - second
But I get the error
*** ValueError: operands could not be broadcast together with shapes (200,200,3) (22,3)
I think I need to pad the first array somehow in order to allow NumPy to broadcast the arrays together. How do I tell NumPy how to perform the broadcast?
numpy numpy-broadcasting numpy-ndarray
add a comment |
up vote
0
down vote
favorite
I have a 200 x 200 array of vectors. Its shape is (200, 200, 3)
.
I also have an array of 22 vectors. Its shape is (22,3)
.
I want to subtract all 22 vectors in the second array from each vector in the first array. The output should have shape (200, 200, 22, 3)
.
I'd like to perform an operation like
first - second
But I get the error
*** ValueError: operands could not be broadcast together with shapes (200,200,3) (22,3)
I think I need to pad the first array somehow in order to allow NumPy to broadcast the arrays together. How do I tell NumPy how to perform the broadcast?
numpy numpy-broadcasting numpy-ndarray
Doesn't subtracting 3D vectors produce another 3D vector? How is the output shape (200, 200, 22)? Shoudn't it be (200, 200, 22, 3)?
– eozd
Nov 19 at 21:22
You're right, fixed.
– bcattle
Nov 19 at 21:23
3
first[:,:,None,:] - second
; or to be a bit more explicitfirst[:,:,None,:] - second[None,None,:,:]
. In other words imagine where the two arrays have to be expanded to fit in the target (200,200,22,3) array.
– hpaulj
Nov 19 at 21:24
Works beautifully, thanks
– bcattle
Nov 19 at 21:28
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a 200 x 200 array of vectors. Its shape is (200, 200, 3)
.
I also have an array of 22 vectors. Its shape is (22,3)
.
I want to subtract all 22 vectors in the second array from each vector in the first array. The output should have shape (200, 200, 22, 3)
.
I'd like to perform an operation like
first - second
But I get the error
*** ValueError: operands could not be broadcast together with shapes (200,200,3) (22,3)
I think I need to pad the first array somehow in order to allow NumPy to broadcast the arrays together. How do I tell NumPy how to perform the broadcast?
numpy numpy-broadcasting numpy-ndarray
I have a 200 x 200 array of vectors. Its shape is (200, 200, 3)
.
I also have an array of 22 vectors. Its shape is (22,3)
.
I want to subtract all 22 vectors in the second array from each vector in the first array. The output should have shape (200, 200, 22, 3)
.
I'd like to perform an operation like
first - second
But I get the error
*** ValueError: operands could not be broadcast together with shapes (200,200,3) (22,3)
I think I need to pad the first array somehow in order to allow NumPy to broadcast the arrays together. How do I tell NumPy how to perform the broadcast?
numpy numpy-broadcasting numpy-ndarray
numpy numpy-broadcasting numpy-ndarray
edited Nov 19 at 21:23
asked Nov 19 at 21:15
bcattle
5,91943459
5,91943459
Doesn't subtracting 3D vectors produce another 3D vector? How is the output shape (200, 200, 22)? Shoudn't it be (200, 200, 22, 3)?
– eozd
Nov 19 at 21:22
You're right, fixed.
– bcattle
Nov 19 at 21:23
3
first[:,:,None,:] - second
; or to be a bit more explicitfirst[:,:,None,:] - second[None,None,:,:]
. In other words imagine where the two arrays have to be expanded to fit in the target (200,200,22,3) array.
– hpaulj
Nov 19 at 21:24
Works beautifully, thanks
– bcattle
Nov 19 at 21:28
add a comment |
Doesn't subtracting 3D vectors produce another 3D vector? How is the output shape (200, 200, 22)? Shoudn't it be (200, 200, 22, 3)?
– eozd
Nov 19 at 21:22
You're right, fixed.
– bcattle
Nov 19 at 21:23
3
first[:,:,None,:] - second
; or to be a bit more explicitfirst[:,:,None,:] - second[None,None,:,:]
. In other words imagine where the two arrays have to be expanded to fit in the target (200,200,22,3) array.
– hpaulj
Nov 19 at 21:24
Works beautifully, thanks
– bcattle
Nov 19 at 21:28
Doesn't subtracting 3D vectors produce another 3D vector? How is the output shape (200, 200, 22)? Shoudn't it be (200, 200, 22, 3)?
– eozd
Nov 19 at 21:22
Doesn't subtracting 3D vectors produce another 3D vector? How is the output shape (200, 200, 22)? Shoudn't it be (200, 200, 22, 3)?
– eozd
Nov 19 at 21:22
You're right, fixed.
– bcattle
Nov 19 at 21:23
You're right, fixed.
– bcattle
Nov 19 at 21:23
3
3
first[:,:,None,:] - second
; or to be a bit more explicit first[:,:,None,:] - second[None,None,:,:]
. In other words imagine where the two arrays have to be expanded to fit in the target (200,200,22,3) array.– hpaulj
Nov 19 at 21:24
first[:,:,None,:] - second
; or to be a bit more explicit first[:,:,None,:] - second[None,None,:,:]
. In other words imagine where the two arrays have to be expanded to fit in the target (200,200,22,3) array.– hpaulj
Nov 19 at 21:24
Works beautifully, thanks
– bcattle
Nov 19 at 21:28
Works beautifully, thanks
– bcattle
Nov 19 at 21:28
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382752%2fnumpy-how-to-broadcast-arrays-of-different-shapes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Doesn't subtracting 3D vectors produce another 3D vector? How is the output shape (200, 200, 22)? Shoudn't it be (200, 200, 22, 3)?
– eozd
Nov 19 at 21:22
You're right, fixed.
– bcattle
Nov 19 at 21:23
3
first[:,:,None,:] - second
; or to be a bit more explicitfirst[:,:,None,:] - second[None,None,:,:]
. In other words imagine where the two arrays have to be expanded to fit in the target (200,200,22,3) array.– hpaulj
Nov 19 at 21:24
Works beautifully, thanks
– bcattle
Nov 19 at 21:28