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 1-dim 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 <[email protected]>
To: Discussion of Numerical Python <[email protected]>
Subject: Re: [Numpy-discussion] array - dimension size of 1-D and 2-D
examples
Message-ID:
<cc548593-308b-4561-a03c-d3017c707...@astro.physik.uni-goettingen.de>
Content-Type: text/plain; charset=utf-8
On 30 Dec 2017, at 5:38 pm, Vinodhini Balusamy <[email protected]> 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 2-dimensional 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 1d-array, a list of
lists (or tuples etc?) into a 2d-array,_ 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.array-creation.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
_______________________________________________
NumPy-Discussion mailing list
[email protected]
https://mail.python.org/mailman/listinfo/numpy-discussion