Skip to content

Latest commit

 

History

History
executable file
·
474 lines (328 loc) · 56.1 KB

README.md

File metadata and controls

executable file
·
474 lines (328 loc) · 56.1 KB

Learn machine learning by building all the fundamentals from scratch - with Python

Welcome!

Learn machine learning from the ground up - using Python and a handful of fundamental tools.

This repository contains a range of resources associated with the 2nd edition of the university textbook Machine Learning Refined. Our pedagogical approach stresses intuition, visualization, and "getting your hands dirty" building real machine learning models from scratch. The only technical prerequisites is a basic understanding of Python and matrix maths.

Independent learners

Are you an independent learner aiming to build a solid foundation in machine learning? Get started fast by

When you're ready - follow our installation instructions to run the notes locally or get started on a range of exercises. Always feel free to reach out to us with questions by filing an issue in this repository. A physical copy of the text may be procured via online retailers like these.

Instructors

Are you an instructor looking use the text in an upcoming course? You're in good company!.

Get started fast by

Instructor requests are most easily answered by filing an issue in this repository.

Verified college and university instructors may request a free physical copy of the text for examination via the publisher's website.

Our pedagogy

(Back to top)

We believe mastery of a machine learning concept/topic is achieved only when the answer to each of the following three questions is affirmative.

  1. Intuition Can you describe the idea with a simple picture?
  2. Mathematical derivation Can you express your intuition in mathematical notation and derive underlying models/cost functions?
  3. Implementation Can you code up your derivations in a programming language, say Python, without using high-level libraries?

We wrote our text with the aim of empowering readers to grasp the concepts of classic machine learning at this high standard.

Online notes

(Back to top)

Early drafts of the 2nd edition were released as Jupyter / Collab notebooks and are shared below.

While these allow for interesting and unique interactivity, the final draft significantly expands on this content and is available for download as a PDF here.




Chapter 1. Introduction to Machine Learning (Download Chapter PDF 📄)

1.1 Introduction
1.2 Distinguishing Cats from Dogs: a Machine Learning Approach
1.3 The Basic Taxonomy of Machine Learning Problems
1.4 Mathematical Optimization
1.5 Conclusion

Chapter 2. Zero-Order Optimization Techniques (Download Chapter PDF 📄)

2.1 Introduction 2.1
2.2 The Zero-Order Optimality Condition 2.2
2.3 Global Optimization Methods 2.3
2.4 Local Optimization Methods 2.4
2.5 Random Search 2.5
2.6 Coordinate Search and Descent 2.6
2.7 Conclusion
2.8 Exercises

Chapter 3. First-Order Optimization Techniques (Download Chapter PDF 📄)

3.1 Introduction 3.1
3.2 The First-Order Optimality Condition 3.2
3.3 The Geometry of First-Order Taylor Series 3.3
3.4 Computing Gradients Efficiently
3.5 Gradient Descent 3.5
3.6 Two Natural Weaknesses of Gradient Descent 3.6
3.7 Conclusion
3.8 Exercises

Chapter 4. Second-Order Optimization Techniques (Download Chapter PDF 📄)

4.1 The Second-Order Optimality Condition 4.1
4.2 The Geometry of Second-Order Taylor Series 4.2
4.3 Newton’s Method 4.3
4.4 Two Natural Weaknesses of Newton’s Method 4.4
4.5 Conclusion
4.6 Exercises

Chapter 5. Linear Regression (Download Chapter PDF 📄)

5.1 Introduction
5.2 Least Squares Linear Regression 5.2
5.3 Least Absolute Deviations 5.3
5.4 Regression Quality Metrics 5.4
5.5 Weighted Regression 5.5
5.6 Multi-Output Regression 5.6
5.7 Conclusion
5.8 Exercises
5.9 Endnotes: Probabilistic interpretation of linear regression 5.9 \

Chapter 6. Linear Two-Class Classification (Download Chapter PDF 📄)

6.1 Introduction
6.2 Logistic Regression and the Cross Entropy Cost 6.2
6.3 Logistic Regression and the Softmax Cost 6.3
6.4 The Perceptron 6.4
6.5 Support Vector Machines 6.5
6.6 Which Approach Produces the Best Results? 6.6
6.7 The Categorical Cross Entropy Cost 6.7
6.8 Classification Quality Metrics 6.8
6.9 Weighted Two-Class Classification 6.9
6.10 Conclusion
6.11 Exercises

Chapter 7. Linear Multi-Class Classification (Download Chapter PDF 📄)

7.1 Introduction
7.2 One-versus-All Multi-Class Classification 7.2
7.3 Multi-Class Classification and the Perceptron 7.3
7.4 Which Approach Produces the Best Results? 7.4
7.5 The Categorical Cross Entropy Cost Function 7.5
7.6 Classification Quality Metrics 7.6
7.7 Weighted Multi-Class Classification
7.8 Stochastic and Mini-Batch Learning 7.8
7.9 Conclusion
7.10 Exercises

Chapter 8. Linear Unsupervised Learning (Download Chapter PDF 📄)

