This book is licensed for the personal use of the buyer. Please do not share or resell the file. Short quotations with attribution are welcome.
The information in this book is provided for education. It is not legal, financial or other professional advice. Product and company names are used only to describe them and belong to their owners.
This is a free sample: the introduction and first chapter.
IntroductionIntroduction
There are two common ways to learn machine learning. The first is to call a library function, get a number back and move on. It is fast, but the algorithm remains a black box, and when results look wrong you have little idea why. The second is to start with dense mathematical notation, which is precise but discourages many capable people before they reach the useful ideas.
This book takes a third path. Every core algorithm is worked through by hand, with datasets small enough to fit on a page. You will compute a regression line, run gradient descent for several steps, calculate a probability with logistic regression, choose a decision-tree split, and watch k-means find its clusters. Each formula is described in words before it appears in symbols, and every calculation is shown.
You need only school-level algebra: adding, multiplying, squaring, and reading a simple formula. A calculator helps. If you later use a library such as scikit-learn, you will know exactly what its functions are doing, which settings matter and what can go wrong.
The route through the book
Chapters 1 to 4 lay the foundations: data and features, linear regression and the idea of loss, gradient descent step by step, and logistic regression for yes/no questions, with the standard ways to measure a classifier.
Chapters 5 to 9 add the main families of models: k-nearest neighbours, naive Bayes, decision trees and random forests, gradient boosting, and a neural network trained by hand through a full forward and backward pass.
Chapters 10 and 11 are about trust: overfitting, validation and regularisation, then the metrics, curves and error bars that tell you how good a model really is.
Chapters 12 and 13 cover learning without labels: k-means clustering and principal component analysis.
Chapters 14 and 15 turn algorithms into results: feature engineering, then a complete project from question to monitoring.
Appendix A contains ten practice problems with full solutions, and Appendix B is a short maths refresher covering every tool the book uses. If a formula ever feels unfamiliar, turn to Appendix B first. Appendix C shows where each idea lives in a standard Python library, so you can move from pencil to code.
How to work through it
Read with a pencil. When a table of numbers appears, cover the right-hand columns and try to compute them yourself before checking. The examples are deliberately tiny so that you can do this; the understanding you gain transfers directly to datasets with millions of rows, where only the bookkeeping changes.
Chapter 1Data, Features and Labels
Rows, features and labels
Machine-learning data is usually arranged as a table. Each row is one example: a house, a customer, an email. Each column describing the example is a feature (also called an input or variable). The column we want to predict is the label (or target).
Customer
Visits last month
Average basket (€)
Has app
Bought again?
1
4
35
yes
yes
2
1
12
no
no
3
6
22
yes
yes
Here "visits", "average basket" and "has app" are features; "bought again?" is the label.
Kinds of features
Numeric: quantities such as age, price and distance.
Categorical: categories such as colour, country or plan type. These must be converted to numbers. The usual way is one-hot encoding: a column "plan" with values basic/plus/pro becomes three columns (is_basic, is_plus, is_pro), each 0 or 1. Assigning basic=1, plus=2, pro=3 would wrongly suggest that "pro" is three times "basic".
Boolean: yes/no, encoded as 1/0.
Text, images, time: need special processing to become numbers (word counts, pixel values, day-of-week and so on).
Scaling
Many algorithms compare or combine features numerically. If one feature ranges from 0 to 1 and another from 0 to 100,000, the large one can dominate simply because of its units. Two common fixes: