Python: How to reshape a multidimensional array with various dimensions?
Suppose I have an array like this
[[1,2], [3,4,5]]
and I would like to reshape it to
[[[1],[2]], [[3],[4],[5]]]
Is there a simple way to do so in Python? I know this is super easy if the 2nd dimension is the same across the entire data, but in my case the length of my 2nd dimension is 2 and 3, respectively.
Many Thanks.
python numpy reshape
add a comment |
Suppose I have an array like this
[[1,2], [3,4,5]]
and I would like to reshape it to
[[[1],[2]], [[3],[4],[5]]]
Is there a simple way to do so in Python? I know this is super easy if the 2nd dimension is the same across the entire data, but in my case the length of my 2nd dimension is 2 and 3, respectively.
Many Thanks.
python numpy reshape
1
numpy doesn't support ragged arrays like that. are you sure you don't have a list of lists?
– wim
Nov 13 '18 at 20:38
Well, this request is in fact coming from image classification.
– Yao Peng
Nov 14 '18 at 21:22
Sorry I didn't finish yesterday. So when I read image of RGB color into python, it only comes with format like [[1,2], [3,4,5]], however to use keras CNN, the last dimension has to be an array, like [[[1],[2]], [[3],[4],[5]]]. The answers are great if the number of images are relatively small, but when there are 100,000 images of various sizes it could take a while to finish
– Yao Peng
Nov 15 '18 at 15:48
add a comment |
Suppose I have an array like this
[[1,2], [3,4,5]]
and I would like to reshape it to
[[[1],[2]], [[3],[4],[5]]]
Is there a simple way to do so in Python? I know this is super easy if the 2nd dimension is the same across the entire data, but in my case the length of my 2nd dimension is 2 and 3, respectively.
Many Thanks.
python numpy reshape
Suppose I have an array like this
[[1,2], [3,4,5]]
and I would like to reshape it to
[[[1],[2]], [[3],[4],[5]]]
Is there a simple way to do so in Python? I know this is super easy if the 2nd dimension is the same across the entire data, but in my case the length of my 2nd dimension is 2 and 3, respectively.
Many Thanks.
python numpy reshape
python numpy reshape
asked Nov 13 '18 at 20:30
Yao PengYao Peng
191
191
1
numpy doesn't support ragged arrays like that. are you sure you don't have a list of lists?
– wim
Nov 13 '18 at 20:38
Well, this request is in fact coming from image classification.
– Yao Peng
Nov 14 '18 at 21:22
Sorry I didn't finish yesterday. So when I read image of RGB color into python, it only comes with format like [[1,2], [3,4,5]], however to use keras CNN, the last dimension has to be an array, like [[[1],[2]], [[3],[4],[5]]]. The answers are great if the number of images are relatively small, but when there are 100,000 images of various sizes it could take a while to finish
– Yao Peng
Nov 15 '18 at 15:48
add a comment |
1
numpy doesn't support ragged arrays like that. are you sure you don't have a list of lists?
– wim
Nov 13 '18 at 20:38
Well, this request is in fact coming from image classification.
– Yao Peng
Nov 14 '18 at 21:22
Sorry I didn't finish yesterday. So when I read image of RGB color into python, it only comes with format like [[1,2], [3,4,5]], however to use keras CNN, the last dimension has to be an array, like [[[1],[2]], [[3],[4],[5]]]. The answers are great if the number of images are relatively small, but when there are 100,000 images of various sizes it could take a while to finish
– Yao Peng
Nov 15 '18 at 15:48
1
1
numpy doesn't support ragged arrays like that. are you sure you don't have a list of lists?
– wim
Nov 13 '18 at 20:38
numpy doesn't support ragged arrays like that. are you sure you don't have a list of lists?
– wim
Nov 13 '18 at 20:38
Well, this request is in fact coming from image classification.
– Yao Peng
Nov 14 '18 at 21:22
Well, this request is in fact coming from image classification.
– Yao Peng
Nov 14 '18 at 21:22
Sorry I didn't finish yesterday. So when I read image of RGB color into python, it only comes with format like [[1,2], [3,4,5]], however to use keras CNN, the last dimension has to be an array, like [[[1],[2]], [[3],[4],[5]]]. The answers are great if the number of images are relatively small, but when there are 100,000 images of various sizes it could take a while to finish
– Yao Peng
Nov 15 '18 at 15:48
Sorry I didn't finish yesterday. So when I read image of RGB color into python, it only comes with format like [[1,2], [3,4,5]], however to use keras CNN, the last dimension has to be an array, like [[[1],[2]], [[3],[4],[5]]]. The answers are great if the number of images are relatively small, but when there are 100,000 images of various sizes it could take a while to finish
– Yao Peng
Nov 15 '18 at 15:48
add a comment |
3 Answers
3
active
oldest
votes
If you had a list like that:
nested = [[1,2], [3,4,5]]
you could split it out like so:
nested_split = [[[single_elt] for single_elt in inside_list] for inside_list in nested]
which would give you the following output when calling print:
[[[1], [2]], [[3], [4], [5]]]
The dimensionality of the inner or outer lists doesn't affect this solution in any way, since the use of the for loops and list comprehension will dynamically accommodate any size list.
add a comment |
We can quibble about whether this is a list of lists or multidimensional array, but concateante
does a nice job of flattening it into a 1d array:
In [173]: alist = [[1,2], [3,4,5]]
In [175]: np.concatenate(alist, axis=0)
Out[175]: array([1, 2, 3, 4, 5])
Then it's easy to reshape it into a (5,1) shape array:
In [176]: np.concatenate(alist, axis=0).reshape(-1,1)
Out[176]:
array([[1],
[2],
[3],
[4],
[5]])
There are idioms for flattening a list of lists, but since you flagged this a numpy, the numpy approach is more obvious.
In [177]: import itertools
In [178]: list(itertools.chain(*alist))
Out[178]: [1, 2, 3, 4, 5]
In [180]: [[x] for x in itertools.chain(*alist)]
Out[180]: [[1], [2], [3], [4], [5]]
add a comment |
You can use the x for x in array
mechanism:
>>> a = [[1,2], [3,4,5]]
>>> [[[a2] for a2 in a1] for a1 in a]
[[[1], [2]], [[3], [4], [5]]]
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%2f53289026%2fpython-how-to-reshape-a-multidimensional-array-with-various-dimensions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you had a list like that:
nested = [[1,2], [3,4,5]]
you could split it out like so:
nested_split = [[[single_elt] for single_elt in inside_list] for inside_list in nested]
which would give you the following output when calling print:
[[[1], [2]], [[3], [4], [5]]]
The dimensionality of the inner or outer lists doesn't affect this solution in any way, since the use of the for loops and list comprehension will dynamically accommodate any size list.
add a comment |
If you had a list like that:
nested = [[1,2], [3,4,5]]
you could split it out like so:
nested_split = [[[single_elt] for single_elt in inside_list] for inside_list in nested]
which would give you the following output when calling print:
[[[1], [2]], [[3], [4], [5]]]
The dimensionality of the inner or outer lists doesn't affect this solution in any way, since the use of the for loops and list comprehension will dynamically accommodate any size list.
add a comment |
If you had a list like that:
nested = [[1,2], [3,4,5]]
you could split it out like so:
nested_split = [[[single_elt] for single_elt in inside_list] for inside_list in nested]
which would give you the following output when calling print:
[[[1], [2]], [[3], [4], [5]]]
The dimensionality of the inner or outer lists doesn't affect this solution in any way, since the use of the for loops and list comprehension will dynamically accommodate any size list.
If you had a list like that:
nested = [[1,2], [3,4,5]]
you could split it out like so:
nested_split = [[[single_elt] for single_elt in inside_list] for inside_list in nested]
which would give you the following output when calling print:
[[[1], [2]], [[3], [4], [5]]]
The dimensionality of the inner or outer lists doesn't affect this solution in any way, since the use of the for loops and list comprehension will dynamically accommodate any size list.
answered Nov 13 '18 at 20:48
Adithya RamanathanAdithya Ramanathan
573
573
add a comment |
add a comment |
We can quibble about whether this is a list of lists or multidimensional array, but concateante
does a nice job of flattening it into a 1d array:
In [173]: alist = [[1,2], [3,4,5]]
In [175]: np.concatenate(alist, axis=0)
Out[175]: array([1, 2, 3, 4, 5])
Then it's easy to reshape it into a (5,1) shape array:
In [176]: np.concatenate(alist, axis=0).reshape(-1,1)
Out[176]:
array([[1],
[2],
[3],
[4],
[5]])
There are idioms for flattening a list of lists, but since you flagged this a numpy, the numpy approach is more obvious.
In [177]: import itertools
In [178]: list(itertools.chain(*alist))
Out[178]: [1, 2, 3, 4, 5]
In [180]: [[x] for x in itertools.chain(*alist)]
Out[180]: [[1], [2], [3], [4], [5]]
add a comment |
We can quibble about whether this is a list of lists or multidimensional array, but concateante
does a nice job of flattening it into a 1d array:
In [173]: alist = [[1,2], [3,4,5]]
In [175]: np.concatenate(alist, axis=0)
Out[175]: array([1, 2, 3, 4, 5])
Then it's easy to reshape it into a (5,1) shape array:
In [176]: np.concatenate(alist, axis=0).reshape(-1,1)
Out[176]:
array([[1],
[2],
[3],
[4],
[5]])
There are idioms for flattening a list of lists, but since you flagged this a numpy, the numpy approach is more obvious.
In [177]: import itertools
In [178]: list(itertools.chain(*alist))
Out[178]: [1, 2, 3, 4, 5]
In [180]: [[x] for x in itertools.chain(*alist)]
Out[180]: [[1], [2], [3], [4], [5]]
add a comment |
We can quibble about whether this is a list of lists or multidimensional array, but concateante
does a nice job of flattening it into a 1d array:
In [173]: alist = [[1,2], [3,4,5]]
In [175]: np.concatenate(alist, axis=0)
Out[175]: array([1, 2, 3, 4, 5])
Then it's easy to reshape it into a (5,1) shape array:
In [176]: np.concatenate(alist, axis=0).reshape(-1,1)
Out[176]:
array([[1],
[2],
[3],
[4],
[5]])
There are idioms for flattening a list of lists, but since you flagged this a numpy, the numpy approach is more obvious.
In [177]: import itertools
In [178]: list(itertools.chain(*alist))
Out[178]: [1, 2, 3, 4, 5]
In [180]: [[x] for x in itertools.chain(*alist)]
Out[180]: [[1], [2], [3], [4], [5]]
We can quibble about whether this is a list of lists or multidimensional array, but concateante
does a nice job of flattening it into a 1d array:
In [173]: alist = [[1,2], [3,4,5]]
In [175]: np.concatenate(alist, axis=0)
Out[175]: array([1, 2, 3, 4, 5])
Then it's easy to reshape it into a (5,1) shape array:
In [176]: np.concatenate(alist, axis=0).reshape(-1,1)
Out[176]:
array([[1],
[2],
[3],
[4],
[5]])
There are idioms for flattening a list of lists, but since you flagged this a numpy, the numpy approach is more obvious.
In [177]: import itertools
In [178]: list(itertools.chain(*alist))
Out[178]: [1, 2, 3, 4, 5]
In [180]: [[x] for x in itertools.chain(*alist)]
Out[180]: [[1], [2], [3], [4], [5]]
answered Nov 13 '18 at 20:50
hpauljhpaulj
111k776142
111k776142
add a comment |
add a comment |
You can use the x for x in array
mechanism:
>>> a = [[1,2], [3,4,5]]
>>> [[[a2] for a2 in a1] for a1 in a]
[[[1], [2]], [[3], [4], [5]]]
add a comment |
You can use the x for x in array
mechanism:
>>> a = [[1,2], [3,4,5]]
>>> [[[a2] for a2 in a1] for a1 in a]
[[[1], [2]], [[3], [4], [5]]]
add a comment |
You can use the x for x in array
mechanism:
>>> a = [[1,2], [3,4,5]]
>>> [[[a2] for a2 in a1] for a1 in a]
[[[1], [2]], [[3], [4], [5]]]
You can use the x for x in array
mechanism:
>>> a = [[1,2], [3,4,5]]
>>> [[[a2] for a2 in a1] for a1 in a]
[[[1], [2]], [[3], [4], [5]]]
answered Nov 13 '18 at 20:50
mrtumnusmrtumnus
159112
159112
add a comment |
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%2f53289026%2fpython-how-to-reshape-a-multidimensional-array-with-various-dimensions%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
numpy doesn't support ragged arrays like that. are you sure you don't have a list of lists?
– wim
Nov 13 '18 at 20:38
Well, this request is in fact coming from image classification.
– Yao Peng
Nov 14 '18 at 21:22
Sorry I didn't finish yesterday. So when I read image of RGB color into python, it only comes with format like [[1,2], [3,4,5]], however to use keras CNN, the last dimension has to be an array, like [[[1],[2]], [[3],[4],[5]]]. The answers are great if the number of images are relatively small, but when there are 100,000 images of various sizes it could take a while to finish
– Yao Peng
Nov 15 '18 at 15:48