NumPy Introduction

NumPy Beginner 3 min read Published
In this lesson

NumPy (Numerical Python) is the library that makes Python fast at maths. A Python list can hold numbers, but it handles them one by one. A NumPy array holds numbers in one tight block of memory and works on all of them at once.

That’s why almost every data and AI tool is built on top of NumPy: Pandas, Matplotlib, scikit-learn, TensorFlow and PyTorch all speak “array”.

Install and import#

NumPy is already included in Google Colab, Anaconda and Jupyter. On your own computer, install it once with pip:

pip install numpy

Then import it at the top of your program. Everyone uses the short name np:

import numpy as np

print(np.__version__)

Output (your version may be newer):

2.2.6

Your first array#

import numpy as np

marks = np.array([72, 85, 64, 90, 58])
print(marks)
print(type(marks))

Output:

[72 85 64 90 58]
<class 'numpy.ndarray'>

ndarray means “n-dimensional array”. It prints without commas, which is an easy way to tell an array from a list.

Lists vs arrays: maths on everything at once#

Suppose every student gets 5 bonus marks. With a list you need a loop:

marks_list = [72, 85, 64, 90, 58]
with_bonus = [m + 5 for m in marks_list]
print(with_bonus)

Output:

[77, 90, 69, 95, 63]

With NumPy you write the maths once and it applies to every element. This is called vectorisation:

import numpy as np

marks = np.array([72, 85, 64, 90, 58])
print(marks + 5)
print(marks * 2)
print(marks > 70)

Output:

[77 90 69 95 63]
[144 170 128 180 116]
[ True  True False  True False]

Careful: with a plain list, marks_list * 2 does something completely different. It repeats the list instead of doubling the numbers:

print([72, 85] * 2)

Output:

[72, 85, 72, 85]

Vectorised code is shorter, easier to read and usually 10 to 100 times faster than a Python loop, because the loop runs inside NumPy’s compiled C code.

One type per array#

A list can mix anything. An array stores one type for every element, which is part of what makes it fast:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([1.5, 2, 3])
print(a.dtype, b.dtype)

Output:

int64 float64

Because 1.5 is a float, NumPy turned every number in b into a float. On Windows you may see int32 instead of int64; both are whole numbers.

Where NumPy is used#

  • Data analysis: every Pandas column is a NumPy array underneath.
  • Images: a photo is an array of pixels (height × width × 3 colours).
  • Machine learning: your data, the model’s weights and its predictions are all arrays.
  • Science and finance: simulations, statistics and fast calculations.
Quick check

Is np.array([1, 2, 3]) * 3 equal to [3, 6, 9] or [1, 2, 3, 1, 2, 3, 1, 2, 3]? NumPy multiplies every element, so the answer is [3 6 9]. Only a plain list repeats.

Summary#

  • NumPy gives Python fast arrays for numbers; import it as np.
  • np.array([...]) turns a list into an array.
  • Maths on an array applies to every element at once (vectorisation), with no loop.
  • All elements in an array share one type, shown by .dtype.

Quick quiz

3 questions. Check what stuck and earn 2 XP for each right answer.

1What does np.array([1, 2, 3]) * 2 give?
2Why is NumPy faster than a Python loop over a list?
3What is the usual short name for NumPy?

Progress is saved in this browser. Create a free account to keep it on every device.