Data Preparation for Machine Learning in Python
Machine learning models learn from numbers, and they learn better when features are on similar scales. Normalization and standardization rescale the data, and matrix multiplication is how models combine many features at once.
The concept in plain words
Machine learning models learn from numbers, and they learn better when features are on similar scales. Normalization and standardization rescale the data, and matrix multiplication is how models combine many features at once.
Scale features with min-max normalization and z-score standardization, and multiply matrices by hand.
Key ideas
- Features on very different scales let the large numbers dominate the model.
- Min-max scaling keeps values between 0 and 1; z-scores keep the shape but centre it on 0.
- Compute the scaling values on the training data and reuse them for new data.
- A matrix product A × B needs the number of columns of A to match the rows of B.
When should you use it?
- Understand what happens inside neural networks and AI libraries.
- Learn the idea before you implement it in the matching challenges.
- Prepare for machine learning courses, interviews and projects.
See it in action
Drag x5 to make an outlier and compare how min-max and z-score scaling react. Point at a cell of A·B to see which row and column it uses.
Example code
import statistics
heights = [150, 160, 170, 180, 190]
# Min-max scaling squeezes values into the range 0..1.
low, high = min(heights), max(heights)
print("min-max:", [round((h - low) / (high - low), 2) for h in heights])
# Z-score standardization centres values on 0 with a spread of 1.
mean = statistics.mean(heights)
std = statistics.pstdev(heights)
print("z-scores:", [round((h - mean) / std, 2) for h in heights])
# Matrix multiplication: each output is a row of A dotted with a column of B.
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
product = [[sum(A[i][k] * B[k][j] for k in range(2)) for j in range(2)] for i in range(2)]
print("A x B:", product)
Expected output
min-max: [0.0, 0.25, 0.5, 0.75, 1.0]
z-scores: [-1.41, -0.71, 0.0, 0.71, 1.41]
A x B: [[19, 22], [43, 50]]
How it works
Min-max scaling maps the smallest height to 0 and the largest to 1. Z-scores subtract the mean and divide by the standard deviation, so the values are centred on 0. In the matrix product each cell is a row of A multiplied element by element with a column of B, then summed.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the inputs or weights and predict the output before running.
- Print the intermediate values to follow each step of the calculation.
- Solve the linked challenges below to implement the idea yourself.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Matrix Multiplication Multiply two matrices, the operation behind every layer of a neural network. Easy
- Min-Max Normalization Rescale a feature so its smallest value becomes 0 and its largest becomes 1. Easy
- Z-Score Standardization Center a feature at 0 and scale it to a standard deviation of 1. Easy