Modules & Packages · Unit 5

NumPy

Arrays, attributes and statistics

Unit 5 · Numerical computing with NumPy

Arrays make numerical data explicit

NumPy is a Python package for working with arrays and numerical operations. An array has a shape and a data type; arithmetic can operate across the array without writing a loop for every item. Select a one-dimensional or two-dimensional view, then run real NumPy examples in the browser.

First run: NumPy is loaded on demand into the shared Pyodide runtime when an example imports it.

Explore array structure

Edit up to four numeric values. Switch dimensions to see how the same values can be grouped into rows.

Indexed array view

[0]
82
[1]
91
[2]
76
[3]
88
.shape

(4,)

.ndim

1

.size

4

.dtype

int64*

*For this integer-valued example; NumPy chooses dtype from the supplied values. Real arrays may use float64 or other dtypes.

Inspect attributes in Python

NumPy array attributes
Starting up Python…
import numpy as np
arr = np.array([82, 91, 76, 88])
print("Array:", arr)
print("shape:", arr.shape)
print("ndim:", arr.ndim)
print("size:", arr.size)
print("dtype:", arr.dtype)
print("itemsize:", arr.itemsize)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Index, slice, and calculate

Like a list, a one-dimensional array uses zero-based indexes and slices. NumPy arrays also support vectorized operations and matrix calculations.

Indexing and slicing
Starting up Python…
import numpy as np
marks = np.array([82, 91, 76, 88])
print("first:", marks[0])
print("last:", marks[-1])
print("first three:", marks[:3])
print("add five:", marks + 5)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Choose a numerical operation

Calculate the arithmetic average.

Try np.mean
Starting up Python…
import numpy as np
marks = np.array([82, 91, 76, 88])
print("mean:", np.mean(marks))
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

What the array attributes tell you

.shape

The size along each dimension, such as (2, 2).

.ndim

How many dimensions the array has.

.size

Total number of elements.

.dtype

The element data type, such as int64 or float64.

.itemsize

Bytes used by one array element.

Common NumPy mistakes

Shape mismatch

np.matmul(a, b)

Matrix multiplication needs compatible inner dimensions.

IndexError

arr[4] on an array with four items

For four elements, valid positions are 0 through 3.

Unexpected dtype

Mixing integers and decimals

NumPy may choose a floating-point dtype to represent all values.

List vs array

[1, 2, 3] + 5

A Python list cannot add a number element-wise; create a NumPy array first.

Check your understanding

1. What is the shape of a 2 by 3 array?

2. What does arr.ndim report?

3. What does np.mean(values) calculate?

4. What must match for matrix multiplication?