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.
Before we start, let us first import the numpy library.
Table of Contents
ToggleImport Numpy Library
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
numpy_array = np.array([5, 12, 8, 21,11])
numpy_array
array([ 5, 12, 8, 21, 11])
numpy_array.shape
(5,)
Example 2: Two Dimensional Numpy Array
numpy_array = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
numpy_array
array([[1, 2, 3], [4, 2, 5]])
numpy_array.shape
(2, 3)
Example 3: Three Dimensional Numpy Array
numpy_array = np.ones((3,2,4))
numpy_array
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.]]])
numpy_array.shape
(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.
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
numpy_array = np.array([5, 12, 8, 21])
numpy_array
array([ 5, 12, 8, 21])
numpy_array.reshape(2,2)
array([[ 5, 12], [ 8, 21]])
Example 2: 1-D array to 3-D array
numpy_array = np.arange(12)
numpy_array
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
numpy_array.reshape(3,2,2)
array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]]])
Example 3: 2-D array to 1-D array
numpy_array = np.zeros((4,3))
numpy_array
array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
numpy_array.reshape(12)
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.
numpy_array = np.arange(6)
numpy_array
array([0, 1, 2, 3, 4, 5])
numpy_array.reshape(2,2)
--------------------------------------------------------------------------- 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.
numpy_array = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
numpy_array
array([[1, 2, 3], [4, 2, 5]])
np.transpose(numpy_array)
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.
numpy_array = np.array([5, 12, 8, 21])
numpy_array
array([ 5, 12, 8, 21])
np.transpose(numpy_array)
array([ 5, 12, 8, 21])
Example 3:
In the below example, we are explicitly passing the axes values for transposing.
numpy_array = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
numpy_array
array([[1, 2, 3], [4, 2, 5]])
np.transpose(numpy_array, axes=(1,0))
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/
- Also Read – Python Numpy Array – A Gentle Introduction to beginners
- Also Read – Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python
- Also Read – Complete Numpy Random Tutorial – Rand, Randn, Randint, Normal