Can I create a numpy array with one variable dimension from a list of numpy arrays?










1















Here is a miminum example of my problem, I have a list of numpy arrays that look like this:



a = np.zeros([4,3])
b = np.ones([5,3])
my_list = [a, b]

my_list
[array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]), array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])]


Arrays in the list could have variable length in one (and only one) of the dimension (here 4 or 5).



What I would like to have eventually is a numpy array that is of dimension (2, "Variable-size", 3). The output should look something like the following:



array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])


np.vstack() or np.concatenate() do not return me the shape I am looking for as they are producing an output that has a (9, 3) shape.










share|improve this question



















  • 3





    The above is not possible since here result[0] has a different shape than result[1]. Numpy typically works with "rectangular" data.

    – Willem Van Onsem
    Nov 13 '18 at 23:20






  • 1





    Show us why you think the desired answer is feasible. Something from the numpy documentation, for example.

    – hpaulj
    Nov 14 '18 at 3:04











  • I am asking if this is feasible, or how I could achieve an equivalent result, does not mean I have gathered any evidence that it is indeed feasible, in fact I haven't

    – Mth Clv
    Nov 14 '18 at 10:41











  • Numpy does not currently, and I believe has no plans to ever, support jagged arrays.

    – PMende
    Nov 14 '18 at 17:31















1















Here is a miminum example of my problem, I have a list of numpy arrays that look like this:



a = np.zeros([4,3])
b = np.ones([5,3])
my_list = [a, b]

my_list
[array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]), array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])]


Arrays in the list could have variable length in one (and only one) of the dimension (here 4 or 5).



What I would like to have eventually is a numpy array that is of dimension (2, "Variable-size", 3). The output should look something like the following:



array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])


np.vstack() or np.concatenate() do not return me the shape I am looking for as they are producing an output that has a (9, 3) shape.










share|improve this question



















  • 3





    The above is not possible since here result[0] has a different shape than result[1]. Numpy typically works with "rectangular" data.

    – Willem Van Onsem
    Nov 13 '18 at 23:20






  • 1





    Show us why you think the desired answer is feasible. Something from the numpy documentation, for example.

    – hpaulj
    Nov 14 '18 at 3:04











  • I am asking if this is feasible, or how I could achieve an equivalent result, does not mean I have gathered any evidence that it is indeed feasible, in fact I haven't

    – Mth Clv
    Nov 14 '18 at 10:41











  • Numpy does not currently, and I believe has no plans to ever, support jagged arrays.

    – PMende
    Nov 14 '18 at 17:31













1












1








1








Here is a miminum example of my problem, I have a list of numpy arrays that look like this:



a = np.zeros([4,3])
b = np.ones([5,3])
my_list = [a, b]

my_list
[array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]), array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])]


Arrays in the list could have variable length in one (and only one) of the dimension (here 4 or 5).



What I would like to have eventually is a numpy array that is of dimension (2, "Variable-size", 3). The output should look something like the following:



array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])


np.vstack() or np.concatenate() do not return me the shape I am looking for as they are producing an output that has a (9, 3) shape.










share|improve this question
















Here is a miminum example of my problem, I have a list of numpy arrays that look like this:



a = np.zeros([4,3])
b = np.ones([5,3])
my_list = [a, b]

my_list
[array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]), array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])]


Arrays in the list could have variable length in one (and only one) of the dimension (here 4 or 5).



What I would like to have eventually is a numpy array that is of dimension (2, "Variable-size", 3). The output should look something like the following:



array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])


np.vstack() or np.concatenate() do not return me the shape I am looking for as they are producing an output that has a (9, 3) shape.







python arrays numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 23:38







Mth Clv

















asked Nov 13 '18 at 23:14









Mth ClvMth Clv

11910




11910







  • 3





    The above is not possible since here result[0] has a different shape than result[1]. Numpy typically works with "rectangular" data.

    – Willem Van Onsem
    Nov 13 '18 at 23:20






  • 1





    Show us why you think the desired answer is feasible. Something from the numpy documentation, for example.

    – hpaulj
    Nov 14 '18 at 3:04











  • I am asking if this is feasible, or how I could achieve an equivalent result, does not mean I have gathered any evidence that it is indeed feasible, in fact I haven't

    – Mth Clv
    Nov 14 '18 at 10:41











  • Numpy does not currently, and I believe has no plans to ever, support jagged arrays.

    – PMende
    Nov 14 '18 at 17:31












  • 3





    The above is not possible since here result[0] has a different shape than result[1]. Numpy typically works with "rectangular" data.

    – Willem Van Onsem
    Nov 13 '18 at 23:20






  • 1





    Show us why you think the desired answer is feasible. Something from the numpy documentation, for example.

    – hpaulj
    Nov 14 '18 at 3:04











  • I am asking if this is feasible, or how I could achieve an equivalent result, does not mean I have gathered any evidence that it is indeed feasible, in fact I haven't

    – Mth Clv
    Nov 14 '18 at 10:41











  • Numpy does not currently, and I believe has no plans to ever, support jagged arrays.

    – PMende
    Nov 14 '18 at 17:31







