Python ML Types of Machine Learning
The four types of machine learning differ in what the examples carry. Labels in the data decide the type.
Labels teach the answers
Each weather example carries its answer. The first answer is play.
labelled = [("sunny", "play"), ("rainy", "stay in"), ("sunny", "play"), ("rainy", "stay in")]
print(len(labelled))
print(labelled[0])Groups form without labels
No example carries a label. The split at 100 still finds two groups.
spends = [20, 25, 200, 220, 30, 210]
small = [s for s in spends if s < 100]
big = [s for s in spends if s >= 100]
print(small)
print(big)A few labels and many plain examples
Three examples carry labels. Seven carry none. That mix is semi-supervised learning.
labelled = ["spam", "ham", "spam"]
unlabelled = ["message"] * 7
print(len(labelled))
print(len(unlabelled))Rewards add up over moves
Right moves add 1 and left moves take 1 away. Four moves end at a score of 2.
score = 0
for move in ["right", "right", "left", "right"]:
if move == "right":
score = score + 1
else:
score = score - 1
print(score)Example
examples = [("email one", "spam"), ("email two", "ham"), ("email three", None), ("email four", None), ("email five", None), ("email six", "spam")]
labelled = [e for e in examples if e[1] is not None]
unlabelled = [e for e in examples if e[1] is None]
print("Total examples:", len(examples))
print("Labelled:", len(labelled))
print("Unlabelled:", len(unlabelled))
print("Semi-supervised uses both kinds.")Key points
- Supervised learning: learning from examples that carry labels
- Unsupervised learning: learning from examples with no labels at all
- Semi-supervised learning: learning from a few labelled examples and many unlabelled ones
- Reinforcement learning: learning from rewards for actions, like points in a game
- Use the labels to pick the type. Labels mean supervised, and no labels mean unsupervised.
- A reward is a number that says how good one action was.
In this example, the six emails form one mixed set. Three carry labels and three carry none. A supervised program would use the three labelled emails. An unsupervised program would use all six without their labels. Semi-supervised learning uses both kinds at once. The labels decide the type, and the mix shows why a third type exists.
Note
One project can mix types. A program can learn from labels and from rewards at once.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which type learns from examples with no labels?