NumPy Array Shape

Introduction

In NumPy Array Shape, the shape of an array tells us how many elements are present along each dimension. It helps us understand the structure of an array, such as the number of rows and columns. The shape attribute is especially useful when working with tables, student records, and company data.

1. What is the Shape of a NumPy Array?

The shape attribute returns a tuple containing the size of each dimension in an array.

Example:

import numpy as np

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

print(arr.shape)

Output:

(4,)

Explanation:

  • The array contains 4 elements.
  • It has only one dimension.
  • The comma indicates that the result is a tuple.

2. Shape of a 1-D Array

A 1-D array contains elements in a single dimension. Its shape tells us the total number of elements.

Example:

import numpy as np

marks = np.array([75, 80, 85, 90, 95])

print(marks.shape)

Output:

(5,)

Explanation:

The output (5,) means the array has 5 elements in one dimension.

3. Shape of a 2-D Array

A 2-D array contains rows and columns. Its shape returns the number of rows first and the number of columns second.

Example:

import numpy as np

marks = np.array([
    [75, 80, 85],
    [70, 90, 95]
])

print(marks.shape)

Output:

(2, 3)

Explanation:

  • 2 represents the number of rows.
  • 3 represents the number of columns.

Therefore, this array has 2 rows and 3 columns.

4. Shape of a 3-D Array

A 3-D array contains multiple 2-D arrays. Its shape returns three values representing the number of groups, rows, and columns.

Example:

import numpy as np

sales = np.array([
    [
        [1000, 1200, 1500],
        [2000, 2200, 2500]
    ],
    [
        [3000, 3200, 3500],
        [4000, 4200, 4500]
    ]
])

print(sales.shape)

Output:

(2, 2, 3)

Explanation:

  • 2 represents the number of 2-D arrays (groups).
  • 2 represents the number of rows in each group.
  • 3 represents the number of columns in each row.

5. Access Individual Values from Shape

The shape attribute returns a tuple, so you can access its individual values using indexing.

Example:

import numpy as np

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

print("Rows:", arr.shape[0])
print("Columns:", arr.shape[1])

Output:

Rows: 2
Columns: 3

Explanation:

  • shape[0] returns the number of rows.
  • shape[1] returns the number of columns.

For a 3-D array, you can also access the third value using shape[2].

6. Difference Between Shape and Size

Both shape and size provide information about an array, but they return different results.

ShapeSize
Returns the size of each dimensionReturns the total number of elements
Gives a tupleGives an integer
Uses shapeUses size

Example:

import numpy as np

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

print("Shape:", arr.shape)
print("Size:", arr.size)

Output:

Shape: (2, 3)
Size: 6

Explanation:

The array has 2 rows and 3 columns, so its shape is (2, 3). It contains 6 elements in total, so its size is 6.

Student Task

A school stores marks for 3 students in 4 subjects.

import numpy as np

marks = np.array([
    [75, 80, 85, 90],
    [65, 70, 75, 80],
    [88, 92, 85, 95]
])

Complete these tasks:

  1. Find the shape of the marks array.
  2. Find the number of rows.
  3. Find the number of columns.
  4. Find the total number of elements.
  5. Print the number of rows and columns separately.

Final Student Challenge: Company Sales Records

A company stores sales records for 2 regions. Each region has 3 employees, and each employee has sales figures for 4 months.

import numpy as np

sales = np.array([
    [
        [10000, 12000, 15000, 18000],
        [11000, 13000, 16000, 19000],
        [9000,  14000, 17000, 20000]
    ],
    [
        [15000, 16000, 18000, 21000],
        [12000, 14000, 17000, 19000],
        [13000, 15000, 18000, 22000]
    ]
])

print("Array Shape:", sales.shape)
print("Number of Regions:", sales.shape[0])
print("Employees per Region:", sales.shape[1])
print("Months:", sales.shape[2])
print("Total Sales Entries:", sales.size)

Expected Output:

Array Shape: (2, 3, 4)
Number of Regions: 2
Employees per Region: 3
Months: 4
Total Sales Entries: 24

Your Challenge:

  1. Create a 2-D array containing 4 employees and 3 monthly sales values.
  2. Find its shape.
  3. Print the number of employees and months separately.
  4. Calculate the total number of sales entries using size.
  5. Create a 3-D array for 3 regions, with 2 employees in each region and 4 monthly sales values for each employee. Find its shape.

Key Takeaways

  • The shape attribute returns the dimensions of an array as a tuple.
  • A 1-D array returns a shape such as (5,).
  • A 2-D array returns rows and columns, such as (2, 3).
  • A 3-D array returns groups, rows, and columns, such as (2, 2, 3).
  • Use shape[0] to access the first dimension and shape[1] to access the second.
  • Use size to find the total number of elements.

Frequently Asked Questions (FAQs)

1. What is NumPy Array Shape in Python?

NumPy Array Shape describes the number of elements present along each dimension of an array. You can use the shape attribute to find the size of each dimension.

2. How do you find the shape of a NumPy Array?

You can use the shape attribute to find the shape of a NumPy Array.

Example: arr.shape

It returns a tuple containing the number of elements in each dimension.

3. What is the difference between NumPy Array Shape and NumPy Array Dimensions?

NumPy Array Shape tells you the number of elements along each dimension, while the ndim attribute tells you the total number of dimensions in an array.

4. How do you find NumPy Array Dimensions?

You can use the ndim attribute to check the number of dimensions in a NumPy Array.

Example: arr.ndim

5. What is the NumPy shape function?

The NumPy shape attribute returns the size of an array along each dimension. Although commonly called the shape function, it is an attribute rather than a function.

6. How do you find the number of rows and columns in a NumPy 2-D Array?

You can use the shape attribute to find the number of rows and columns in a NumPy 2-D Array.

Example: arr.shape

It returns (rows, columns), where the first value represents rows and the second value represents columns.

7. How do you create a NumPy 2-D Array with specific rows and columns?

You can use np.array() to create a 2-D array by arranging elements into rows and columns.

Example:

import numpy as np

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

print(arr.shape)

Output:

(2, 3)

8. Can we change the shape of a NumPy Array?

Yes, you can use the reshape() method to change the shape of a NumPy Array without changing its original data, provided the total number of elements remains the same.

Example: arr.reshape(2, 3)

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

Scroll to Top