3




3





The above is not possible since here result[0] has a different shape than result[1]. Numpy typically works with "rectangular" data.

– Willem Van Onsem
Nov 13 '18 at 23:20





The above is not possible since here result[0] has a different shape than result[1]. Numpy typically works with "rectangular" data.

– Willem Van Onsem
Nov 13 '18 at 23:20




1




1





Show us why you think the desired answer is feasible. Something from the numpy documentation, for example.

– hpaulj
Nov 14 '18 at 3:04





Show us why you think the desired answer is feasible. Something from the numpy documentation, for example.

– hpaulj
Nov 14 '18 at 3:04













I am asking if this is feasible, or how I could achieve an equivalent result, does not mean I have gathered any evidence that it is indeed feasible, in fact I haven't

– Mth Clv
Nov 14 '18 at 10:41





I am asking if this is feasible, or how I could achieve an equivalent result, does not mean I have gathered any evidence that it is indeed feasible, in fact I haven't

– Mth Clv
Nov 14 '18 at 10:41













Numpy does not currently, and I believe has no plans to ever, support jagged arrays.

– PMende
Nov 14 '18 at 17:31





Numpy does not currently, and I believe has no plans to ever, support jagged arrays.

– PMende
Nov 14 '18 at 17:31












1 Answer
1






active

oldest

votes


















0














You can't have numpy array with the shape: (2, "Variable-size", 3), but you can concatenate two arrays with the shape ("Variable-size", 3) to (shape1[0] + shape2[0], 3).
As you wrote:




I am looking for as they are producing an output that has a (9, 3) shape




numpy.concatenate()


can be a solution to your problem:



np.concatenate((a,b))


Out:



array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])


If you still want to maintain a variable 3rd dimension in numpy, the only way to do it with padding, here with zero padding:



import numpy as np

a = np.array([
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[0., 0., 0.] # zero padding
])

b = np.array([
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]
])

new_matrix = [a, b]

print(new_matrix)


Out:



[array([[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[ 0., 0., 0.]]), array([[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]])]


This method is usable widely at a sort of image processing solutions.
With this trick you can leverage all the positive properties of matrix operations in numpy too, but you can keep your data shape relatively flexible.
As Willem mentioned above numpy only work with "rectangular" data and operations with variable dimensional matrices would be in the most situation ambiguous.



If you don't even want to use any of the mentioned solution, you have to choose list and numpy array combinations i.e. numpy arrays with any dimensions in a list.






share|improve this answer

























  • Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

    – Mth Clv
    Nov 13 '18 at 23:40











  • @MthClv No problem, see my full answer above, I hope it helps.

    – Geeocode
    Nov 14 '18 at 0:21










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290903%2fcan-i-create-a-numpy-array-with-one-variable-dimension-from-a-list-of-numpy-arra%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









0














You can't have numpy array with the shape: (2, "Variable-size", 3), but you can concatenate two arrays with the shape ("Variable-size", 3) to (shape1[0] + shape2[0], 3).
As you wrote:




I am looking for as they are producing an output that has a (9, 3) shape




numpy.concatenate()


can be a solution to your problem:



np.concatenate((a,b))


Out:



array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])


If you still want to maintain a variable 3rd dimension in numpy, the only way to do it with padding, here with zero padding:



import numpy as np

a = np.array([
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[0., 0., 0.] # zero padding
])

b = np.array([
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]
])

new_matrix = [a, b]

print(new_matrix)


Out:



[array([[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[ 0., 0., 0.]]), array([[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]])]


This method is usable widely at a sort of image processing solutions.
With this trick you can leverage all the positive properties of matrix operations in numpy too, but you can keep your data shape relatively flexible.
As Willem mentioned above numpy only work with "rectangular" data and operations with variable dimensional matrices would be in the most situation ambiguous.



If you don't even want to use any of the mentioned solution, you have to choose list and numpy array combinations i.e. numpy arrays with any dimensions in a list.






share|improve this answer

























  • Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

    – Mth Clv
    Nov 13 '18 at 23:40











  • @MthClv No problem, see my full answer above, I hope it helps.

    – Geeocode
    Nov 14 '18 at 0:21















0














You can't have numpy array with the shape: (2, "Variable-size", 3), but you can concatenate two arrays with the shape ("Variable-size", 3) to (shape1[0] + shape2[0], 3).
As you wrote:




I am looking for as they are producing an output that has a (9, 3) shape




numpy.concatenate()


can be a solution to your problem:



np.concatenate((a,b))


Out:



array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])


