Re: [Numpydiscussion] array  dimension size of 1D and 2D examples
Hi Derek I have a related question: Given: a = numpy.array([[0,1,2],[3,4]]) assert a.ndim == 1 b = numpy.array([[0,1,2],[3,4,5]]) assert b.ndim == 2 Is there an elegant way to force b to remain a 1dim object array? I have a use case where normally the sublists are of different lengths, but I get a completely different structure when they are (coincidentally in my case) of the same length. Thanks and best regards, Martin Martin Gfeller, Swisscom / Enterprise / Banking / Products / Quantax Message: 1 Date: Sun, 31 Dec 2017 00:11:48 +0100 From: Derek Homeier <derek@astro.physik.unigoettingen.de> To: Discussion of Numerical Python <numpydiscussion@python.org> Subject: Re: [Numpydiscussion] array  dimension size of 1D and 2D examples MessageID: <CC548593308B4561A03CD3017C707108@astro.physik.unigoettingen.de> ContentType: text/plain; charset=utf8 On 30 Dec 2017, at 5:38 pm, Vinodhini Balusamy <me.vinob@gmail.com> wrote:
Just one more question from the details you have provided which from my understanding strongly seems to be Design [DEREK] You cannot create a regular 2dimensional integer array from one row of length 3
and a second one of length 0. Thus np.array chooses the next most basic type of array it can fit your input data in
Indeed, the general philosophy is to preserve the structure and type of your input data as far as possible, i.e. a list is turned into a 1darray, a list of lists (or tuples etc?) into a 2darray,_ if_ the sequences are of equal length (even if length 1). As long as there is an unambiguous way to convert the data into an array (see below).
Which is the case, only if an second one of length 0 is given. What about the case 1 :
x12 = np.array([[1,2,3]]) x12 array([[1, 2, 3]]) print(x12) [[1 2 3]] x12.ndim 2
This seems to take 2 dimension.
Yes, structurally this is equivalent to your second example
also,
x12 = np.array([[1,2,3],[0,0,0]]) print(x12) [[1 2 3] [0 0 0]] x12.ndim 2
I presumed the above case and the case where length 0 is provided to be treated same(I mean same behaviour). Correct me if I am wrong.
In this case there is no unambiguous way to construct the array  you would need a shape (2, 3) array to store the two lists with 3 elements in the first list. Obviously x12[0] would be np.array([1,2,3]), but what should be the value of x12[1], if the second list is empty  it could be zeros, or repeating x12[0], or simply undefined. np.array([1, 2, 3], [4]]) would be even less clearly defined. These cases where there is no obvious ?right? way to create the array have usually been discussed at some length, but I don?t know if this is fully documented in some place. For the essentials, see https://docs.scipy.org/doc/numpy/reference/routines.arraycreation.html note also the upcasting rules if you have e.g. a mix of integers and reals or complex numbers, and also how to control shape or data type explicitly with the respective keywords. Derek
On Tue, 20180109 at 12:27 +0000, Martin.Gfeller@swisscom.com wrote:
Hi Derek
I have a related question:
Given:
a = numpy.array([[0,1,2],[3,4]]) assert a.ndim == 1 b = numpy.array([[0,1,2],[3,4,5]]) assert b.ndim == 2
Is there an elegant way to force b to remain a 1dim object array?
You will have to create an empty object array and assign the lists to it. ``` b = np.empty(len(l), dtype=object) b[...] = l ```
I have a use case where normally the sublists are of different lengths, but I get a completely different structure when they are (coincidentally in my case) of the same length.
Thanks and best regards, Martin
Martin Gfeller, Swisscom / Enterprise / Banking / Products / Quantax
Message: 1 Date: Sun, 31 Dec 2017 00:11:48 +0100 From: Derek Homeier <derek@astro.physik.unigoettingen.de> To: Discussion of Numerical Python <numpydiscussion@python.org> Subject: Re: [Numpydiscussion] array  dimension size of 1D and 2D examples MessageID: <CC548593308B4561A03CD3017C707108@astro.physik.unigoetting en.de> ContentType: text/plain; charset=utf8
On 30 Dec 2017, at 5:38 pm, Vinodhini Balusamy <me.vinob@gmail.com> wrote:
Just one more question from the details you have provided which from my understanding strongly seems to be Design [DEREK] You cannot create a regular 2dimensional integer array from one row of length 3
and a second one of length 0. Thus np.array chooses the next most basic type of array it can fit your input data in
Indeed, the general philosophy is to preserve the structure and type of your input data as far as possible, i.e. a list is turned into a 1darray, a list of lists (or tuples etc?) into a 2darray,_ if_ the sequences are of equal length (even if length 1). As long as there is an unambiguous way to convert the data into an array (see below).
Which is the case, only if an second one of length 0 is given. What about the case 1 :
x12 = np.array([[1,2,3]]) x12
array([[1, 2, 3]])
print(x12)
[[1 2 3]]
x12.ndim
2
This seems to take 2 dimension.
Yes, structurally this is equivalent to your second example
also,
x12 = np.array([[1,2,3],[0,0,0]]) print(x12)
[[1 2 3] [0 0 0]]
x12.ndim
2
I presumed the above case and the case where length 0 is provided to be treated same(I mean same behaviour). Correct me if I am wrong.
In this case there is no unambiguous way to construct the array  you would need a shape (2, 3) array to store the two lists with 3 elements in the first list. Obviously x12[0] would be np.array([1,2,3]), but what should be the value of x12[1], if the second list is empty  it could be zeros, or repeating x12[0], or simply undefined. np.array([1, 2, 3], [4]]) would be even less clearly defined. These cases where there is no obvious ?right? way to create the array have usually been discussed at some length, but I don?t know if this is fully documented in some place. For the essentials, see
https://docs.scipy.org/doc/numpy/reference/routines.arraycreation.ht ml
note also the upcasting rules if you have e.g. a mix of integers and reals or complex numbers, and also how to control shape or data type explicitly with the respective keywords.
Derek
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@python.org https://mail.python.org/mailman/listinfo/numpydiscussion
participants (2)

Martin.Gfeller＠swisscom.com

Sebastian Berg