Why use arrays of the form (x,) and not (x, 1)?

I recently ran into several errors due to numpy arrays being in the form (x,) - these can be easily resolved with the snippet below

a = np.array([1,2,3,4]) #this form produced a bug
a.shape 
>>> (4,)  
a.shape = [4,1] #but this change fixed it 

      

But I'm wondering why is (x,) the default shape for 1D arrays?

+3


source to share


3 answers


Each element in the tuple shape

denotes an axis. When you have one element, it means your array is 1 size (1 axis), otherwise it will be a 2D array. When you do a.shape = [4,1]

, you are simply converting your 1D array to 2D:



In [26]: a = np.array([1,2,3,4])
In [27]: a.shape = [4,1]

In [28]: a.shape        
Out[28]: (4, 1)

In [29]: a
Out[29]: 
array([[1],
       [2],
       [3],
       [4]])

      

+2


source


I suspect this question is appropriate because you come from a Matlab background in which everything is treated as a matrix. In Matlab, all 1D datasets are treated as either a row or a column vector, and the indexing is short-circuited so that specifying one index is treated as 1D lists.

Numpy is not about matrices per se, but rather nested lists. Lists of lists have a similar interpretation of Matlab matrices, but there are key differences. For example, Numpy will not make any assumptions about which element you mean if you only specify one index, indexing always acts the same regardless of the depth of the nested lists.



import numpy as np

arr = np.array([1, 2, 3, 4])
print(arr)
>> [1 2 3 4]
print(arr[0])
>> 1

arr.shape = [4, 1]
print(arr)
>> [[1]
>>  [2]
>>  [3]
>>  [4]]
print(arr[0])
>> [1]

arr.shape = [1, 4]
print(arr)
>> [[1 2 3 4]]
print(arr[0])
>> [1 2 3 4]

      

+1


source


Quoting from the documentation:

shape: tuple ints The
   elements of the shape tuple give lengths corresponding to the dimensions of the array.

So when you have a shape like (4, )

this it means that there are 4 elements in its first dimension. Does it make sense right, like from your example?

Conversely, if we have a shape like you say how (4, 1)

, then that means the first dimension (axis = 1, in NumPy lingo) has 4 elements and the second dimension (in NumPy lingo, axis = 0) has 1 element in it which doesn't matter (for 1D arrays)

0


source







All Articles