Python for ML

The working 90% of the ML stack, for people who already code.

This course is for people who already program in another language: it’s the Python you need on top of that to read and write ML code: the functions and habits used every day, not the whole API. Each chapter is short. Every function is a word you can tap for its definition and a small picture, and one example moves through the chapter’s ideas.

Nothing runs in your browser. Each chapter shows its code with the output it really printed, and ends with a workbook packet: a small folder of exercises and tests you download and fill in on your own machine, the same way ML code is tested at work. Set up the environment once, with the versions below, and every packet runs in it.

Pinned as of September 25, 2026 · Python 3.12 or newer
LibraryVersion
numpy2.5.3
pandas3.0.6
pyarrow25.0.1
matplotlib3.11.2
scikit-learn1.9.1
torch2.14.0
pytest9.1.1

Changelog

  • September 25, 2026: first edition. Ten chapters and their workbook packets, written against the versions above.
Chapter 1: Python for People Who Already Code Chapter 1 of Python for ML: lists, dicts and sets, comprehensions, f-strings, unpacking, dataclasses, with blocks and generators, and the virtual environment every later chapter runs in. Chapter 2: Arrays and Shapes Chapter 2 of Python for ML: NumPy arrays, their shape and dtype, and how to pick out and rearrange the values in them. Chapter 3: Vectorize Everything Chapter 3 of Python for ML: vectorization, broadcasting, reductions along an axis, joining arrays, and the difference between a view and a copy. Chapter 4: Tables Chapter 4 of Python for ML: pandas DataFrames and Series, reading a CSV, selecting rows and columns by label and by position, cleaning missing values, and pandas 3's copy-on-write. Chapter 5: Reshape and Combine Chapter 5 of Python for ML: pandas groupby and agg, merge and concat, pivot_table and melt, apply against vectorized code, and datetimes with to_datetime, resample and rolling. Chapter 6: Seeing the Data Chapter 6 of Python for ML: Matplotlib's Figure and Axes, line, scatter, histogram, bar and image plots, labels and legends, subplot grids, log scales, savefig and DataFrame.plot, and reading a loss curve for overfitting. Chapter 7: The Estimator Pattern Chapter 7 of Python for ML: scikit-learn's fit, predict and transform, train_test_split, StandardScaler and OneHotEncoder, Pipeline and ColumnTransformer, three models and the metrics that score them, cross_val_score and GridSearchCV, and the data leakage a Pipeline prevents. Chapter 8: Tensors and Autograd Chapter 8 of Python for ML: PyTorch tensors, torch.tensor, zeros and randn, dtype and device, .to(), from_numpy and .numpy(), requires_grad and autograd, backward and .grad, torch.no_grad and detach, and the gradients that pile up when you forget to clear them. Chapter 9: Models and the Training Loop Chapter 9 of Python for ML: nn.Module and forward, nn.Linear, nn.ReLU, nn.Sequential and nn.Embedding, CrossEntropyLoss and MSELoss, SGD and AdamW, zero_grad, backward and step, Dataset and DataLoader, batches and epochs, train and eval mode, saving a state_dict, and the bugs that train without an error. Chapter 10: Putting It Together Chapter 10 of Python for ML: one pipeline from a pandas table to NumPy arrays, a scikit-learn baseline, a PyTorch model and a Matplotlib comparison, with the habits around it: printing and asserting shapes, seeding, timing with time.perf_counter and %timeit, and notebooks against scripts.