Difference between array( [1,0,1] ) and array( [ [1,0,1] ] )

Markos markos at c2o.pro.br
Sat Jun 22 11:20:54 EDT 2019


Thanks Edmondo, Stephen, Mats and Steven you for the tips,

I studied linear algebra many years ago and I remember only a few rudiments.

But I was trying to visualize (in a geometric way) how the numpy 
represents arrays, and what the geometrical meaning of the transpose 
operation made by numpy.

I think I understood a little bit more.

The number of nested brackets indicates the number of array dimensions.
the vector ( [1,2] ) is one-dimensional, but the vector ( [ [1,2] ] ) is 
two-dimensional.

v_1 = np.array( [1,2] )
 > v_1.shape
(2,)
 > v_1
v_1
 > v_1
array( [1, 2] )
 > v_2 = np.array( [ [1,2] ] )
 > v_2.shape
(1, 2)

And it does not make sense to transpose a one-dimensional array.

 > v_1.T
array( [1, 2] )
 > v_2.T
array( [ [1],
              [2] ] )

Anothe example:

vector_1 = np.array( [   1,   2,   3,   4,   5,   6,   7,   8  ] )

                                   ^

vector_2 = np.array( [    [1, 2, 3, 4],    [5, 6, 7, 8]  ]  )

                                   ^  ^

vector_3 = np.array( [   [   [1,2],  [3,4]  ], [  [5,6],   [7,8] ]  ]  )

                                   ^ ^ ^

 > vector_1
array([1, 2, 3, 4, 5, 6, 7, 8])
 > vector_2
array( [ [1, 2, 3, 4],
              [5, 6, 7, 8] ] )
 > vector_3
array( [ [ [1, 2],
                [3, 4] ],

              [ [5, 6],
                [7, 8] ] ] )

And looking for some tutorial about geometric aspects of matrices and 
the geometric meaning of the transpose I found that transposed is 
"mirrored along the diagonal" at:

https://www.coranac.com/documents/geomatrix/

 >vector_1.T
array([1, 2, 3, 4, 5, 6, 7, 8])
 > vector_2.T
array( [ [1, 5],
              [2, 6],
              [3, 7],
              [4, 8] ] )
 > vector_3.T
array( [ [ [1, 5],
                [3, 7]],

              [ [2, 6],
                [4, 8] ] ] )

Thank you,
Markos

Em 21-06-2019 07:44, edmondo.giovannozzi at gmail.com escreveu:
> Every array in numpy has a number of dimensions,
> "np.array" is a function that can create an array numpy given a list.
>
> when  you write
> vector_1  = np.array([1,2,1])
> you are passing a list of number to thet function array that will create a 1D array.
> As you are showing:
> vector_1.shape
> will return a tuple with the sizes of each dimension of the array that is:
> (3,)
> Note the comma thta indicate that is a tuple.
> While if you write:
> vector_2 = np.array([[1,2,3]])
> You are passing a list of list to the function array that will instruct it to crete a 2D array, even though the size of the first dimension is 1:
> vector_2.shape
> (1,3)
> It is still a tuple as you can see.
> Try:
> vector_3 = np.array([[1,2,3],[4,5,6]])
> And you'll see that i'll return a 2D array with a shape:
> vector_3.shape
> (2,3)
> As the external list has 2 elements that is two sublists each with 3 elements.
> The vector_2 case is just when the external list has only 1 element.
>
> I hope it is more clear now.
> Cherrs,
>
>    
>
>    
>    
>
> Il giorno venerdì 21 giugno 2019 08:29:36 UTC+2, Markos ha scritto:
>> Hi,
>>
>> I'm studying Numpy and I don't understand the difference between
>>
>>>>> vector_1 = np.array( [ 1,0,1 ] )
>> with 1 bracket and
>>
>>>>> vector_2 = np.array( [ [ 1,0,1 ] ] )
>> with 2 brackets
>>
>> The shape of vector_1 is:
>>
>>>>> vector_1.shape
>> (3,)
>>
>> But the shape of vector_2 is:
>>
>>>>> vector_2.shape
>> (1, 3)
>>
>> The transpose on vector_1 don't work:
>>
>>>>> vector_1.T
>> array([1, 0, 1])
>>
>> But the transpose method in vector_2 works fine:
>>
>>>>> vector_2.T
>> array([[1],
>>          [0],
>>          [1]])
>>
>>
>> I thought that both vectors would be treated as an matrix of 1 row and 3
>> columns.
>>
>> Why this difference?
>>
>> Any tip?
>>
>> Thank you,
>> Markos




More information about the Python-list mailing list