Menu
Coddy logo textTech

Understanding Shapes

Lesson 5 of 18 in Coddy's Numpy Fundamentals course.

Shape is an attribute that shows the dimension of the array

ary = np.array([1, 2, 3])
print(ary.shape)

Output:

(3,)

Shape has 1 element with the value 3, meaning it is a 1-dimensional array with 3 elements.

What about 2 dimensions?

ary = np.array([[1, 2, 3], [1, 2, 3]])
print(ary.shape)

Output:

(2, 3)

The shape consists of 2 elements, with the first element being 2, indicating two 1-dimensional arrays, each containing 3 values.

It's important to mention that you cannot have two N-dimensional arrays within the same structure if they contain a different number of elements.
For example, this is not possible:

 ary = np.array([[1, 2, 3], [1]])

The first element has 3 values ([1, 2, 3]) and the second has one value ([1]).

 

What about 3 dimensions?

ary = np.array([[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]])
print(ary.shape)

Output:

(2, 2, 3)

3 elements in the shape meaning it is a 3D array.

two 2D arrays, two 1D arrays and each 1D array has 3 values.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create an array named ary with the following shape:

(1, 2, 2, 2)

Try it yourself

import numpy as np
ary = np.array(
	# Complete
)

print(ary.shape) # Don't touch

All lessons in Numpy Fundamentals