8.1 Introduction
8.2 Fixed Spanning Sets, Orthonormality, and Projections 8.2
8.3 The Linear Autoencoder and Principal Component Analysis 8.3
8.4 Recommender Systems 8.4
8.5 K-Means Clustering 8.5
8.6 General Matrix Factorization Techniques 7.8
8.7 Conclusion
8.8 Exercises
8.9 Endnotes

Chapter 9. Feature Engineering and Selection (Download Chapter PDF 📄)

9.1 Introduction
9.2 Histogram Features 9.2
9.3 Feature Scaling via Standard Normalization 9.3
9.4 Imputing Missing Values in a Dataset 9.4
9.5 Feature Scaling via PCA-Sphering 9.5
9.6 Feature Selection via Boosting 9.6
9.7 Feature Selection via Regularization 9.7
9.8 Conclusion
9.9 Exercises

Chapter 10. Principles of Nonlinear Feature Engineering (Download Chapter PDF 📄)

10.1 Introduction 10.1 \
10.2 Nonlinear Regression 10.2
10.3 Nonlinear Multi-Output Regression 10.3
10.4 Nonlinear Two-Class Classification 10.4
10.5 Nonlinear Multi-Class Classification 10.5
10.6 Nonlinear Unsupervised Learning 10.6
10.7 Conclusion
10.8 Exercises

Chapter 11. Principles of Feature Learning (Download Chapter PDF 📄)

11.1 Introduction 11.1
11.2 Universal Approximators 11.2
11.3 Universal Approximation of Real Data 11.3
11.4 Naive Cross-Validation 11.4
11.5 Efficient Cross-Validation via Boosting 11.5
11.6 Efficient Cross-Validation via Regularization 11.6
11.7 Testing Data
11.8 Which Universal Approximator Works Best in Practice?
11.9 Bagging Cross-Validated Models 11.9
11.10 K-Fold Cross-Validation 11.10
11.11 When Feature Learning Fails
11.12 Conclusion
11.13 Exercises

Chapter 12. Kernel Methods (Download Chapter PDF 📄)

12.1 Introduction
12.2 Fixed-Shape Universal Approximators
12.3 The Kernel Trick
12.4 Kernels as Measures of Similarity
12.5 Optimization of Kernelized Models
12.6 Cross-Validating Kernelized Learners
12.7 Conclusion
12.8 Exercises

Chapter 13. Fully Connected Neural Networks (Download Chapter PDF 📄)

13.1 Introduction
13.2 Fully Connected Neural Networks 13.2
13.3 Activation Functions 13.3
13.4 The Backpropagation Algorithm
13.5 Optimization of Neural Network Models 13.5
13.6 Batch Normalization 13.6
13.7 Cross-Validation via Early Stopping 13.7
13.8 Conclusion
13.9 Exercises

Chapter 14. Tree-Based Learners (Download Chapter PDF 📄)

14.1 Introduction
14.2 From Stumps to Deep Trees
14.3 Regression Trees
14.4 Classification Trees
14.5 Gradient Boosting
14.6 Random Forests
14.7 Cross-Validation Techniques for Recursively Defined Trees
14.8 Conclusion
14.9 Exercises

Appendix A. Advanced First- and Second-Order Optimization Methods (Download Chapter PDF 📄)

A.1 Introduction
A.2 Momentum-Accelerated Gradient Descent A.2
A.3 Normalized Gradient Descent A.3
A.4 Advanced Gradient-Based Methods A.4
A.5 Mini-Batch Optimization A.5
A.6 Conservative Steplength Rules A.6
A.7 General Steepest Descent A.7
A.8 Newton’s Method, Regularization, and Nonconvex Functions A.8
A.9 Hessian-Free Methods A.9

Appendix B. Derivatives and Automatic Differentiation (Download Chapter PDF 📄)

B.1 Introduction
B.2 The Derivative
B.3 Derivative Rules for Elementary Functions and Operations
B.4 The Gradient
B.5 The Computation Graph
B.6 The Forward Mode of Automatic Differentiation
B.7 The Reverse Mode of Automatic Differentiation
B.8 Higher-Order Derivatives
B.9 Taylor Series
B.10 Using the autograd Library B.10

Appendix C. Linear Algebra (Download Chapter PDF 📄)

C.1 Introduction
C.2 Vectors and Vector Operations 16.2
C.3 Matrices and Matrix Operations 16.3
C.4 Eigenvalues and Eigenvectors 16.4
C.5 Vector and Matrix Norms 16.5

How to use the book?

(Back to top)

Example ”roadmaps” shown below provide suggested paths for navigating the text based on a variety of learning outcomes and university courses taught using the present book.

Recommended study roadmap for a course on the essentials of machine learning, including requisite chapters (left column), sections (middle column), and corresponding topics (right column). This essentials plan is suitable for time-constrained courses (in quarter-based programs and universities) or self-study, or where machine learning is not the sole focus but a key component of some broader course of study.




Recommended study roadmap for a full treatment of standard machine learning subjects, including chapters, sections, as well as corresponding topics to cover. This plan entails a more in-depth coverage of machine learning topics compared to the essentials roadmap given above, and is best suited for senior undergraduate/early graduate students in semester-based programs and passionate independent readers.




