Types of Machine Learning

Machine Learning · Lesson 2 of 32 7 min read Updated

Not every machine learning problem looks the same. Sometimes you have the right answers for your examples, sometimes you have no answers at all, and sometimes the computer must learn by trying things and seeing what happens. Each situation needs a different type of learning.

In this lesson you will learn the main types, see a small runnable example of each, and practise picking the right type for a problem.

Supervised learning

In supervised learning every training example comes with the correct answer, the label. It is like learning with a teacher who marks every answer. The model learns the link between features and labels, then predicts labels for new examples. Most business uses of machine learning are supervised.

Supervised problems come in two kinds, depending on what you predict:

  • Regression predicts a number: a flat's rent, tomorrow's temperature, a student's marks.
  • Classification predicts a category: spam or not spam, which of three flower species, fraud or genuine.

The same data can give either kind of problem. Here the study hours of eight students are used to predict their marks (regression) and whether they passed (classification).

PythonRun it yourself
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeClassifier

hours = np.array([[1], [2], [3], [4], [5], [6], [7], [8]])
marks = np.array([30, 36, 45, 52, 61, 66, 75, 82])  # numbers -> regression
passed = (marks >= 40).astype(int)                   # categories -> classification

reg = LinearRegression().fit(hours, marks)
clf = DecisionTreeClassifier(random_state=0).fit(hours, passed)

print("Labels for classification:", passed)
print("Predicted marks for 4.5 hours:", round(reg.predict([[4.5]])[0], 1))
print("Pass (1) or fail (0) for 1.5 hours:", clf.predict([[1.5]])[0])
Output
Labels for classification: [0 0 1 1 1 1 1 1]
Predicted marks for 4.5 hours: 55.9
Pass (1) or fail (0) for 1.5 hours: 0

A useful test: if the answer can be "between" two values (56.4 marks makes sense), it is regression. If the answer is one of a fixed set of choices, it is classification. You will study both in detail, starting with Linear Regression and Logistic Regression.

Unsupervised learning

In unsupervised learning there are no labels. The algorithm looks for structure in the features alone. Common tasks are:

  • Clustering: grouping similar samples, such as customers with similar shopping habits.
  • Dimensionality reduction: squeezing many columns into a few while keeping most of the information.
  • Anomaly detection: finding rare, unusual samples, such as a strange login pattern.

Suppose a Lucknow supermarket records how often each customer visits per month and how much they spend. Nobody has labelled the customers, but k-means clustering can still discover groups.

PythonRun it yourself
import numpy as np
from sklearn.cluster import KMeans

rng = np.random.default_rng(42)
# columns: visits per month, average bill in rupees
occasional = rng.normal([2, 400], [1, 80], size=(30, 2))
regular = rng.normal([8, 900], [2, 150], size=(30, 2))
bulk = rng.normal([3, 3000], [1, 300], size=(30, 2))
customers = np.vstack([occasional, regular, bulk])

kmeans = KMeans(n_clusters=3, random_state=42).fit(customers)
for k, centre in enumerate(kmeans.cluster_centers_):
    size = np.sum(kmeans.labels_ == k)
    print(f"Group {k}: {size} customers, {centre[0]:.1f} visits, ₹{centre[1]:.0f} per bill")
Output
Group 0: 29 customers, 7.6 visits, ₹880 per bill
Group 1: 30 customers, 3.0 visits, ₹2976 per bill
Group 2: 31 customers, 2.4 visits, ₹408 per bill

The model was never told which customer belongs where, yet it found the three hidden groups, apart from a borderline customer or two. The group numbers themselves mean nothing; a person must look at each group and name it ("bulk buyers"). You will learn this properly in Clustering: k-means & DBSCAN and Dimensionality Reduction with PCA.

Semi-supervised and self-supervised learning

Labels are often expensive. A hospital may have 10,000 X-rays but a doctor has labelled only 200. Semi-supervised learning uses the few labelled examples together with the many unlabelled ones. In scikit-learn you mark unlabelled samples with the label -1.

PythonRun it yourself
import numpy as np
from sklearn.datasets import load_iris
from sklearn.semi_supervised import LabelSpreading

X, y = load_iris(return_X_y=True)
rng = np.random.default_rng(42)
hidden = rng.random(len(y)) < 0.9          # hide 90% of the labels
y_partial = np.where(hidden, -1, y)
print("Labelled flowers:", np.sum(y_partial != -1), "of", len(y))

model = LabelSpreading(kernel="knn", n_neighbors=7).fit(X, y_partial)
guessed = model.transduction_[hidden]       # labels it filled in
print(f"Correct on the hidden flowers: {np.mean(guessed == y[hidden]):.2f}")
Output
Labelled flowers: 11 of 150
Correct on the hidden flowers: 0.93

With only 11 labelled flowers, the model spread those labels to similar unlabelled flowers and got most of them right.

Self-supervised learning goes further: the labels are created from the data itself. Hide the next word of a sentence and ask the model to predict it, and every sentence on the internet becomes a training example. This is how the large models behind chatbots are first trained, as you will see in LLMs & Generative AI.

