NumPy Shape and Reshape Practice Questions with Solutions

Understanding shape and reshape is essential when working with NumPy arrays. The shape attribute helps you identify the dimensions of an array, while the reshape() method allows you to change the structure of an array without modifying its data. In this chapter, you’ll practice the most important NumPy shape and reshape questions with complete solutions. NumPy Shape and Reshape practice questions with solutions help to gain concepts.


1. Python Program to Find the Shape of a NumPy Array

Problem Statement

Write a Python program to print the shape of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [10, 20, 30],
    [40, 50, 60]
])

print(numbers.shape)

Sample Output

(2, 3)

Explanation

The shape attribute returns the number of rows and columns in a NumPy array.

Concepts Covered

  • shape
  • Array Dimensions

2. Python Program to Reshape a 1D Array into a 2D Array

Problem Statement

Write a Python program to convert a one-dimensional array into a two-dimensional array with 2 rows and 3 columns.

Python Solution

import numpy as np

numbers = np.array([1, 2, 3, 4, 5, 6])

result = numbers.reshape(2, 3)

print(result)

Sample Output

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

Explanation

The reshape() method changes the dimensions of an array while keeping the same data.

Concepts Covered

  • reshape()
  • 1D to 2D Conversion

3. Python Program to Reshape a 1D Array into a 3D Array

Problem Statement

Write a Python program to reshape a one-dimensional array into a three-dimensional array.

Python Solution

import numpy as np

numbers = np.array([1, 2, 3, 4, 5, 6, 7, 8])

result = numbers.reshape(2, 2, 2)

print(result)

Sample Output

[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

Explanation

The total number of elements must remain the same after reshaping.

Concepts Covered

  • 3D Arrays
  • reshape()

4. Python Program to Flatten a 2D Array

Problem Statement

Write a Python program to convert a two-dimensional array into a one-dimensional array.

Python Solution

import numpy as np

numbers = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(numbers.flatten())

Sample Output

[1 2 3 4 5 6]

Explanation

The flatten() method converts a multi-dimensional array into a one-dimensional array.

Concepts Covered

  • flatten()
  • 2D to 1D Conversion

5. Python Program to Check the Number of Dimensions

Problem Statement

Write a Python program to print the number of dimensions of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [10, 20],
    [30, 40]
])

print(numbers.ndim)

Sample Output

2

Explanation

The ndim attribute returns the number of dimensions in an array.

Concepts Covered

  • ndim
  • Array Dimensions

6. Python Program to Reshape an Array into One Row

Problem Statement

Write a Python program to reshape a one-dimensional array into a single-row array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40])

print(numbers.reshape(1, 4))

Sample Output

[[10 20 30 40]]

Explanation

The reshape() function can create arrays with different row and column combinations.

Concepts Covered

  • Single Row Array
  • reshape()

7. Python Program to Reshape an Array into One Column

Problem Statement

Write a Python program to reshape a one-dimensional array into a single-column array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40])

print(numbers.reshape(4, 1))

Sample Output

[[10]
 [20]
 [30]
 [40]]

Explanation

A single-column array is useful in machine learning and data preprocessing.

Concepts Covered

  • Single Column Array
  • reshape()

8. Python Program to Automatically Calculate One Dimension

Problem Statement

Write a Python program to reshape an array using -1.

Python Solution

import numpy as np

numbers = np.array([1, 2, 3, 4, 5, 6])

print(numbers.reshape(2, -1))

Sample Output

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

Explanation

Using -1 allows NumPy to automatically calculate the required dimension.

Concepts Covered

  • Automatic Reshape
  • -1 in reshape()

9. Python Program to Print the Size of an Array

Problem Statement

Write a Python program to print the total number of elements in a NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [10, 20, 30],
    [40, 50, 60]
])

print(numbers.size)

Sample Output

6

Explanation

The size attribute returns the total number of elements present in the array.

Concepts Covered

  • size
  • Total Elements

10. Python Program to Print the Shape After Reshaping

Problem Statement

Write a Python program to reshape an array and print its new shape.

Python Solution

import numpy as np

numbers = np.array([1, 2, 3, 4, 5, 6])

result = numbers.reshape(3, 2)

print(result.shape)

Sample Output

(3, 2)

Explanation

The shape attribute confirms whether the array has been reshaped successfully.

Concepts Covered

  • shape
  • reshape()

Chapter Summary

In this chapter, you learned how to find the shape of a NumPy array, change its dimensions using reshape(), flatten multi-dimensional arrays, calculate array size, and work with different array structures. These concepts are essential for preparing data for analysis and machine learning.


Key Takeaways

  • The shape attribute returns the dimensions of an array.
  • The reshape() method changes the structure of an array without changing its data.
  • The total number of elements must remain the same after reshaping.
  • The flatten() method converts multi-dimensional arrays into one-dimensional arrays.
  • The ndim attribute returns the number of dimensions.
  • The size attribute returns the total number of elements.
  • Using -1 in reshape() lets NumPy automatically calculate one dimension.

Frequently Asked Questions (FAQs)

1. What is the shape attribute in NumPy?

The shape attribute returns a tuple representing the dimensions of a NumPy array.


2. What is the purpose of the reshape() method?

The reshape() method changes the dimensions of an array without modifying its data.


3. Can I reshape an array into any size?

No. The total number of elements before and after reshaping must remain the same.


4. What does reshape(-1) mean in NumPy?

Using -1 tells NumPy to automatically calculate the required dimension based on the total number of elements.


5. What is the difference between shape and size?

  • shape returns the dimensions of an array.
  • size returns the total number of elements in the array.

6. What is the use of the flatten() method?

The flatten() method converts a multi-dimensional array into a one-dimensional array.


7. Why are shape and reshape important in Data Science?

Shape and reshape operations are widely used in data preprocessing, machine learning, deep learning, and data analysis to organize data into the required format.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top