If you still want to maintain a variable 3rd dimension in numpy, the only way to do it with padding, here with zero padding:



import numpy as np

a = np.array([
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[0., 0., 0.] # zero padding
])

b = np.array([
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]
])

new_matrix = [a, b]

print(new_matrix)


Out:



[array([[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[ 0., 0., 0.]]), array([[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]])]


This method is usable widely at a sort of image processing solutions.
With this trick you can leverage all the positive properties of matrix operations in numpy too, but you can keep your data shape relatively flexible.
As Willem mentioned above numpy only work with "rectangular" data and operations with variable dimensional matrices would be in the most situation ambiguous.



If you don't even want to use any of the mentioned solution, you have to choose list and numpy array combinations i.e. numpy arrays with any dimensions in a list.






share|improve this answer

























  • Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

    – Mth Clv
    Nov 13 '18 at 23:40











  • @MthClv No problem, see my full answer above, I hope it helps.

    – Geeocode
    Nov 14 '18 at 0:21













0












0








0







You can't have numpy array with the shape: (2, "Variable-size", 3), but you can concatenate two arrays with the shape ("Variable-size", 3) to (shape1[0] + shape2[0], 3).
As you wrote:




I am looking for as they are producing an output that has a (9, 3) shape




numpy.concatenate()


can be a solution to your problem:



np.concatenate((a,b))


Out:



array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])


If you still want to maintain a variable 3rd dimension in numpy, the only way to do it with padding, here with zero padding:



import numpy as np

a = np.array([
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[0., 0., 0.] # zero padding
])

b = np.array([
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]
])

new_matrix = [a, b]

print(new_matrix)


Out:



[array([[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[ 0., 0., 0.]]), array([[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]])]


This method is usable widely at a sort of image processing solutions.
With this trick you can leverage all the positive properties of matrix operations in numpy too, but you can keep your data shape relatively flexible.
As Willem mentioned above numpy only work with "rectangular" data and operations with variable dimensional matrices would be in the most situation ambiguous.



If you don't even want to use any of the mentioned solution, you have to choose list and numpy array combinations i.e. numpy arrays with any dimensions in a list.






share|improve this answer















You can't have numpy array with the shape: (2, "Variable-size", 3), but you can concatenate two arrays with the shape ("Variable-size", 3) to (shape1[0] + shape2[0], 3).
As you wrote:




I am looking for as they are producing an output that has a (9, 3) shape




numpy.concatenate()


can be a solution to your problem:



np.concatenate((a,b))


Out:



array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])


If you still want to maintain a variable 3rd dimension in numpy, the only way to do it with padding, here with zero padding:



import numpy as np

a = np.array([
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[0., 0., 0.] # zero padding
])

b = np.array([
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]
])

new_matrix = [a, b]

print(new_matrix)


Out:



[array([[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[50., 50., 50.],
[ 0., 0., 0.]]), array([[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.],
[80., 80., 80.]])]


This method is usable widely at a sort of image processing solutions.
With this trick you can leverage all the positive properties of matrix operations in numpy too, but you can keep your data shape relatively flexible.
As Willem mentioned above numpy only work with "rectangular" data and operations with variable dimensional matrices would be in the most situation ambiguous.



If you don't even want to use any of the mentioned solution, you have to choose list and numpy array combinations i.e. numpy arrays with any dimensions in a list.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 0:31

























answered Nov 13 '18 at 23:37









GeeocodeGeeocode

2,3061820




2,3061820












  • Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

    – Mth Clv
    Nov 13 '18 at 23:40











  • @MthClv No problem, see my full answer above, I hope it helps.

    – Geeocode
    Nov 14 '18 at 0:21

















  • Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

    – Mth Clv
    Nov 13 '18 at 23:40











  • @MthClv No problem, see my full answer above, I hope it helps.

    – Geeocode
    Nov 14 '18 at 0:21
















Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

– Mth Clv
Nov 13 '18 at 23:40





Sorry if I wasnt clear, but I am not looking for a 2d output, it is crucial for me to keep this variable dimension in the 3d output

– Mth Clv
Nov 13 '18 at 23:40













@MthClv No problem, see my full answer above, I hope it helps.

– Geeocode
Nov 14 '18 at 0:21





@MthClv No problem, see my full answer above, I hope it helps.

– Geeocode
Nov 14 '18 at 0:21

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290903%2fcan-i-create-a-numpy-array-with-one-variable-dimension-from-a-list-of-numpy-arra%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto