NumPy Shape and Reshape Practice Questions with Solutions

Introduction

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()

11. Python Program to Display the Shape, Size, and Dimensions of a NumPy Array

Problem Statement

Write a Python program to create a 3×4 NumPy array and display its shape, size, number of dimensions, and data type.

Python Solution

import numpy as np

# Create a 3×4 array
array = np.array([
    [10, 20, 30, 40],
    [50, 60, 70, 80],
    [90, 100, 110, 120]
])

print("Original Array:")
print(array)

print("\nShape:", array.shape)
print("Size:", array.size)
print("Dimensions:", array.ndim)
print("Data Type:", array.dtype)

Sample Output

Original Array:
[[ 10  20  30  40]
 [ 50  60  70  80]
 [ 90 100 110 120]]

Shape: (3, 4)
Size: 12
Dimensions: 2
Data Type: int64

Note: The exact data type (int32 or int64) depends on your operating system and Python installation.

Explanation

The program demonstrates commonly used NumPy array attributes:

  • shape → Returns rows and columns.
  • size → Returns the total number of elements.
  • ndim → Returns the number of dimensions.
  • dtype → Returns the data type of array elements.

Concepts Covered

  • shape
  • size
  • ndim
  • dtype
  • Array Properties

12. Python Program to Reshape a One-Dimensional Array into Multiple Dimensions

Problem Statement

Write a Python program to create a one-dimensional array containing numbers from 1 to 24 and reshape it into:

  • 4×6 matrix
  • 6×4 matrix
  • 2×3×4 three-dimensional array

Python Solution

import numpy as np

numbers = np.arange(1, 25)

print("Original Array:")
print(numbers)

print("\nReshape into 4×6:")
print(numbers.reshape(4, 6))

print("\nReshape into 6×4:")
print(numbers.reshape(6, 4))

print("\nReshape into 2×3×4:")
print(numbers.reshape(2, 3, 4))

Sample Output

Original Array:
[ 1  2  3 ... 24]

Reshape into 4×6:
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]]

Reshape into 6×4:
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]
 [17 18 19 20]
 [21 22 23 24]]

Reshape into 2×3×4:
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

 [[13 14 15 16]
  [17 18 19 20]
  [21 22 23 24]]]

Explanation

The reshape() method reorganizes the data into different dimensions without changing the values.

Concepts Covered

  • reshape()
  • 1D Array
  • 2D Array
  • 3D Array

13. Python Program to Automatically Calculate One Dimension Using reshape(-1)

Problem Statement

Write a Python program that creates an array of 30 elements and reshapes it into:

  • 5 rows with automatic columns
  • Automatic rows with 6 columns

Python Solution

import numpy as np

array = np.arange(1, 31)

print("Original Array:")
print(array)

print("\n5 Rows:")
print(array.reshape(5, -1))

print("\n6 Columns:")
print(array.reshape(-1, 6))

Sample Output

5 Rows:
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]
 [25 26 27 28 29 30]]

6 Columns:
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]
 [25 26 27 28 29 30]]

Explanation

Using -1 allows NumPy to automatically determine the missing dimension based on the total number of elements.

Concepts Covered

  • reshape(-1)
  • Automatic Dimension Calculation
  • Matrix Transformation

14. Python Program to Flatten a Multi-Dimensional Array

Problem Statement

Write a Python program to convert a 3×3 matrix into a one-dimensional array using both flatten() and reshape().

Python Solution

import numpy as np

matrix = np.array([
    [11, 22, 33],
    [44, 55, 66],
    [77, 88, 99]
])

print("Original Matrix:")
print(matrix)

print("\nUsing flatten():")
print(matrix.flatten())

print("\nUsing reshape(-1):")
print(matrix.reshape(-1))

Sample Output

Original Matrix:
[[11 22 33]
 [44 55 66]
 [77 88 99]]

Using flatten():
[11 22 33 44 55 66 77 88 99]

Using reshape(-1):
[11 22 33 44 55 66 77 88 99]

Explanation

Both methods convert a multi-dimensional array into a one-dimensional array.

  • flatten() returns a copy.
  • reshape(-1) returns a reshaped view whenever possible.

Concepts Covered

  • flatten()
  • reshape(-1)
  • 1D Arrays
  • Matrix Conversion

15. Python Program to Verify Whether an Array Can Be Reshaped

Problem Statement

Write a Python program that checks whether a NumPy array containing 18 elements can be reshaped into different dimensions. If possible, reshape the array; otherwise, display an appropriate message.

Python Solution

import numpy as np

array = np.arange(1, 19)

rows = int(input("Enter Number of Rows: "))
columns = int(input("Enter Number of Columns: "))

if rows * columns == array.size:

    reshaped_array = array.reshape(rows, columns)

    print("\nReshaped Array:")
    print(reshaped_array)

else:

    print("\nReshape is not possible because the dimensions do not match the total number of elements.")

Sample Output

Enter Number of Rows: 3
Enter Number of Columns: 6

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

Sample Output (Invalid Case)

Enter Number of Rows: 5
Enter Number of Columns: 5

Reshape is not possible because the dimensions do not match the total number of elements.

Explanation

Before reshaping, the program verifies that:

rows × columns = total number of elements

If this condition is not satisfied, NumPy cannot reshape the array, so the program displays a meaningful message instead of causing an error.

Concepts Covered

  • reshape()
  • Array Validation
  • size Attribute
  • User Input
  • Matrix Transformation

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