Introduction to the NumPy Library

Almost every AI library — PyTorch, TensorFlow, scikit-learn — is built on top of NumPy. It provides a single, powerful data structure called the ndarray (n-dimensional array) that stores and processes numbers with the speed of compiled C code while remaining as readable as Python. Before you can write a neural network, you need to be comfortable with NumPy.


Why NumPy and Not Plain Python Lists?

Standard Python lists can hold any type of object. This flexibility costs performance. NumPy arrays store a single data type in a contiguous block of memory, which allows the processor to crunch through them with vectorized CPU instructions — no Python interpreter overhead.

The practical difference is dramatic: a NumPy operation on a million numbers is typically 100–500x faster than an equivalent Python loop.

Creating Your First Arrays

You import NumPy by convention as np. From there, np.array() turns any list (or list of lists) into an ndarray.

np.zeros(), np.ones(), and np.arange() are the fastest ways to create arrays of a known size.

<pre><code class="language-python">import numpy as np # 1D array (a vector) v = np.array([1, 2, 3, 4]) # 2D array (a matrix) M = np.array([[1, 2], [3, 4]]) # Utility constructors zeros = np.zeros((3, 3)) # 3x3 matrix of 0.0 ones = np.ones((2, 4)) # 2x4 matrix of 1.0 range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8] </pre>

Essential Attributes Every AI Engineer Uses Daily

Before doing any math on an array, you need to understand its shape, dtype, and size. Shape mismatches are the most common source of bugs when building neural networks.

Shape, dtype, and ndim

<pre><code class="language-python">M = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) print(M.shape) # (2, 3) — 2 rows, 3 columns print(M.dtype) # float64 print(M.ndim) # 2 — it's a 2D array print(M.size) # 6 — total elements </pre>

Rule of thumb: always check .shape when you get an unexpected error. 90% of NumPy bugs come from arrays having the wrong shape.

Element-wise Math — The NumPy Default

Any arithmetic operator applied to a NumPy array acts on every element simultaneously — no loop required. This is called element-wise or vectorized operation.

Operations on Arrays

<pre><code class="language-python">a = np.array([1, 2, 3, 4]) print(a + 10) # [11, 12, 13, 14] print(a * 2) # [ 2, 4, 6, 8] print(a ** 2) # [ 1, 4, 9, 16] print(np.sqrt(a)) # [1.0, 1.41, 1.73, 2.0] # Two arrays of the same shape b = np.array([10, 20, 30, 40]) print(a + b) # [11, 22, 33, 44] </pre>