NUMPY FOR MACHINE LEARNING
NumPy (Numerical Python) provides the core array and numerical-computing primitives used throughout the Python machine-learning ecosystem. Before working deeply with Pandas, scikit-learn, deep-learning frameworks, and scientific computing libraries, it is valuable to understand NumPy arrays, dimensions, indexing, vectorized operations, broadcasting, aggregation, and numerical transformations.
BITCODEMATRIX
NUMPY FOR MACHINE LEARNING
NumPy (Numerical Python) provides the core array and numerical-computing primitives used throughout the Python machine-learning ecosystem. Before working deeply with Pandas, scikit-learn, deep-learning frameworks, and scientific computing libraries, it is valuable to understand NumPy arrays, dimensions, indexing, vectorized operations, broadcasting, aggregation, and numerical transformations.
Learning Objectives
- Understand NumPy arrays and how they differ from ordinary Python lists.
- Create one-dimensional and multi-dimensional arrays.
- Understand shape, dimensions, size, dtype, and axis.
- Index, slice, reshape, flatten, and transpose arrays.
- Perform vectorized arithmetic and comparison operations.
- Understand broadcasting and when it is valid.
- Use aggregation functions such as sum, mean, min, max, std, and percentile.
- Generate random data for ML experiments and simulations.
- Handle missing or invalid numerical values with NumPy tools.
- Use NumPy for feature matrices, labels, normalization, and basic ML calculations.
- Understand views versus copies and avoid common array-manipulation mistakes.
1. Why NumPy Matters in Machine Learning
Machine-learning datasets are fundamentally collections of numerical values. A feature matrix can contain thousands or millions of rows and many columns. Python lists can represent such data, but NumPy arrays provide a compact, vectorized representation designed for numerical computation.
2. Installing and Importing NumPy
pip install numpy
import numpy as np
The alias np is the conventional short name used in Python code. It is not required by NumPy, but it is widely used.
3. Python Lists vs NumPy Arrays
python_list = [1, 2, 3, 4]
numpy_array = np.array([1, 2, 3, 4])
print(python_list)
print(numpy_array)
A NumPy ndarray has a homogeneous dtype for its elements in normal numerical use, whereas a Python list can contain objects of different types. NumPy arrays also support vectorized numerical operations.
a = np.array([1, 2, 3])
print(a * 2) # [2 4 6]
print(a + 10) # [11 12 13]
With a Python list, list * 2 repeats the list rather than multiplying every number. NumPy's element-wise semantics make numerical operations concise and efficient.
4. Creating NumPy Arrays
a = np.array([10, 20, 30])
zeros = np.zeros(5)
ones = np.ones(5)
full = np.full(5, 7)
sequence = np.arange(0, 10, 2)
points = np.linspace(0, 1, 5)
identity = np.eye(3)
5. Dimensions, Shape, Size and dtype
x = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(x.ndim) # 2
print(x.shape) # (2, 3)
print(x.size) # 6
print(x.dtype) # integer dtype
For ML, shape is especially important. A matrix with shape (1000, 20) can represent 1,000 observations and 20 features.
6. One-Dimensional, Two-Dimensional and Higher-Dimensional Arrays
vector = np.array([1, 2, 3])
matrix = np.array([
[1, 2, 3],
[4, 5, 6]
])
tensor = np.zeros((2, 3, 4))
print(vector.shape) # (3,)
print(matrix.shape) # (2, 3)
print(tensor.shape) # (2, 3, 4)
A one-dimensional array has one axis, a matrix has two axes, and higher-dimensional arrays can represent more complex structures such as image batches or model inputs.
7. Indexing
x = np.array([10, 20, 30, 40, 50])
print(x[0]) # 10
print(x[-1]) # 50
m = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(m[0, 1]) # 20
print(m[1, 2]) # 60
NumPy uses zero-based indexing. For a two-dimensional array, m[row, column] accesses one element.
8. Slicing
x = np.array([0, 1, 2, 3, 4, 5])
print(x[1:4]) # [1 2 3]
print(x[:3]) # [0 1 2]
print(x[::2]) # [0 2 4]
m = np.arange(12).reshape(3, 4)
print(m[:, 1]) # second column
print(m[0, :]) # first row
print(m[:2, :2]) # top-left 2x2 block
Slicing is heavily used when selecting rows, columns, feature subsets, or sections of numerical data.
9. Reshape
x = np.arange(12)
matrix = x.reshape(3, 4)
print(matrix)
print(matrix.shape)
Reshape changes the array's dimensions without changing the number of elements. The product of the new dimensions must match the original number of elements.
x = np.arange(12)
x.reshape(2, 6)
x.reshape(4, 3)
# x.reshape(5, 3) # invalid: 12 elements cannot fill 15 positions
10. Flatten, Ravel and Transpose
m = np.array([
[1, 2, 3],
[4, 5, 6]
])
flat = m.flatten()
flat_view_like = m.ravel()
transposed = m.T
print(flat)
print(transposed)
flatten() returns a flattened copy. ravel() returns a flattened array and may return a view when possible. The .T attribute provides the transpose for a two-dimensional array.
11. Data Types (dtype)
a = np.array([1, 2, 3], dtype=np.int32)
b = np.array([1.0, 2.0, 3.0], dtype=np.float64)
print(a.dtype)
print(b.dtype)
Choosing suitable dtypes can affect memory use and numerical behavior. Converting data types should be deliberate, especially when working with large ML datasets.
x = np.array([1, 2, 3])
x_float = x.astype(np.float64)
12. Vectorization
Vectorization means expressing operations on whole arrays instead of manually looping over individual elements in Python.
x = np.array([1, 2, 3, 4, 5])
y = x * 10
z = x ** 2
print(y)
print(z)
Vectorized operations are concise and generally allow NumPy to perform numerical work efficiently in optimized compiled code.
13. Arithmetic Operations
x = np.array([10, 20, 30])
y = np.array([1, 2, 3])
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x ** 2)
print(x % 3)
These operations are element-wise when the arrays have compatible shapes.
14. Comparison and Boolean Operations
x = np.array([10, 20, 30, 40])
mask = x > 20
print(mask)
print(x[mask])
Boolean masks are fundamental to filtering numerical data.
x = np.array([10, 20, 30, 40, 50])
mask = (x >= 20) & (x <= 40)
print(x[mask])
For element-wise logical conditions, use NumPy's &, |, and ~ operators with parentheses around individual comparisons.
15. Broadcasting
Broadcasting allows NumPy to perform operations on arrays with compatible but different shapes. It can avoid explicit loops and is widely used in numerical preprocessing.
X = np.array([
[10, 20, 30],
[40, 50, 60]
])
offset = np.array([1, 2, 3])
result = X + offset
print(result)
Here, the shape (3,) vector is broadcast across the rows of the (2, 3) matrix.
Broadcasting follows NumPy's shape-compatibility rules. When an operation fails, inspect the shapes before changing the data.
16. Aggregation Functions
x = np.array([10, 20, 30, 40, 50])
print(x.sum())
print(x.mean())
print(x.min())
print(x.max())
print(x.std())
print(np.var(x))
print(np.median(x))
print(np.percentile(x, 75))
Axis
X = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(X.sum(axis=0)) # column-wise
print(X.sum(axis=1)) # row-wise
axis=0 aggregates down rows, producing one result per column. axis=1 aggregates across columns, producing one result per row.
17. Handling Missing and Invalid Numerical Values
NaN represents a missing/not-a-number floating-point value. NumPy provides nan-aware functions for many aggregation tasks.
x = np.array([10.0, 20.0, np.nan, 40.0])
print(np.mean(x)) # nan
print(np.nanmean(x)) # ignores NaN for the mean
print(np.isnan(x))
Missing-value handling should reflect the meaning of the data. Simply ignoring missing values is not always the correct modeling decision.
18. Random Numbers for ML
Random generation is useful for simulations, synthetic datasets, initialization, sampling, and experiments. For reproducible work, use an explicit random generator.
rng = np.random.default_rng(42)
print(rng.random(5))
print(rng.integers(0, 10, size=5))
print(rng.normal(loc=0, scale=1, size=5))
A fixed seed such as 42 makes a particular pseudo-random sequence reproducible when the same generator and operations are used.
19. Random Sampling
rng = np.random.default_rng(42)
data = np.arange(100)
sample = rng.choice(data, size=10, replace=False)
print(sample)
Sampling without replacement prevents the same element from being selected more than once within that sample.
20. Normalization and Standardization with NumPy
A common transformation is standardization: subtract a feature's mean and divide by its standard deviation.
x = np.array([10.0, 20.0, 30.0, 40.0, 50.0])
mean = x.mean()
std = x.std()
z = (x - mean) / std
print(z)
The formula is conceptually simple, but production ML pipelines must define how statistics are calculated. For supervised learning, preprocessing parameters should normally be learned from the training data rather than the full dataset, to avoid information leakage.
21. Feature Matrix and Target Vector
X = np.array([
[25, 40000],
[31, 55000],
[42, 90000],
[28, 48000],
[36, 70000]
])
y = np.array([0, 1, 0, 1, 1])
print("X shape:", X.shape)
print("y shape:", y.shape)
Keeping the dimensions of X and y correct is one of the most common practical issues when moving from raw data into ML libraries.
22. Basic ML Calculation with NumPy
A simple linear prediction can be written as:
ŷ = Xw + b
X = np.array([
[1.0, 2.0],
[2.0, 1.0],
[3.0, 4.0]
])
w = np.array([0.5, 2.0])
b = 1.0
y_pred = X @ w + b
print(y_pred)
The @ operator performs matrix multiplication. This operation is foundational to linear models and neural-network computations.
23. Dot Product and Matrix Multiplication
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a @ b)
A = np.array([
[1, 2],
[3, 4]
])
B = np.array([
[5, 6],
[7, 8]
])
print(A @ B)
Element-wise multiplication (*) and matrix multiplication (@) are different operations. Confusing them can produce incorrect ML calculations.
24. Concatenation and Stacking
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate([a, b]))
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.vstack([A, B]))
print(np.hstack([A, B]))
The correct stacking operation depends on the intended shape. Always inspect the resulting shape.
25. Views vs Copies
NumPy may return a view into existing memory rather than an independent copy. Modifying a view can therefore affect the original array.
x = np.array([10, 20, 30, 40])
view = x[1:3]
view[0] = 999
print(x)
Use copy() when an independent array is required:
x = np.array([10, 20, 30, 40])
copy_x = x[1:3].copy()
copy_x[0] = 999
print(x)
26. NumPy in an ML Data Pipeline
27. Practical Example — Customer Features
import numpy as np
customers = np.array([
[25, 40000, 12],
[31, 55000, 18],
[42, 90000, 6],
[28, 48000, 24],
[36, 70000, 15]
], dtype=float)
ages = customers[:, 0]
income = customers[:, 1]
support_calls = customers[:, 2]
print("Average age:", ages.mean())
print("Median income:", np.median(income))
print("Maximum support calls:", support_calls.max())
income_mean = income.mean()
income_std = income.std()
income_z = (income - income_mean) / income_std
print("Income z-scores:", income_z)
This example demonstrates a common pattern: select columns, calculate descriptive statistics, then transform a feature.
28. Common NumPy Mistakes
- Confusing shape (n,) with (n, 1).
- Using * when matrix multiplication @ is required.
- Forgetting zero-based indexing.
- Misunderstanding axis=0 versus axis=1.
- Assuming reshape changes the number of elements.
- Ignoring dtype and numerical precision.
- Accidentally modifying the original array through a view.
- Using Python loops where vectorized NumPy operations are appropriate.
- Ignoring NaN values during aggregation.
- Using random data without controlling reproducibility when experiments need to be repeatable.
- Calculating preprocessing statistics using validation/test data and introducing leakage.
29. Interview Questions
- What is NumPy and why is it important for machine learning?
- What is an ndarray?
- How is a NumPy array different from a Python list?
- What are ndim, shape, size, and dtype?
- What is the difference between a vector with shape (n,) and a column vector with shape (n, 1)?
- What is vectorization?
- What is broadcasting?
- Give an example of a broadcasting-compatible operation.
- What is the difference between * and @ in NumPy?
- What does axis=0 mean for a two-dimensional array?
- What does axis=1 mean?
- How does reshape work?
- What is the difference between flatten() and ravel()?
- What is a Boolean mask?
- How do you handle NaN values in NumPy?
- How can you generate reproducible random numbers?
- What is the difference between a view and a copy?
- How do you calculate a z-score using NumPy?
- How would you represent an ML feature matrix?
- Why can preprocessing on the complete dataset cause data leakage?
- How do you calculate column-wise means?
- How do you select a specific column from a matrix?
- What is np.dot and how does it relate to @?
- When would you use np.linspace instead of np.arange?
- Why is NumPy generally useful for numerical workloads?
30. Hands-on Exercises
- Create a NumPy array containing the ages of 20 customers and calculate mean, median, minimum, maximum, and standard deviation.
- Create a 5 × 4 feature matrix and print its shape, number of dimensions, and total element count.
- Extract the first two rows and last two columns from a matrix using slicing.
- Reshape an array containing 24 values into shapes (4, 6), (6, 4), and (2, 3, 4).
- Create a Boolean mask to select values greater than the mean.
- Calculate column-wise and row-wise means using axis.
- Create an array containing NaN values and compare mean() with nanmean().
- Generate 1,000 normally distributed values using a reproducible random generator and inspect their mean and standard deviation.
- Standardize each column of a small feature matrix.
- Perform a matrix multiplication using X @ w.
- Create two arrays and demonstrate the difference between element-wise multiplication and matrix multiplication.
- Create a slice, modify it, and observe view behavior; repeat using copy().
31. Mini Project — Build a Numerical Preprocessing Pipeline
Create a small customer dataset with age, income, account tenure, and monthly spend. Use NumPy to:
- Represent the features as X.
- Inspect shape and dtype.
- Calculate descriptive statistics.
- Detect missing values.
- Select valid observations with Boolean masks.
- Standardize numerical features.
- Create a target vector y.
- Calculate a simple linear prediction using X @ w + b.
- Document every transformation and its expected shape.
32. What You Should Be Able to Do After This Article
Create and inspect NumPy arrays.
Reason about dimensions and shapes.
Use indexing, slicing, and Boolean masks.
Reshape and transpose numerical data.
Perform vectorized calculations.
Use broadcasting correctly.
Aggregate numerical data by axis.
Generate reproducible random samples.
Perform basic ML-oriented transformations.
Understand feature matrices and target vectors.
Avoid common shape, view/copy, and data-leakage mistakes.
BITCODEMATRIX • Practical Engineering Learning