Page 03 of 14 · Foundations
The Python toolkit behind almost every ML project
Three libraries do most of the work in classic machine learning. NumPy handles the math, pandas handles the tables, and scikit-learn handles the models. Learn the pattern they share and every later page will feel familiar.
Fast arrays and matrix math. Everything else is built on it.
Load, clean, filter, and reshape tables of data, such as CSV logs.
Dozens of ready-made models, plus tools to split, scale, and score.
Later pages also mention matplotlib for plotting and PyTorch for neural networks.
The fit-and-predict pattern
Step through a complete script, one line at a time.
Every scikit-learn model, from a straight line to a random forest, follows the same steps: create the model, call fit on training data, then call predict on new data. This script trains a model to flag compromised user accounts from login records.
Output shown is illustrative; your numbers will differ with real data.
Shapes: the most common source of errors
NumPy arrays and pandas tables have a shape, written as (rows, columns). scikit-learn expects features X to be two-dimensional, (samples, features), and labels y to be one-dimensional, (samples,). Most beginner errors come from mixing these up.
Predict the shape
import numpy as np x = np.array([3, 5, 8]) # shape (3,) one feature, 1-D X = x.reshape(-1, 1) # shape (3, 1) what sklearn wants for one feature X * 2 # vectorized: doubles every value, no loop needed
Which library would you reach for?
Where to run your code
Notebook in the browser with everything preinstalled, including free GPU time. The easiest place to start.
The same notebook experience on your own machine. Good for private data that should not leave your computer.
Plain Python files for anything you will automate or deploy.
# local setup (Python 3.10 or newer) python -m venv mlenv # Windows: mlenv\Scripts\activate macOS/Linux: source mlenv/bin/activate pip install numpy pandas scikit-learn matplotlib jupyter
- NumPy for arrays and math, pandas for tables, scikit-learn for models.
- Every scikit-learn model uses the same pattern: create, fit, predict, score.
- Check shapes first when something breaks: X is (samples, features), y is (samples,).