> ML SERIES :: 03 python toolkit

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.

numpy
np.array

Fast arrays and matrix math. Everything else is built on it.

pandas
pd.DataFrame

Load, clean, filter, and reshape tables of data, such as CSV logs.

scikit-learn
model.fit()

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

google colab

Notebook in the browser with everything preinstalled, including free GPU time. The easiest place to start.

jupyter (local)

The same notebook experience on your own machine. Good for private data that should not leave your computer.

.py scripts

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
Security habit: install packages only from their official names, and pin versions in a requirements.txt. Typosquatted packages on PyPI (for example, a misspelled "scikit-learn") are a real supply-chain attack vector, and page 14 covers how attackers target the ML pipeline.