Saturday, November 16, 2024
Google search engine
HomeLanguagesReshape NumPy Array

Reshape NumPy Array

NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy is basically used for creating array of n dimensions.
Reshaping numpy array simply means changing the shape of the given array, shape basically tells the number of elements and dimension of array, by reshaping an array we can add or remove dimensions or change number of elements in each dimension.
In order to reshape a numpy array we use reshape method with the given array. 
 

Syntax : array.reshape(shape)
Argument : It take tuple as argument, tuple is the new shape to be formed
Return : It returns numpy.ndarray
 

Note : We can also use np.reshape(array, shape) command to reshape the array
Reshaping : 1-D to 2D 
In this example we will reshape the 1-D array of shape (1, n) to 2-D array of shape (N, M) here M should be equal to the n/N there for N should be factor of n. 
 

Python3




# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
# length of array
n = array.size
 
# N-D array N dimension
N = 4
 
# calculating M
M = n//N
 
# reshaping numpy array
# converting it to 2-D from 1-D array
reshaped1 = array.reshape((N, M))
 
# printing reshaped array
print("First Reshaped Array : ")
print(reshaped1)
 
# creating another reshaped array
reshaped2 = np.reshape(array, (2, 8))
 
# printing reshaped array
print("Second Reshaped Array : ")
print(reshaped2)


Output : 
 

Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
First Reshaped Array : 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
Second Reshaped Array : 
[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]

Reshaping : 1-D to 3-D 
In this we will see how we can reshape a 1-D array to 3-D dimension array. A 3-D array is the 1-D array of 2-D arrays. 
 

Python3




# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
 
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped = array.reshape((2, 2, 4))
 
# printing reshaped array
print("Reshaped 3-D Array : ")
print(reshaped)


Output : 
 

Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
Reshaped 3-D Array : 
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]

Reshaping N-D to 1-D array 
In this example we will see how we can reshape a 2-D or 3-D array to form a 1-D array. We can also use reshape(-1) to do this, here -1 is the unknown dimension. 
 

Python3




# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
 
# printing array
print(" 2-D Array : ")
print(array)
 
 
# reshaping numpy array
# converting it to 1-D from 2-D array
reshaped = array.reshape((9))
 
# or we can use unknown dimension
# reshaped = array.reshape((-1))
 
# printing reshaped array
print("Reshaped 1-D Array : ")
print(reshaped)


Output : 
 

 2-D Array : 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Reshaped 1-D Array : 
[[1 2 3 4 5 6 7 8 9]]

Reshaping using unknown dimension 
We can reshape a array although we don’t know all the new dimensions by using -1 as one of the dimension, but we should know all the other dimension to use unknown dimension 
 

Python3




# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
 
# printing array
print("Array : " + str(array))
 
 
# reshaping numpy array
# converting it to 3-D from 1-D array
reshaped1 = array.reshape((2, 2, -1))
 
# printing reshaped array
print("First Reshaped Array : ")
print(reshaped1)
 
 
# converting it to 2-D array
reshaped2 = array.reshape((4, -1))
 
# printing reshaped array
print("Second Reshaped Array : ")
print(reshaped2)


Output : 
 

Array : [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
First Reshaped Array : 
[[[ 1  2  3  4]
  [ 5  6  7  8]]

 [[ 9 10 11 12]
  [13 14 15 16]]]
Second Reshaped Array : 
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]

Errors Occur during reshaping 
When we try to reshape a array to a shape which is not mathematically possible then value error is generated saying can not reshape the array. For example when we try to reshape 1-D array with 4 elements into a 2-D array of dimension(3, 3) is not possible as new array requires 9 elements 
 

Python3




# importing numpy
import numpy as np
 
# creating a numpy array
array = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
 
# printing array
print(" 2-D Array : ")
print(array)
 
 
# reshaping numpy array
# converting it to 1-D from 2-D array
# reshaping it into 1, 5
reshaped = array.reshape((1, 5))
 
# or we can use
 
# printing reshaped array
print("Reshaped 1-D Array : ")
print(reshaped)


ValueError: cannot reshape array of size 9 into shape (1, 5)

RELATED ARTICLES

Most Popular

Recent Comments