Recommended study roadmap for a course on mathematical optimization for machine learning and deep learning, including chapters, sections, as well as topics to cover.




Recommended study roadmap for an introductory portion of a course on deep learning, including chapters, sections, as well as topics to cover.




Technical prerequisites

(Back to top)

To make full use of the text one needs only a basic understanding of vector algebra (mathematical functions, vector arithmetic, etc.) and computer programming (for example, basic proficiency with a dynamically typed language like Python). We provide complete introductory treatments of other prerequisite topics including linear algebra, vector calculus, and automatic differentiation in the appendices of the text.

Software installation and dependencies

(Back to top)

Google Collab !

The majority of the notes and exercise wrappers in this repository can be run without the need to install anything locally - for free on Google Collab. Click the Collab sticker collab sticker at the top of a notebook to open it in Collab.

Running locally

After cloning this repository and entering the directory we recommend one of three methods for successfully running the Jupyter notebooks contained therein.

Docker method

After installing docker and docker-compose on your machine
traverse to this repo at your terminal and type

docker-compose up -d

When running this command the first time an associated docker image is pulled from DockerHub.

Then in any web browser go to

localhost:8888

to view the repository contents - including jupyter notebooks.

Anaconda method

After installing Anaconda Python 3 distribution on your machine, cd into this repo's directory and follow these steps to create a conda virtual environment to view its contents and notebooks. Python 3.10 and up is required.

First, create the environment

conda create python=3.10 --name mlr2 --file requirements.txt

Then activate it

conda activate mlr2

Run jupyter via the command below

jupyter notebook --port=8888 --ip=0.0.0.0 --allow-root --NotebookApp.token=''

And finally, open any web browser and traverse to

localhost:8888

to view the repository contents - including jupyter notebooks.

pip / uv method

Using Python 3.10 or above and pip or uv on your machine, cd into this repo's directory and follow these steps to install the required packages.

First create a virtual environment and activate it - for example with uv

uv venv --python 3.10.0 && source .venv/bin/activate 

Then install Python requirements

uv pip install -r requirements.txt

Run jupyter via the command below

jupyter notebook --port=8888 --ip=0.0.0.0 --allow-root --NotebookApp.token=''

And finally, open any web browser and traverse to

localhost:8888

to view the repository contents - including jupyter notebooks.

Reviews and Endorsements

(Back to top)

An excellent book that treats the fundamentals of machine learning from basic principles to practical implementation. The book is suitable as a text for senior-level and first-year graduate courses in engineering and computer science. It is well organized and covers basic concepts and algorithms in mathematical optimization methods, linear learning, and nonlinear learning techniques. The book is nicely illustrated in multiple colors and contains numerous examples and coding exercises using Python.

John G. Proakis, University of California, San Diego

Some machine learning books cover only programming aspects, often relying on outdated software tools; some focus exclusively on neural networks; others, solely on theoretical foundations; and yet more books detail advanced topics for the specialist. This fully revised and expanded text provides a broad and accessible introduction to machine learning for engineering and computer science students. The presentation builds on first principles and geometric intuition, while offering real-world examples, commented implementations in Python, and computational exercises. I expect this book to become a key resource for students and researchers.

Osvaldo Simeone, King's College, London

This book is great for getting started in machine learning. It builds up the tools of the trade from first principles, provides lots of examples, and explains one thing at a time at a steady pace. The level of detail and runnable code show what's really going when we run a learning algorithm.

David Duvenaud, University of Toronto

This book covers various essential machine learning methods (e.g., regression, classification, clustering, dimensionality reduction, and deep learning) from a unified mathematical perspective of seeking the optimal model parameters that minimize a cost function. Every method is explained in a comprehensive, intuitive way, and mathematical understanding is aided and enhanced with many geometric illustrations and elegant Python implementations.

Kimiaki Sihrahama, Kindai University, Japan

Books featuring machine learning are many, but those which are simple, intuitive, and yet theoretical are extraordinary 'outliers'. This book is a fantastic and easy way to launch yourself into the exciting world of machine learning, grasp its core concepts, and code them up in Python or Matlab. It was my inspiring guide in preparing my 'Machine Learning Blinks' on my BASIRA YouTube channel for both undergraduate and graduate levels.

Islem Rekik, Director of the Brain And SIgnal Research and Analysis (BASIRA) Laboratory

A sample of widgets from the notes

(Back to top)

You'll find a large number of intuitive animations in the notes of this repository.



Cross-validation (regression) Cross-validation (two-class classification) Cross-validation (multi-class classification)



K-means clustering Feature normalization Normalized gradient descent



Rotation Convexification Dogification!



A nonlinear transformation Weighted classification The moving average



Batch normalization Logistic regression



Polynomials vs. NNs vs. Trees (regression) Polynomials vs. NNs vs. Trees (classification)



Changing gradient descent's steplength (1d) Changing gradient descent's steplength (2d)



Convex combination of two functions Taylor series approximation



Feature selection via regularization Secant planes



Function approximation with a neural network A regression tree

Get a physical copy of the book

(Back to top)