Machine Learning ML-00.04
Your toolkit: Python, NumPy, pandas, scikit-learn, PyTorch
Set up Python for machine learning and learn what NumPy, pandas, scikit-learn and PyTorch each do, with code you can run.
The big idea
Almost all machine learning work today happens in Python, not because Python is fast (it is not) but because it is the friendly front door to libraries that are. When you multiply two large arrays in NumPy, the actual work runs in optimized C and Fortran code; Python just describes what to do.
Four libraries cover nearly everything in this track, and each has one job. Think of a workshop: NumPy is the raw material and the measuring tools, pandas is the workbench where you sort and clean, scikit-learn is a wall of ready-made power tools with identical handles, and PyTorch is the machine shop you move into when you need to build something custom and heavy, like a neural network.
How it works
Step 1: Install Python and an environment
Use a recent Python 3 and give every project its own virtual environmentAn isolated folder of Python packages for one project, so projects do not break each other's dependencies., so one project's library versions never break another's. Two common ways:
# Option A: uv (a fast installer and environment manager)
uv venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
uv pip install numpy pandas scikit-learn
# Option B: the built-in tools
python3 -m venv .venv
source .venv/bin/activate
pip install numpy pandas scikit-learn
PyTorch has a separate installer per operating system and GPU type, so install it from the official instructions when you reach the deep learning modules.
Step 2: Notebooks and scripts
A notebookAn interactive document, such as Jupyter, that mixes code cells, their output and notes, and keeps variables in memory between cells. (Jupyter, VS Code notebooks, Colab) lets you run code a cell at a time and see tables and charts inline. It is perfect for exploring data. It is also easy to run cells out of order and end up with results nobody can reproduce. The usual pattern: explore in a notebook, then move anything you will rerun into plain .py scripts or a package.
Step 3: NumPy, arrays and broadcasting
NumPy's core object is the ndarrayNumPy's fixed-type, n-dimensional grid of numbers, stored compactly so maths on it runs in fast compiled code., a grid of numbers of one type. Every array has a shapeThe size of an array along each dimension, such as (2, 3) for 2 rows and 3 columns.. Operations work on whole arrays at once, with no Python loop, and broadcastingNumPy's rule for combining arrays of different shapes by stretching the smaller one across the larger, without copying data. lets arrays of different shapes combine when their shapes line up from the right.
import numpy as np
prices = np.array([3.0, 5.0, 2.5]) # price of 3 products
qty = np.array([[1, 2, 0], # customer 1 bought 1, 2, 0
[4, 0, 1]]) # customer 2 bought 4, 0, 1
print(prices.shape, qty.shape)
spend = qty * prices # broadcasting: prices is reused for each row
print(spend)
print(spend.sum(axis=1)) # total per customerOutput
(3,) (2, 3)
[[ 3. 10. 0. ]
[12. 0. 2.5]]
[13. 14.5] qty has shape (2, 3) and prices has shape (3,). The trailing dimensions match (3 and 3), so NumPy applies prices to every row. No loop, and it stays fast with millions of rows. Shapes and broadcasting come back constantly; Matrices and linear transformations covers them with matrices.
Step 4: pandas for tables, scikit-learn for models
pandas gives you the DataFramepandas' table type: named columns, each one a typed array, with an index for the rows.: a table with named columns that you can filter, group, join and clean. scikit-learn gives you dozens of models that all share one interface: create the model, call fit(X, y) to train, call predict(X_new) for inference. Swapping a linear model for a random forest is a one-line change.
import pandas as pd
from sklearn.linear_model import LinearRegression
df = pd.DataFrame({
"size_m2": [50, 72, 95, 120, 64],
"rooms": [1, 2, 3, 4, 2],
"price_k": [210, 290, 360, 470, 255],
})
print(df.head(3))
print(df["price_k"].mean())
print(df[df["rooms"] >= 3])
X = df[["size_m2", "rooms"]] # features
y = df["price_k"] # label
model = LinearRegression().fit(X, y) # training
new = pd.DataFrame({"size_m2": [80], "rooms": [3]})
print(model.predict(new).round(1)) # inference on a flat it has never seenOutput
size_m2 rooms price_k
0 50 1 210
1 72 2 290
2 95 3 360
317.0
size_m2 rooms price_k
2 95 3 360
3 120 4 470
[306.4] Notice the vocabulary from What is machine learning? showing up as code: X holds the features, y the label, fit is training and predict is inference. With only five rows the prediction is a toy, but the shape of the code is exactly what you will write on real datasets.
Step 5: When to move to PyTorch
Stay with pandas and scikit-learn for tabular data: rows and columns of numbers and categories. Move to PyTorch when you need neural networks, which usually means images, text, audio or very large datasets, and when you want a GPU to do the heavy arithmetic. PyTorch's tensorPyTorch's array type: like a NumPy array, but it can live on a GPU and track operations so gradients can be computed automatically. is NumPy's array with two superpowers: it can live on a GPU, and it remembers the operations applied to it so it can compute gradients automatically.
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) # looks like a NumPy array
w = torch.randn(2, 1, requires_grad=True) # weights we want to learn
loss = (x @ w).pow(2).mean() # some computation
loss.backward() # PyTorch computes d(loss)/d(w)
print(w.grad.shape) # torch.Size([2, 1])
# On a machine with an NVIDIA GPU: x = x.to("cuda")
You will use that backward() call to train networks from Activation functions onward.
Try it
Run the two files above on your own machine:
- Create an environment with
uv venvorpython3 -m venv .venv, activate it, and install numpy, pandas and scikit-learn. - Save each code block as a
.pyfile and run it withpython numpy_basics.py. Your output should match the boxes above exactly. - Change
pricestonp.array([3.0, 5.0])and run it again. Read the error: the shapes(2, 3)and(2,)no longer line up from the right. Then tryprices[:, None]with a 2-row array and see broadcasting work down the columns instead.
Trade-offs
| Option | Wins when | Loses when |
|---|---|---|
| Notebook | Exploring data, plotting, explaining results to others | Repeatable pipelines: hidden state and out-of-order cells make results hard to reproduce |
| Scripts and packages | Training jobs, scheduled pipelines, anything you run twice | Quick visual exploration of a new dataset |
| scikit-learn | Tabular data, classical models, fast experiments with a uniform API | Neural networks, GPUs and unstructured data at scale |
| PyTorch | Neural networks, images, text, audio, GPU training | Small tabular problems where a few lines of scikit-learn would do |
Common mistakes
- Installing packages globally, then breaking one project when another needs a different version. Use one environment per project.
- Looping over rows in Python instead of using array operations. The vectorized version is usually far faster.
- Mixing up shapes
(3,)and(3, 1). They broadcast differently; print.shapewhenever results look strange. - Trusting a notebook whose cells were run out of order. Restart and run all before believing a result.
- Reaching for PyTorch on a small spreadsheet problem that scikit-learn solves in three lines.
In the wild
These four libraries are the shared language of the field. Data scientists explore data in pandas notebooks, classical models for tabular problems such as churn prediction or credit scoring are commonly trained with scikit-learn or gradient-boosting libraries that follow its fit and predict style, and most modern deep learning research and production training, including large language models, is written in PyTorch. Under all of them sits NumPy's array model, which is why its shape and broadcasting rules are worth learning well.
Check yourself
-
An array of shape (4, 3) is multiplied by an array of shape (3,). What happens?
Show answer
B. Broadcasting: the (3,) array is applied to each of the 4 rows Shapes are compared from the right. 3 matches 3, so the smaller array is stretched across all 4 rows, giving shape (4, 3).
-
In scikit-learn, which call does inference?
Show answer
B. predict fit(X, y) trains the model; predict(X_new) applies it to new data, which is inference.
-
You have a CSV of 50,000 customers with 30 numeric and categorical columns and want to predict churn. Where should you start?
Show answer
B. pandas to clean it and a scikit-learn model Tabular data of this size is exactly what pandas and scikit-learn are for. Start simple and only move to deep learning if it clearly helps.
-
Why use a virtual environment per project?
Show answer
B. It isolates each project's package versions so projects do not break each other Different projects often need different library versions. An environment keeps each set separate and reproducible.
Recap
- Python is the front end; NumPy, pandas, scikit-learn and PyTorch do the real work in fast compiled code.
- NumPy arrays have shapes, and broadcasting combines different shapes without loops.
- pandas DataFrames hold and clean tables; scikit-learn models share fit for training and predict for inference.
- Move to PyTorch for neural networks, unstructured data and GPUs.
- Use one virtual environment per project and move repeatable work from notebooks into scripts.
Next up
- From notebook to production learning path