StudyAIStudyAI
Pro
Lesson 49 min

NumPy

Fast numerical arrays — the foundation of ML in Python.

What you will learn
  • Creating NumPy arrays
  • Vectorised math (no loops)
  • Why NumPy is fast

Explanation

NumPy provides the ndarray, a fast, typed array. Almost every ML library is built on it.

Its superpower is vectorisation: you do math on whole arrays at once instead of looping element by element, which is far faster and cleaner.

Think of NumPy arrays as the numbers your models actually consume.

Code Example

python
1
import numpy as np
2
 
3
x = np.array([1, 2, 3, 4])
4
print(x.mean())      # 2.5
5
print(x * 10)        # [10 20 30 40]  (no loop needed)
Real-world use

When a model takes an image as input, that image is a NumPy array of pixel numbers.

Common mistakes
  • Looping over a NumPy array in pure Python instead of using vectorised operations.
Practice

Create a NumPy array of 5 numbers and print its mean, max, and the array doubled.

Knowledge check
0/1 answered

1. What is NumPy's main advantage for ML?

Answer all questions to check.