Numpy ValueError broadcasting list of tuples into an array
I'm observing some odd behaviour using numpy broadcasting. The problem is illustrated below, where running the first piece of code produces an error:
A = np.ones((10))
B = np.ones((10, 4))
C = np.ones((10))
np.asarray([A, B, C])
ValueError: could not broadcast input array from shape (10,4) into shape (10)
If I instead expand the dimensions of B, using B = np.expand_dims(B, axis=0)
, it will successfully create the array, but it now has (not surprisingly) the wrong dimensions:
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=float32)
Why does it fail to broadcast the first example, and how can I end up with an array like below (notice only double brackets around the second array)? Any feedback is much appreciated.
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
python arrays numpy
|
show 1 more comment
I'm observing some odd behaviour using numpy broadcasting. The problem is illustrated below, where running the first piece of code produces an error:
A = np.ones((10))
B = np.ones((10, 4))
C = np.ones((10))
np.asarray([A, B, C])
ValueError: could not broadcast input array from shape (10,4) into shape (10)
If I instead expand the dimensions of B, using B = np.expand_dims(B, axis=0)
, it will successfully create the array, but it now has (not surprisingly) the wrong dimensions:
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=float32)
Why does it fail to broadcast the first example, and how can I end up with an array like below (notice only double brackets around the second array)? Any feedback is much appreciated.
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
python arrays numpy
np.hstack([A[:,None], B, C[:,None]])
?
– Divakar
Nov 19 '18 at 14:38
This doesn't quite work as it creates (in the example above) a new array of shape(10,6)
and not(3,)
or(1,3)
as I need.
– andkir
Nov 19 '18 at 14:41
So, you need an object array with a shape of (3,)?
– Divakar
Nov 19 '18 at 14:42
Yep, or to be precise, once I join (or append) 5 of these together I need them to be of shape (5, 3)
– andkir
Nov 19 '18 at 14:49
1
The common first dimensions (10) in all the arrays sendsnp.array
down a faulty path, trying to create a size (10,?) array. Keep in mind the default behavior fornp.array
is to create a multidimensional (numeric) array. Creating an object array is a fall back option. With this error yet another possibility.
– hpaulj
Nov 19 '18 at 17:15
|
show 1 more comment
I'm observing some odd behaviour using numpy broadcasting. The problem is illustrated below, where running the first piece of code produces an error:
A = np.ones((10))
B = np.ones((10, 4))
C = np.ones((10))
np.asarray([A, B, C])
ValueError: could not broadcast input array from shape (10,4) into shape (10)
If I instead expand the dimensions of B, using B = np.expand_dims(B, axis=0)
, it will successfully create the array, but it now has (not surprisingly) the wrong dimensions:
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=float32)
Why does it fail to broadcast the first example, and how can I end up with an array like below (notice only double brackets around the second array)? Any feedback is much appreciated.
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
python arrays numpy
I'm observing some odd behaviour using numpy broadcasting. The problem is illustrated below, where running the first piece of code produces an error:
A = np.ones((10))
B = np.ones((10, 4))
C = np.ones((10))
np.asarray([A, B, C])
ValueError: could not broadcast input array from shape (10,4) into shape (10)
If I instead expand the dimensions of B, using B = np.expand_dims(B, axis=0)
, it will successfully create the array, but it now has (not surprisingly) the wrong dimensions:
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=float32)
Why does it fail to broadcast the first example, and how can I end up with an array like below (notice only double brackets around the second array)? Any feedback is much appreciated.
array([array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]),
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
python arrays numpy
python arrays numpy
edited Nov 19 '18 at 20:49
Ulrich Stern
5,08712350
5,08712350
asked Nov 19 '18 at 14:32
andkir
205
205
np.hstack([A[:,None], B, C[:,None]])
?
– Divakar
Nov 19 '18 at 14:38
This doesn't quite work as it creates (in the example above) a new array of shape(10,6)
and not(3,)
or(1,3)
as I need.
– andkir
Nov 19 '18 at 14:41
So, you need an object array with a shape of (3,)?
– Divakar
Nov 19 '18 at 14:42
Yep, or to be precise, once I join (or append) 5 of these together I need them to be of shape (5, 3)
– andkir
Nov 19 '18 at 14:49
1
The common first dimensions (10) in all the arrays sendsnp.array
down a faulty path, trying to create a size (10,?) array. Keep in mind the default behavior fornp.array
is to create a multidimensional (numeric) array. Creating an object array is a fall back option. With this error yet another possibility.
– hpaulj
Nov 19 '18 at 17:15
|
show 1 more comment
np.hstack([A[:,None], B, C[:,None]])
?
– Divakar
Nov 19 '18 at 14:38
This doesn't quite work as it creates (in the example above) a new array of shape(10,6)
and not(3,)
or(1,3)
as I need.
– andkir
Nov 19 '18 at 14:41
So, you need an object array with a shape of (3,)?
– Divakar
Nov 19 '18 at 14:42
Yep, or to be precise, once I join (or append) 5 of these together I need them to be of shape (5, 3)
– andkir
Nov 19 '18 at 14:49
1
The common first dimensions (10) in all the arrays sendsnp.array
down a faulty path, trying to create a size (10,?) array. Keep in mind the default behavior fornp.array
is to create a multidimensional (numeric) array. Creating an object array is a fall back option. With this error yet another possibility.
– hpaulj
Nov 19 '18 at 17:15
np.hstack([A[:,None], B, C[:,None]])
?– Divakar
Nov 19 '18 at 14:38
np.hstack([A[:,None], B, C[:,None]])
?– Divakar
Nov 19 '18 at 14:38
This doesn't quite work as it creates (in the example above) a new array of shape
(10,6)
and not (3,)
or (1,3)
as I need.– andkir
Nov 19 '18 at 14:41
This doesn't quite work as it creates (in the example above) a new array of shape
(10,6)
and not (3,)
or (1,3)
as I need.– andkir
Nov 19 '18 at 14:41
So, you need an object array with a shape of (3,)?
– Divakar
Nov 19 '18 at 14:42
So, you need an object array with a shape of (3,)?
– Divakar
Nov 19 '18 at 14:42
Yep, or to be precise, once I join (or append) 5 of these together I need them to be of shape (5, 3)
– andkir
Nov 19 '18 at 14:49
Yep, or to be precise, once I join (or append) 5 of these together I need them to be of shape (5, 3)
– andkir
Nov 19 '18 at 14:49
1
1
The common first dimensions (10) in all the arrays sends
np.array
down a faulty path, trying to create a size (10,?) array. Keep in mind the default behavior for np.array
is to create a multidimensional (numeric) array. Creating an object array is a fall back option. With this error yet another possibility.– hpaulj
Nov 19 '18 at 17:15
The common first dimensions (10) in all the arrays sends
np.array
down a faulty path, trying to create a size (10,?) array. Keep in mind the default behavior for np.array
is to create a multidimensional (numeric) array. Creating an object array is a fall back option. With this error yet another possibility.– hpaulj
Nov 19 '18 at 17:15
|
show 1 more comment
1 Answer
1
active
oldest
votes
Including, say, None
prevents the broadcasting, so this workaround is an option:
np.asarray([A, B, C, None])[:-1]
Here the outcome:
array([array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]),
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53376823%2fnumpy-valueerror-broadcasting-list-of-tuples-into-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Including, say, None
prevents the broadcasting, so this workaround is an option:
np.asarray([A, B, C, None])[:-1]
Here the outcome:
array([array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]),
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
add a comment |
Including, say, None
prevents the broadcasting, so this workaround is an option:
np.asarray([A, B, C, None])[:-1]
Here the outcome:
array([array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]),
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
add a comment |
Including, say, None
prevents the broadcasting, so this workaround is an option:
np.asarray([A, B, C, None])[:-1]
Here the outcome:
array([array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]),
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
Including, say, None
prevents the broadcasting, so this workaround is an option:
np.asarray([A, B, C, None])[:-1]
Here the outcome:
array([array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]]),
array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])], dtype=object)
answered Nov 19 '18 at 16:40
Ulrich Stern
5,08712350
5,08712350
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
add a comment |
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
Worked beautifully, cheers! Learn something new every day.
– andkir
Nov 19 '18 at 19:25
add a comment |
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%2f53376823%2fnumpy-valueerror-broadcasting-list-of-tuples-into-an-array%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
np.hstack([A[:,None], B, C[:,None]])
?– Divakar
Nov 19 '18 at 14:38
This doesn't quite work as it creates (in the example above) a new array of shape
(10,6)
and not(3,)
or(1,3)
as I need.– andkir
Nov 19 '18 at 14:41
So, you need an object array with a shape of (3,)?
– Divakar
Nov 19 '18 at 14:42
Yep, or to be precise, once I join (or append) 5 of these together I need them to be of shape (5, 3)
– andkir
Nov 19 '18 at 14:49
1
The common first dimensions (10) in all the arrays sends
np.array
down a faulty path, trying to create a size (10,?) array. Keep in mind the default behavior fornp.array
is to create a multidimensional (numeric) array. Creating an object array is a fall back option. With this error yet another possibility.– hpaulj
Nov 19 '18 at 17:15