Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python

Numpy Shape, Numpy Reshape and Numpy Transpose in Python

Welcome to yet another tutorial on python numpy arrays. In this post we will understand the concepts of numpy shape, numpy reshape and numpy transpose. All these concepts are related to the dimension of the numpy array and how we can change it. You will need these tricks while manipulating data during your machine learning or data science project.

In [1]:
import numpy as np

Numpy Shape

Shape is an attribute of numpy array that gives the shape of the array. It returns a tuple containing the dimension of the array. Please do note that numpy shape is a method and not a function.

Let us see some examples below –

Example 1: One Dimensional Numpy Array

In [2]:
numpy_array = np.array([5, 12, 8, 21,11])
numpy_array
Out[2]:
array([ 5, 12,  8, 21, 11])
In [3]:
numpy_array.shape
Out[3]:
(5,)

Example 2: Two Dimensional Numpy Array

In [4]:
numpy_array = np.array( [[ 1, 2, 3],
                       [ 4, 2, 5]] )
numpy_array
Out[4]:
array([[1, 2, 3],
       [4, 2, 5]])
In [5]:
numpy_array.shape
Out[5]:
(2, 3)

Example 3: Three Dimensional Numpy Array

In [6]:
numpy_array = np.ones((3,2,4))
numpy_array
Out[6]:
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.]],

       [[1., 1., 1., 1.],
        [1., 1., 1., 1.]]])
In [7]:
numpy_array.shape
Out[7]:
(3, 2, 4)

Numpy Reshape

Numpy Reshape takes a numpy array as input and reshapes its dimension with the same data. Unlike the numpy shape that we discussed above, numpy reshape is actually a function and not an attribute.

Ad
Deep Learning Specialization on Coursera

Syntax

numpy.reshape(a, newshape, order=’C’)

a – It is the array that needs to be reshaped.

newshape – It denotes the new shape of the array. The input is either int or tuple of int.

order (optional) – Signifies how to read/write the elements of the array. By default, the value is ‘C’. Other options are ‘F’ for Fortran-like index order and ‘A’ for read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

Let us now see some examples of numpy reshape to get more clarity.

Example 1: 1-D array to 2-D array

In [8]:
numpy_array = np.array([5, 12, 8, 21])
numpy_array
Out[8]:
array([ 5, 12,  8, 21])
In [9]:
numpy_array.reshape(2,2)
Out[9]:
array([[ 5, 12],
       [ 8, 21]])

Example 2: 1-D array to 3-D array

In [10]:
numpy_array = np.arange(12)
numpy_array
Out[10]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
In [11]:
numpy_array.reshape(3,2,2)
Out[11]:
array([[[ 0,  1],
        [ 2,  3]],

       [[ 4,  5],
        [ 6,  7]],

       [[ 8,  9],
        [10, 11]]])

Example 3: 2-D array to 1-D array

In [12]:
numpy_array = np.zeros((4,3))
numpy_array
Out[12]:
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
In [13]:
numpy_array.reshape(12)
Out[13]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

Example 4: Error Scenario

Do notice that when trying to reshape, the new shape should be able to contain all the data of the original array, else it will throw an error.

In [13]:
numpy_array = np.arange(6)
numpy_array
Out[13]:
array([0, 1, 2, 3, 4, 5])
In [14]:
numpy_array.reshape(2,2)
Out[14]:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-51-8254effee560> in <module>
----> 1 numpy_array.reshape(2,2)

ValueError: cannot reshape array of size 6 into shape (2,2)

Numpy Transpose

Numpy Transpose takes a numpy array as input and transposes the numpy array.

Syntax

numpy.transpose(a, axes=None)

a – It is the array that needs to be transposed.

axes (optional) – It denotes how the axes should be transposed as per the given value. It is the list of numbers denoting the new permutation of axes.

Let us now see some examples of numpy transpose.

Example 1:

You can see below that the numpy array’s rows and columns gets transposed.

In [15]:
numpy_array = np.array( [[ 1, 2, 3],
                       [ 4, 2, 5]] )
numpy_array
Out[15]:
array([[1, 2, 3],
       [4, 2, 5]])
In [16]:
np.transpose(numpy_array)
Out[16]:
array([[1, 4],
       [2, 2],
       [3, 5]])

Example 2:

Numpy transpose has no effect on the 1-D numpy array. It is evident from the below example.

In [17]:
numpy_array = np.array([5, 12, 8, 21])
numpy_array
Out[17]:
array([ 5, 12,  8, 21])
In [18]:
np.transpose(numpy_array)
Out[18]:
array([ 5, 12,  8, 21])

Example 3:

In the below example, we are explicitly passing the axes values for transposing.

In [19]:
numpy_array = np.array( [[ 1, 2, 3],
                       [ 4, 2, 5]] )
numpy_array
Out[19]:
array([[1, 2, 3],
       [4, 2, 5]])
In [20]:
np.transpose(numpy_array, axes=(1,0))
Out[20]:
array([[1, 4],
       [2, 2],
       [3, 5]])

 

Summary

  • Numpy Shape is an attribute that is used to determine the dimension of the numpy array.
  • Numpy Reshape is a function that is used to change the dimension of the numpy array, keeping the same data.
  • Numpy Transpose is a function that is used to transpose the numpy array.

Click Here To Download This Tutorial in Interactive Jupyter Notebook

 

Reference-  https://numpy.org/doc/

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet