Welcome Guys,

We will be looking at Array transposition in this quick notes.

This is part of lectures on Learning Python for Data Analysis and Visualization by Jose Portilla on Udemy.

 

In [1]:
import numpy as np

 

In [2]:
arr = np.arange(50)
arr

 

Out[2]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
In [17]:
arr.reshape((10,5))

 

Out[17]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44],
       [45, 46, 47, 48, 49]])
In [10]:
arr.T

 

Out[10]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
In [11]:
np.dot(arr.T, arr)

 

Out[11]:
40425
In [23]:
arr3d = np.arange(50).reshape((5,5,12))

 

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-8908d625ec66> in <module>()
----> 1 arr3d = np.arange(50).reshape((5,5,12))

ValueError: total size of new array must be unchanged
In [33]:
arr3d

 

Out[33]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9]],

       [[10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29]],

       [[30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],

       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49]]])
In [41]:
arr2d2 = np.arange(40).reshape((4,5,2))

 

In [42]:
arr2d2

 

Out[42]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9]],

       [[10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29]],

       [[30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]]])
In [46]:
arr2d2.transpose((1,0,2))

 

Out[46]:
array([[[ 0,  1],
        [10, 11],
        [20, 21],
        [30, 31]],

       [[ 2,  3],
        [12, 13],
        [22, 23],
        [32, 33]],

       [[ 4,  5],
        [14, 15],
        [24, 25],
        [34, 35]],

       [[ 6,  7],
        [16, 17],
        [26, 27],
        [36, 37]],

       [[ 8,  9],
        [18, 19],
        [28, 29],
        [38, 39]]])
In [47]:
arr2e = np.array([[1,2,3]])

 

In [48]:
arr2e.swapaxes(0,1)

 

Out[48]:
array([[1],
       [2],
       [3]])

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *