Home > Notes > Python Numpy

numpy shape and reshape

Python Numpy

numpy Array (1D and 2’D) creation and operation
numpy array itreating
numpy array join
numpy Array slicing and indexing
numpy array sort and Filter
numpy shape and reshape

numpy shape and reshape

Get the Shape of an Array

NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements

import numpy as np
 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
 print(arr.shape)
'''
The example above returns (2, 4), which means that the array has 2 dimensions, where
 the first dimension has 2 elements and the second has 4
'''
'''
Example
 Create an array with 5 dimensions using ndmin using a vector with values 1,2,3,4 and verify
 that last dimension has value 4:
'''
import numpy as np
 arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
 print('shape of array :', arr.shape)

Reshaping arrays

Reshaping means changing the shape of an array. The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension. Reshape From 1-D to 2-D Example Convert the following 1-D array with 12 elements into a 2-D array. The outermost dimension will have 4 arrays, each with 3 elements:

import numpy as np
 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
 newarr = arr.reshape(4, 3)
 print(newarr)

Reshape From 1-D to 3-D

Example Convert the following 1-D array with 12 elements into a 3-D array. The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
 newarr = arr.reshape(2, 3, 2)
 print(newarr)

Can We Reshape Into any Shape?

Yes, as long as the elements required for reshaping are equal in both shapes. We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3×3 = 9 elements.

 Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension
 (will raise an error):
import numpy as np
 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 newarr = arr.reshape(3, 3)
 print(newarr)

Returns Copy or View?

Check if the returned array is a copy or a view:

 import numpy as np
 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 print(arr.reshape(2, 4).base)
#  Unknown Dimension
'''
Example
 Convert 1D array with 8 elements to 3D array with 2×2 elements:
'''
 import numpy as np
 arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 newarr = arr.reshape(2, 2, -1)
 print(newarr)

Flattening the arrays

Flattening array means converting a multidimensional array into a 1D array. We can use reshape(-1) to do this.

import numpy as np
 arr = np.array([[1, 2, 3], [4, 5, 6]])
 newarr = arr.reshape(-1)
 print(newarr)