Reinforcement learning

In reinforcement learning (RL) there is no fixed dataset. An agent takes actions in an environment and receives rewards. It learns a policy, a strategy for choosing actions, that collects the most reward over time. RL has trained programs that beat world champions at Go, and it is used to fine-tune chatbots with human feedback.

The simplest RL problem is the "multi-armed bandit". A food delivery app has three banner designs and does not know which one gets the most clicks. Each time a user opens the app, the agent picks a banner (action) and sees whether the user clicked (reward). It must balance exploring (trying banners to learn about them) and exploiting (showing the banner that looks best so far).

PythonRun it yourself
import numpy as np

rng = np.random.default_rng(1)
true_click_rate = [0.04, 0.11, 0.07]  # unknown to the agent
estimates = np.zeros(3)                # agent's current guess per banner
shown = np.zeros(3)
epsilon = 0.1                          # explore 10% of the time

for user in range(5000):
    if rng.random() < epsilon:
        banner = rng.integers(3)              # explore: random banner
    else:
        banner = int(np.argmax(estimates))    # exploit: best so far
    reward = rng.random() < true_click_rate[banner]
    shown[banner] += 1
    estimates[banner] += (reward - estimates[banner]) / shown[banner]

print("Times shown:", shown.astype(int))
print("Estimated click rates:", np.round(estimates, 3))
Output
Times shown: [ 897 3940  163]
Estimated click rates: [0.049 0.115 0.061]

The agent was never told the true click rates. By trial and reward it learned that banner 1 is best and showed it most of the time, while still testing the others now and then. Real RL problems, such as a robot learning to walk, have many states and long chains of actions, but the loop is the same: act, get a reward, update.

Choosing the right type

Ask two questions about your problem. Do I have the correct answers for my examples? Is there a fixed dataset, or does the system learn by acting?

SituationTypeExample
Labelled examples, predict a numberSupervised regressionPredict the price of a used car in Pune
Labelled examples, predict a categorySupervised classificationFlag a UPI payment as fraud
No labels, find groups or patternsUnsupervisedSegment a store's customers
A few labels, many unlabelled samplesSemi-supervisedLabel X-rays with little doctor time
Learn by trial, reward and feedbackReinforcementChoose which offer to show each user

Most of this course is about supervised learning, because it covers the majority of real projects, followed by the main unsupervised methods.

Common mistakes

  • Treating category codes as numbers. If classes are stored as 0, 1, 2, the problem is still classification. Predicting "1.4" for a flower species makes no sense.
  • Expecting clusters to have meaning by themselves. Clustering returns group numbers only. You must inspect the groups to decide whether they are useful.
  • Using RL when supervised learning would do. If you already have past examples with correct answers, supervised learning is far simpler and needs no trial and error.
  • Forgetting to explore. An RL agent that always exploits can get stuck on a poor choice it happened to try first.

Key points

  • Supervised learning learns from labelled examples: regression predicts numbers, classification predicts categories.
  • Unsupervised learning finds structure such as clusters without any labels.
  • Semi-supervised learning mixes a few labels with many unlabelled samples; scikit-learn marks unlabelled samples as -1.
  • Self-supervised learning creates labels from the data itself, which is how large chatbot models are first trained.
  • Reinforcement learning learns a policy from actions and rewards, balancing exploration and exploitation.

Exercise

Run the banner example again with epsilon = 0.3 and then with epsilon = 0.0. For each run, also print the total number of clicks the agent collected (add up the rewards in the loop). Which setting earns the most clicks, and why does 0.0 behave strangely?

Show solution
Python
import numpy as np

def run(epsilon):
    rng = np.random.default_rng(1)
    true_click_rate = [0.04, 0.11, 0.07]
    estimates, shown, clicks = np.zeros(3), np.zeros(3), 0
    for user in range(5000):
        if rng.random() < epsilon:
            banner = rng.integers(3)
        else:
            banner = int(np.argmax(estimates))
        reward = rng.random() < true_click_rate[banner]
        clicks += reward
        shown[banner] += 1
        estimates[banner] += (reward - estimates[banner]) / shown[banner]
    return clicks, shown.astype(int)

for eps in [0.0, 0.1, 0.3]:
    clicks, shown = run(eps)
    print(f"epsilon={eps}: {clicks} clicks, shown {shown}")
Output
epsilon=0.0: 210 clicks, shown [5000    0    0]
epsilon=0.1: 506 clicks, shown [ 897 3940  163]
epsilon=0.3: 478 clicks, shown [ 564 3841  595]

With epsilon=0.0 the agent never explores: it picks banner 0 first (all estimates start equal) and never tries the others, so it misses the best banner. With 0.3 it explores too much and wastes views on weak banners. A small amount of exploration earns the most.

Quick Quiz

1. You want to predict the number of passengers on a train next Monday. Which type of problem is this?

2. Which type of learning works without any labels?

3. In reinforcement learning, what does the agent learn?

4. How does scikit-learn mark an unlabelled sample in semi-supervised learning?

Learn Machine Learning with a mentor

Free lessons get you started. Our Data Science & Machine Learning training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call