Lesson 1 of 227 min
What machine learning is
Machine learning is a computer finding the rule in examples, instead of a person writing it; what the examples' label is decides the kind of job.
After this lesson you can tell learning from examples apart from writing rules, and name the main kinds.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
Ask the tree about a new penguin
Once a model is trained, you use it with predict. Give it the same columns it learned from, as a table, and it gives back a kind. Here is the video's tree again, in a few lines, asked about a penguin with a 210 mm flipper and a 45 mm bill. The file is penguins.csv from Python for AI.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
cols = ["flipper_length_mm", "bill_length_mm"]
df = pd.read_csv("penguins.csv").dropna(subset=cols)
X_train, X_test, y_train, y_test = train_test_split(
df[cols], df["species"], test_size=0.25, stratify=df["species"], random_state=0)
tree = DecisionTreeClassifier(max_depth=2, random_state=0).fit(X_train, y_train)
print(tree.predict(pd.DataFrame([[210, 45]], columns=cols)))['Gentoo']
Draw the rule as a tree
export_text prints the rule. plot_tree draws the same rule as boxes: the first question at the top, a branch for each answer, and the kinds at the bottom. Each box also says how many penguins reached it. How the tree picks each cut is the Decision Trees lesson.
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree
plot_tree(tree, feature_names=cols, class_names=tree.classes_, filled=True)
plt.show()The names you will read
The kinds of learning are named by where the labels come from. Labels a person gave, like the species, make it supervised. No labels, like the video's three groups, make it unsupervised. Labels from the data itself, like the next word, make it self-supervised. Here are the groups again, held against the species.
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
Z = StandardScaler().fit_transform(df[cols])
groups = KMeans(3, n_init=10, random_state=0).fit_predict(Z)
print(pd.crosstab(groups, df["species"]))species Adelie Chinstrap Gentoo row_0 0 4 59 1 1 1 4 122 2 146 5 0
The model that reads the digits
The video's digit model is k-nearest neighbors. It answers a new digit with the answer most of the five closest examples have. It takes the same four steps as the tree: read, keep some aside, fit, score. It learned from 1,347 digits and got 441 of the 450 kept aside right. The k-Nearest Neighbors lesson teaches how it works.
from sklearn.datasets import load_digits
from sklearn.neighbors import KNeighborsClassifier
digits = load_digits()
X_tr, X_te, y_tr, y_te = train_test_split(
digits.data, digits.target, test_size=0.25, stratify=digits.target, random_state=0)
knn = KNeighborsClassifier().fit(X_tr, y_tr)
print(len(X_tr), (knn.predict(X_te) == y_te).sum(), knn.score(X_te, y_te))1347 441 0.98
Run on Python 3.13.3, scikit-learn 1.9.1, pandas 3.1.1 and matplotlib 3.11.2. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
On the 86 penguins kept aside, the rule by hand got 83 right and the computer's rule 82. So why learn a rule from examples at all?
2 of 2
On the 86 penguins kept aside, tree.score(X_test, y_test) printed 0.953: the share it got right, 82 of 86. The rule by hand got 83 of the 86 right. Written as the same kind of score, what is it?
The ideas in this lesson
- Features and a label
- Each example has features, what the model reads, and a label, the answer it learns to give.
- Machine learning
- A computer finds a rule in examples instead of a person writing it; that finding is called training.
- Classification and regression
- When the label is a kind, the job is classification; when it is a number, it is regression.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
Machine Learning
How do you build a model you can trust? Training it takes one line of code. Checking it is the real work. Hi, I'm Mohi Fayden. Machine learning means a computer learns a rule from examples. Spam filters learn this way. So do chatbots. This course teaches you to train, test and choose models for tables. By the end, you'll know when to trust your own model. To start, you need school math and a little Python. Every lesson has its own page on faydenai.com, with a quiz. There's a lab every few lessons, and a project at the end.
The bet
Here's a bet. A person writes a rule to tell penguins apart. A computer finds its own rule, from examples. Whose rule gets more new penguins right?
Keep your guess
Hold on to your guess. We'll test both rules on penguins that nobody has seen.
Penguins kept aside
These are the penguins from Python for AI, one dot for each penguin. Across is its flipper length, and up is its bill length. There are three kinds: Adelie, Chinstrap and Gentoo.
Before anyone writes a rule, we keep eighty-six of them aside, sealed. Nobody sees them until the end. That leaves two hundred and fifty-six to learn from.
A rule by hand
Where would you cut?
Look at the dots. The Gentoos have the long flippers. So here's a first cut: a flipper over two hundred and six millimeters is a Gentoo.
Of the other penguins, the Chinstraps have the long bills. A bill over forty-three millimeters is a Chinstrap.
Everything else is an Adelie. That's a rule by hand. It gets two hundred and forty of them right.
Features and a label
A computer doesn't get a rule. It only gets examples, and each penguin is one example. This one has a flipper of one hundred and eighty-one millimeters, and a bill of thirty-nine. It's an Adelie. The computer reads the flipper and the bill. Those are called the features. It learns to give one answer, the species. That's called the label.
Can it find a rule?
So here are two hundred and fifty-six penguins, each with its features and its label. And no rule. How could a computer find one?
Will its rule look like the rule by hand?
The rule it finds
It tries many cuts, and keeps the ones that best split the kinds apart. It asks each penguin two questions in a row, like the branches of a tree: first the flipper, then the bill. Here's what it finds. A flipper of two hundred and six and a half millimeters.
A bill of just over forty-three. That's the rule by hand, within half a millimeter. Among the long flippers, it adds one more cut on the bill. That's for a few Adelies with long flippers. Nobody gave it a rule. It only had the examples.
Machine learning
A computer finding the rule in examples, instead of a person writing it... is called machine learning. Finding it is called training. And what it finds is a model: it takes a penguin's measurements, and gives back a kind. Here, the model's numbers are the cuts.
One apart
Now the puzzle. Open the eighty-six penguins kept aside.
The rule by hand gets eighty-three right. The computer's rule gets... eighty-two.
And always saying "Adelie", the most common kind, gets thirty-eight.
So the rule by hand wins, by one.
Given only examples, the computer did about as well as a person who looked.
Where no rule can be written
So why learn at all, if a person can write the rule?
Because for most jobs, nobody can. Here's a handwritten digit from Python for AI. It's sixty-four numbers, each one saying how dark a small square is. Which of these numbers make a three? Try to say it as a rule.
The examples hold it
Nobody can write that rule by hand. But a model learns it from about thirteen hundred examples, each a picture with its digit. Then it reads four hundred and fifty new digits, and gets all but nine right.
Spam is the same. Nobody can write its rule, but emails that people marked as spam are easy to collect. So when nobody can write the rule, you can still learn it from examples.
Classification and regression
The label decides the job. When the label is a kind, like the species or the digit, the job is called classification. Now make the label each penguin's mass, a number. Is that the same job?
No. When the label is a number, the job is called regression.
No label
And with no label at all? Hide the species. A model can still find groups in the dots: three of them, close to the three kinds. Finding groups with no label is called clustering. It's a later lesson.
A chatbot's labels
And a chatbot? Where do its labels come from?
Its model learns from text. In text, the label of each word is the word that comes next.
So nobody has to write the labels. Before it writes, the model works out a chance for every word it knows. So picking the next word is classification too, with one kind for every word: about two hundred and fifty thousand.
In code
Now in real code, with scikit-learn. Read the penguins, and keep a quarter of them aside, sealed. Why we do that is a lesson of its own. Make a tree that can ask two questions. Then fit it on the rest of the penguins. That one line is the training. Print the rule it learned. Its cuts are the ones you saw in the picture. Score gives the share it gets right. What will it say, on the penguins kept aside?
Zero point nine five. That's about ninety-five out of every hundred: eighty-two of the eighty-six.
The same few lines work on any table. Change the file and the columns.
Three ideas
So: the features go in, and a label comes out. Machine learning finds the rule in the examples. A kind is classification. A number is regression.
A rule found in the examples
So machine learning finds the rule in the examples. Nobody has to write it. Now try it on this lesson's page, and take the quiz. Let the computer ask as many questions as it likes.
You use this lesson in Lab 1: A first model, judged on new penguins, after Linear regression.
Facts last checked on .
