Quick ways to manipulate Numpy array in array
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
[array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
For instance, I'd like to compute the total number of array elements, which is 16 for the array above, but without doing a for loop since in practice my "nested array" will be quite large.
Thanks!
python arrays numpy
add a comment |
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
[array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
For instance, I'd like to compute the total number of array elements, which is 16 for the array above, but without doing a for loop since in practice my "nested array" will be quite large.
Thanks!
python arrays numpy
1
Define "manipulation". If all you want is the length, it would be better to flatten into a single array. Otherwise your only choice s iteration because these arrays are uneven length.
– coldspeed
Dec 31 '18 at 7:58
1
That's a list of arrays - or object dtype array. Loops, list comprehensions are the normal tools.np.frompyfunc
may be useful in some cases, with a modest speed difference.
– hpaulj
Dec 31 '18 at 8:04
As shown in the answer, 'flattening' the list into one array with concatenate way help. It depends on the manipulation.
– hpaulj
Dec 31 '18 at 16:57
add a comment |
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
[array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
For instance, I'd like to compute the total number of array elements, which is 16 for the array above, but without doing a for loop since in practice my "nested array" will be quite large.
Thanks!
python arrays numpy
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
[array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
For instance, I'd like to compute the total number of array elements, which is 16 for the array above, but without doing a for loop since in practice my "nested array" will be quite large.
Thanks!
python arrays numpy
python arrays numpy
edited Dec 31 '18 at 7:57
coldspeed
129k23133219
129k23133219
asked Dec 31 '18 at 7:55
nusjjsunnusjjsun
263
263
1
Define "manipulation". If all you want is the length, it would be better to flatten into a single array. Otherwise your only choice s iteration because these arrays are uneven length.
– coldspeed
Dec 31 '18 at 7:58
1
That's a list of arrays - or object dtype array. Loops, list comprehensions are the normal tools.np.frompyfunc
may be useful in some cases, with a modest speed difference.
– hpaulj
Dec 31 '18 at 8:04
As shown in the answer, 'flattening' the list into one array with concatenate way help. It depends on the manipulation.
– hpaulj
Dec 31 '18 at 16:57
add a comment |
1
Define "manipulation". If all you want is the length, it would be better to flatten into a single array. Otherwise your only choice s iteration because these arrays are uneven length.
– coldspeed
Dec 31 '18 at 7:58
1
That's a list of arrays - or object dtype array. Loops, list comprehensions are the normal tools.np.frompyfunc
may be useful in some cases, with a modest speed difference.
– hpaulj
Dec 31 '18 at 8:04
As shown in the answer, 'flattening' the list into one array with concatenate way help. It depends on the manipulation.
– hpaulj
Dec 31 '18 at 16:57
1
1
Define "manipulation". If all you want is the length, it would be better to flatten into a single array. Otherwise your only choice s iteration because these arrays are uneven length.
– coldspeed
Dec 31 '18 at 7:58
Define "manipulation". If all you want is the length, it would be better to flatten into a single array. Otherwise your only choice s iteration because these arrays are uneven length.
– coldspeed
Dec 31 '18 at 7:58
1
1
That's a list of arrays - or object dtype array. Loops, list comprehensions are the normal tools.
np.frompyfunc
may be useful in some cases, with a modest speed difference.– hpaulj
Dec 31 '18 at 8:04
That's a list of arrays - or object dtype array. Loops, list comprehensions are the normal tools.
np.frompyfunc
may be useful in some cases, with a modest speed difference.– hpaulj
Dec 31 '18 at 8:04
As shown in the answer, 'flattening' the list into one array with concatenate way help. It depends on the manipulation.
– hpaulj
Dec 31 '18 at 16:57
As shown in the answer, 'flattening' the list into one array with concatenate way help. It depends on the manipulation.
– hpaulj
Dec 31 '18 at 16:57
add a comment |
1 Answer
1
active
oldest
votes
One way to find the length of the array in your case is to ravel the nested numpy arrays and then find the length as below:
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
len(np.concatenate(a).ravel())
#Here we expand the numpy arrays and then flatten it to find the length.
Output:
16
As per my knowledge, ravel has a better timeit performance time in comparison to for loop.
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
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%2f53984985%2fquick-ways-to-manipulate-numpy-array-in-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
One way to find the length of the array in your case is to ravel the nested numpy arrays and then find the length as below:
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
len(np.concatenate(a).ravel())
#Here we expand the numpy arrays and then flatten it to find the length.
Output:
16
As per my knowledge, ravel has a better timeit performance time in comparison to for loop.
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
add a comment |
One way to find the length of the array in your case is to ravel the nested numpy arrays and then find the length as below:
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
len(np.concatenate(a).ravel())
#Here we expand the numpy arrays and then flatten it to find the length.
Output:
16
As per my knowledge, ravel has a better timeit performance time in comparison to for loop.
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
add a comment |
One way to find the length of the array in your case is to ravel the nested numpy arrays and then find the length as below:
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
len(np.concatenate(a).ravel())
#Here we expand the numpy arrays and then flatten it to find the length.
Output:
16
As per my knowledge, ravel has a better timeit performance time in comparison to for loop.
One way to find the length of the array in your case is to ravel the nested numpy arrays and then find the length as below:
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
len(np.concatenate(a).ravel())
#Here we expand the numpy arrays and then flatten it to find the length.
Output:
16
As per my knowledge, ravel has a better timeit performance time in comparison to for loop.
answered Dec 31 '18 at 8:25
Jim ToddJim Todd
42737
42737
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
add a comment |
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
I don't think you need the ravel here.
– hpaulj
Dec 31 '18 at 13:50
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.
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%2f53984985%2fquick-ways-to-manipulate-numpy-array-in-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
1
Define "manipulation". If all you want is the length, it would be better to flatten into a single array. Otherwise your only choice s iteration because these arrays are uneven length.
– coldspeed
Dec 31 '18 at 7:58
1
That's a list of arrays - or object dtype array. Loops, list comprehensions are the normal tools.
np.frompyfunc
may be useful in some cases, with a modest speed difference.– hpaulj
Dec 31 '18 at 8:04
As shown in the answer, 'flattening' the list into one array with concatenate way help. It depends on the manipulation.
– hpaulj
Dec 31 '18 at 16:57