array  dimension size of 1D and 2D examples
Hi, I am new to bumpy and started working on basics. I need clarification for the below behaviour. case 1:
x12 = np.array([[1,2,3]]) print(x12) [[1 2 3]] x12.ndim 2
Tried creating a 2D array.
also,
x12 = np.array([[1,2,3],[0,0,0]]) print(x12) [[1 2 3] [0 0 0]] x12.ndim 2
Case 2:
x12 = np.array([[1,2,3],[]]) x12.ndim
1
print(x12) [list([1, 2, 3]) list([])]
In case 2, I am trying to understand why it becomes 1 dimentional ?!?!
Case 3:
x12 = np.array([1,2,3]) x12.ndim
1
print(x12) [1 2 3]
This seems reasonable to me to be considered as 1 dimensional.
Would like to understand case 2 a bit more to get to know if i am missing something. Will be much appreciated if someone to explain me a bit. Thanks in advance. Regards, Vinodhini B
On 30 Dec 2017, at 11:37 am, Vinodhini Balusamy
Case 2:
x12 = np.array([[1,2,3],[]]) x12.ndim
1
print(x12) [list([1, 2, 3]) list([])]
In case 2, I am trying to understand why it becomes 1 dimentional ?!?!
Case 3:
x12 = np.array([1,2,3]) x12.ndim
1
print(x12) [1 2 3]
This seems reasonable to me to be considered as 1 dimensional.
Would like to understand case 2 a bit more to get to know if i am missing something. Will be much appreciated if someone to explain me a bit.
Welcome to the crowd! 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  you will notice in case 2 the array actually has two elements of type ‘list’, and you can verify that In [1]: x12 = np.array([[1,2,3],[]]) In [2]: x12.dtype Out[2]: dtype('O') In [3]: x12.shape Out[3]: (2,) i.e. it has created an array of dtype ‘object’, which is probably not what you expected (and nothing you could perform standard arithmetic operations on: In [4]: x12+1 TypeError: can only concatenate list (not "int") to list HTH Derek
Thanks Derek for quick clarification. 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
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. 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.
Also, could u please point out any documentation to understand the logic behind creating elements of type list in case 2(with second grid of length 0) ? If possible. I am curious to understand. Kind Rgds, Vinodhini B
On 30 Dec 2017, at 11:36 PM, Derek Homeier
wrote: On 30 Dec 2017, at 11:37 am, Vinodhini Balusamy
wrote: Case 2:
x12 = np.array([[1,2,3],[]]) x12.ndim
1
print(x12) [list([1, 2, 3]) list([])]
In case 2, I am trying to understand why it becomes 1 dimentional ?!?!
Case 3:
x12 = np.array([1,2,3]) x12.ndim
1
print(x12) [1 2 3]
This seems reasonable to me to be considered as 1 dimensional.
Would like to understand case 2 a bit more to get to know if i am missing something. Will be much appreciated if someone to explain me a bit.
Welcome to the crowd! 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  you will notice in case 2 the array actually has two elements of type ‘list’, and you can verify that
In [1]: x12 = np.array([[1,2,3],[]]) In [2]: x12.dtype Out[2]: dtype('O') In [3]: x12.shape Out[3]: (2,)
i.e. it has created an array of dtype ‘object’, which is probably not what you expected (and nothing you could perform standard arithmetic operations on:
In [4]: x12+1 TypeError: can only concatenate list (not "int") to list
HTH Derek
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@python.org https://mail.python.org/mailman/listinfo/numpydiscussion
On 30 Dec 2017, at 5:38 pm, Vinodhini Balusamy
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
Missed this mail. Thanks Derek For the clarification provided. Kind Rgds, Vinodhini
On 31 Dec 2017, at 10:11 am, Derek Homeier
wrote: On 30 Dec 2017, at 5:38 pm, Vinodhini Balusamy
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
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@python.org https://mail.python.org/mailman/listinfo/numpydiscussion
participants (2)

Derek Homeier

Vinodhini Balusamy