Shape of an Array and Reshape() function
Numpy - Shape Attribute and Reshape() Function

Shape of an Array and Reshape() function

Shape of an Numpy array

In Numpy array, shape is an attribute that is used to check the dimension of an array. shape attribute that returns the number of elements in each dimension of an array in numeric as a result.

syntax: array_name.shape

Example

array_1 = np.array([1,2,3,4,5,6])
array_2 = np.array([[0,9,8,7,6], [5,4,3,2,1]])
print("1D array :", array_1.shape)
print("2D array :", array_2.shape)        

Output

1D array : (6,)
2D array : (2, 5)        

  • For 1D array, the shape attribute returns (6, ) as a result because the array in a single dimension.
  • For 2D array, the shape attribute returns (2, 5) as a result because the array in a two dimension that indicates rows and columns respectively.


Reshape() function

It is used to change the number of elements in each dimensional of an array.

syntax: array_name.reshape(attributes)

Example

print("Reshape :", array_1.reshape(3,2))        

Output

Reshape : [[1 2]
 [3 4]
 [5 6]]        

In the above example, we can reshape the 1D array into 2D array that contains 3 rows with 2 elements each.


Reshape 1D and 2D arrays

Example

print("Reshape 1D array :", array_1.reshape(3,2))
print("Reshape 2D array :", array_2.reshape(5,2))        

Output

Reshape 1D array : [[1 2]
 [3 4]
 [5 6]]
Reshape 2D array : [[0 9]
 [8 7]
 [6 5]
 [4 3]
 [2 1]]        

Here, we change the number of elements in each dimensions.


Reshape 1D array into 2D array

By using reshape() functions, we can also change the array from one dimension to another.

Example

print("Reshape 1D array into 2D array :", array_1.reshape(2,3))
print("Reshape 1D array into 3D array :", array_1.reshape(3,1,2))        

Output

Reshape 1D array into 2D array : [[1 2 3]
 [4 5 6]]
Reshape 1D array into 3D array : [[[1 2]]

 [[3 4]]

 [[5 6]]]        

Here, we can reshape the 1D array into 2D and 3D arrays.


Reshape higher dimension array into single dimension array

Example

print("Reshape 2D array into 1D array :", array_2.reshape(10))        

Output

Reshape 2D array into 1D array : [0 9 8 7 6 5 4 3 2 1]        

Another Way

In the above example, the total number of elements to be used for reshape into single dimension array. When the complexity increases, we cannot count the number of elements each and every time. It is very complex and tedious process.

Example

print("Reshape array from multi dimension into single dimension :", array_1.reshape(-1))        

Output

Reshape 1D array : [1 2 3 4 5]        

Here, we can use -1 for reshape the multi dimension array into the single dimension array. when total number of elements is unknown, then -1 can be used. It automatically calculates the total number of elements in a dimension.


Unknown Dimension

Unknown dimension are used when we don't know the dimensions in an array.

Example

print("Reshape 1D array into 2D array with unknown :", array_1.reshape(2,-1))
print("Reshape 1D array into 3D array with unknown :", array_1.reshape(2,1,-1))        

Output

Reshape 1D array into 2D array with unknown : [[1 2 3]
 [4 5 6]]
Reshape 1D array into 3D array with unknown : [[[1 2 3]]

 [[4 5 6]]]        

In the above example, 1D array was reshaped into 2D and 3D arrays using the unknown dimension as -1.

Condition: We can use unknown dimension for only one dimension in an array. If -1 is used more than once, then the interpreter throws an error.

Challenge - Try to reshape the 3D or more dimension arrays.

Follow on LinkedIn - Trillionaire TK

Follow on Instagram - Trillionaire TK

Visit our website - Information Technology

Learn, Code, Practice!!!

To view or add a comment, sign in

More articles by Tharunkumar S

  • Comparison Operations in Numpy Arrays

    Comparison Operations in Numpy As we already know the comparison or relational operations like less than, less than and…

  • Arithmetic Operations in Numpy Arrays

    Arithmetic Operations in Numpy As we already know the arithmetic operations like addition, subtraction, division…

  • Slicing in Numpy Arrays

    What is Slicing in Numpy Array? Slice the array in a particular range of elements. In other words, Make an sub array…

  • Indexing in Numpy Arrays

    What is Indexing in Numpy Array? Access the elements in a Numpy array based on index values. Accessing the elements in…

  • Explore array(), type() and astype() functions

    What is ndarray in Numpy? "ndarray" stands for n-dimensional array. Numpy creates an object for an array in this type.

  • Introduction to Numpy Library in Python

    Introduction NUMPY stands for Numerical Python. Numpy is one of the libraries for the python programming language.

  • Journey of Machine Learning Begins

    Machine Learning Definition : The term Machine Learning was coined by Arthur Samuel in 1959, an American pioneer in the…

Explore content categories