Types of Machine Learning
Machine learning comes in a few main types, depending on what kind of data you have.
The Three Main Types
| Type | Data you have | Goal | Example |
|---|---|---|---|
| Supervised learning | Examples with correct answers | Predict the answer for new data | Is this email spam? |
| Unsupervised learning | Examples without answers | Find groups or patterns | Group customers by shopping habits |
| Reinforcement learning | No examples, only rewards | Learn the best actions by trying | A robot learning to walk |
Supervised Learning
In supervised learning every example has a label, the correct answer. It is like a student practising with a past paper that has the answers at the back.
Supervised learning has two kinds of problems:
- Classification: the answer is a category, like "spam" or "not spam", "apple" or "guava"
- Regression: the answer is a number, like a price, a temperature or marks
Here a fruit seller in Karachi trains a model to tell apples from guavas using weight and a colour score (10 = very red):
Example
from sklearn.neighbors import KNeighborsClassifier
# Features: [weight in grams, colour score 1-10]
X = [[150, 8], [170, 9], [130, 7], [120, 2], [110, 3], [100, 2]]
y = ["apple", "apple", "apple", "guava", "guava", "guava"]
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
print(model.predict([[160, 8], [105, 3]]))['apple' 'guava']
K-Nearest Neighbors looks at the 3 most similar fruits it has seen and takes a vote. A heavy, red fruit is most like the apples.
Unsupervised Learning
In unsupervised learning there are no labels. The model looks for structure on its own.
The most common task is clustering: putting similar items into groups. A shop owner might want to find types of customers without knowing the types in advance:
Example
from sklearn.cluster import KMeans
# Each customer: [visits per month, average bill in thousand Rs]
customers = [[2, 1], [3, 1.5], [2, 2], [12, 8], [10, 9], [11, 7.5]]
kmeans = KMeans(n_clusters=2, random_state=0, n_init=10)
kmeans.fit(customers)
print(kmeans.labels_)[1 1 1 0 0 0]
The model found two groups: the first three customers are occasional, small buyers, and the last three are frequent, big spenders. Notice that fit() received only customers, with no y.
The group numbers 0 and 1 are just names. The model may call the big spenders 0 or 1; what matters is which customers end up together.
Other unsupervised tasks include dimensionality reduction (shrinking many features into a few) and anomaly detection (finding unusual items, like a strange bank transaction).
Reinforcement Learning
In reinforcement learning, an agent takes actions in an environment and gets a reward or a penalty. Over many tries, it learns which actions give the most reward.
- A game-playing program gets points for winning
- A robot gets a reward for moving forward without falling
- Chat models are partly tuned with feedback from people, rating which answers are better
Reinforcement learning needs special libraries and a lot of trial and error, so scikit-learn does not cover it. This tutorial focuses on supervised and unsupervised learning, which solve most everyday business problems.
Which Type Should I Use?
- You have past data with the answer you want to predict: supervised
- The answer is a category: classification
- The answer is a number: regression
- You have data but no answers and want to find groups: unsupervised
- A system must learn by acting and getting feedback: reinforcement
Exercise
Exercise
A bank wants to predict the exact amount a customer will spend next month, using past data. Which type of problem is this?
