import numpy as np
vector = np.array([1, 2, 3, 4])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
vector, matrix(array([1, 2, 3, 4]),
array([[1, 2],
[3, 4],
[5, 6]]))
NumPy is the array engine underneath a large part of the Python AI ecosystem.
Even when you spend most of your time in pandas, PyTorch, TensorFlow, or scikit-learn, NumPy shapes how you think about:
For AI engineers, NumPy is important because it teaches the mechanics behind efficient numerical code.
This chapter follows the official NumPy beginner material and user guide, then reframes it for system-oriented learning.
Sources:
The core NumPy abstraction is the ndarray.
An ndarray is:
The most important shift is this:
You do not want to think in Python loops first.
You want to think in whole-array operations.
import numpy as np
vector = np.array([1, 2, 3, 4])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
vector, matrix(array([1, 2, 3, 4]),
array([[1, 2],
[3, 4],
[5, 6]]))
Common constructors from the NumPy beginner docs:
np.array(...)np.zeros(...)np.ones(...)np.arange(...)np.linspace(...)np.random.default_rng(...).random(...)zeros = np.zeros((2, 3))
ones = np.ones((2, 3))
steps = np.arange(0, 10, 2)
line = np.linspace(0, 1, 5)
zeros, ones, steps, line(array([[0., 0., 0.],
[0., 0., 0.]]),
array([[1., 1., 1.],
[1., 1., 1.]]),
array([0, 2, 4, 6, 8]),
array([0. , 0.25, 0.5 , 0.75, 1. ]))
Every serious NumPy task starts with inspecting array metadata.
data = np.array([[10, 20, 30], [40, 50, 60]])
print("shape:", data.shape)
print("ndim:", data.ndim)
print("dtype:", data.dtype)
print("size:", data.size)shape: (2, 3)
ndim: 2
dtype: int64
size: 6
These values matter because bugs in numerical code often come from:
NumPy indexing is the foundation for selecting features, batches, windows, and tensor-like slices.
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
arr[0, 1]np.int64(20)
arr[:, 1]array([20, 50, 80])
arr[1:, :2]array([[40, 50],
[70, 80]])
The two key patterns are:
This is where NumPy becomes powerful.
x = np.array([1, 2, 3, 4])
y = np.array([10, 20, 30, 40])
x + yarray([11, 22, 33, 44])
x * 2array([2, 4, 6, 8])
x ** 2array([ 1, 4, 9, 16])
These operations happen elementwise.
That makes them cleaner and usually much faster than manual Python loops.
Broadcasting lets NumPy apply operations across arrays of compatible shapes.
features = np.array(
[
[1.0, 10.0],
[2.0, 20.0],
[3.0, 30.0],
]
)
scale = np.array([0.5, 2.0])
features * scalearray([[ 0.5, 20. ],
[ 1. , 40. ],
[ 1.5, 60. ]])
This is a foundational concept for AI work because the same mental model appears in tensor libraries everywhere.
Before modeling, summarize.
scores = np.array([[0.81, 0.77, 0.79], [0.86, 0.83, 0.85]])
print(scores.mean())
print(scores.mean(axis=0))
print(scores.max(axis=1))0.8183333333333332
[0.835 0.8 0.82 ]
[0.81 0.86]
Important aggregation functions include:
summeanminmaxstdargmaxThe axis parameter is one of the most important details to understand deeply.
AI code constantly moves between different shapes:
values = np.arange(12)
grid = values.reshape(3, 4)
values, grid(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]))
grid.flatten()array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Reshaping is not just formatting.
It is part of how you express the structure of the computation.
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
np.vstack([a, b])array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
np.hstack([a, b])array([[1, 2, 5, 6],
[3, 4, 7, 8]])
These operations are useful when building batches, assembling features, or preparing input blocks.
Randomness matters in simulation, sampling, initialization, and testing.
rng = np.random.default_rng(seed=42)
sample = rng.integers(0, 10, size=(2, 3))
samplearray([[0, 7, 6],
[4, 4, 8]])
Using an explicit generator is usually better than relying on global random state.
You do not need to master every NumPy linear algebra function on day one.
But you should be comfortable with arrays as vectors and matrices.
weights = np.array([0.2, 0.5, 0.3])
features = np.array([3.0, 4.0, 5.0])
np.dot(weights, features)np.float64(4.1)
This kind of operation sits underneath a lot of machine learning code.
This is one of the most practical NumPy ideas from the user guide.
Some operations create views into the same underlying data.
Others create copies.
That distinction matters for:
original = np.array([1, 2, 3, 4])
view = original[1:3]
view[0] = 999
originalarray([ 1, 999, 3, 4])
If you do not understand views, array mutation can feel mysterious.
array, zeros, ones, arange, and linspaceshape, ndim, dtype, and sizeaxisThe following guided notebooks are included in this repo:
NumPy teaches you how numerical computation is structured in Python.
That makes it more than a utility library.
It is part of the mental foundation for understanding features, tensors, batches, matrix operations, and efficient array-first thinking.