Practical, runnable examples and study notes for learning machine learning with Python, from NumPy and data preparation through classical algorithms, PyTorch, and model persistence.
This repository contains code examples, implementations, and study notes based on the book "Machine Learning with Python Cookbook: Practical Solutions from Preprocessing to Deep Learning" (2nd Edition, 2023/2024) by Kyle Gallatin and Chris Albon.
The 2nd edition of this practical guide covers over 200 self-contained recipes for building machine learning applications using the modern Python ecosystem. It transitions from basic data manipulation to advanced ML algorithms, model evaluation, deep learning, and deployment-ready pipelines.
- Core Libraries: Python 3.10+, NumPy, Pandas, SciPy
- Classical ML & Pipeline: Scikit-Learn
- Deep Learning: PyTorch
- Data Visualization: Matplotlib, Seaborn
Each chapter is self-contained and includes its own requirements.txt where dependencies are needed. Create a virtual environment inside the chapter directory, install its requirements, and open the notebook in VS Code or Jupyter:
cd 23_saving_and_loading_models
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtThe notebooks are educational examples rather than a packaged Python library. Some chapters use larger frameworks or local data, so follow the requirements file for the chapter you are running.
Bug reports, corrections, clearer explanations, and focused examples are welcome. See CONTRIBUTING.md before opening a pull request. Please also follow the Code of Conduct.
The repository is organized by chapter, following the book's structure:
.
├── 01_vectors_matrices_arrays/ # NumPy operations, matrix math, properties
├── 02_loading_data/ # CSV, JSON, SQL, Excel, and synthetic dataset generation
├── 03_data_wrangling/ # Filtering, grouping, merging, and transforming DataFrames (Pandas)
├── 04_handling_numerical_data/ # Rescaling, normalizing, binning, outlier detection
├── 05_handling_categorical_data/ # One-Hot encoding, ordinal encoding, target encoding
├── 06_handling_text/ # Bag of Words, TF-IDF, cleaning, tokenization
├── 07_handling_dates_and_times/ # Timezones, lag features, rolling windows, date parsing
├── 08_handling_images/ # Resizing, cropping, filtering, and feature extraction (OpenCV/PIL)
├── 09_dimensionality_reduction/ # PCA, LDA, Truncated SVD, TSNE
├── 10_feature_selection/ # Variance threshold, high correlation filter, recursive feature elimination (RFE)
├── 11_model_evaluation/ # Cross-validation, confusion matrices, ROC-AUC, classification reports
├── 12_model_selection/ # Grid search, random search, hyperparameter optimization
├── 13_linear_regression/ # OLS, Ridge, Lasso, ElasticNet
├── 14_trees_and_forests/ # Decision Trees, Random Forests, Gradient Boosted Trees
├── 15_k_nearest_neighbors/ # KNN classification and regression, distance metrics
├── 16_logistic_regression/ # Binary and multiclass logistic regression, L1/L2 regularization
├── 17_support_vector_machines/ # Linear SVM, RBF kernels, hyperparameter tuning
├── 18_naive_bayes/ # Gaussian, Multinomial, and Bernoulli Naive Bayes
├── 19_clustering/ # K-Means, DBSCAN, Hierarchical Clustering
├── 20_tensors_in_pytorch/ # Tensors in PyTorch
├── 21_neural_networks/ # Intro to PyTorch, tensors, training loops, activation functions
├── 22_nn_with_unstructured_data/ # Neural networks with unstructured data
├── 23_saving_and_loading_models/ # Joblib, Pickle, PyTorch checkpointing
└── README.md