Supervised vs Unsupervised Learning

Python · Machine Learning (Scikit-Learn)

Theory

In supervised learning every training example comes with the answer, called the label or target. The model learns the mapping from features to label and then predicts labels for new rows. If the label is a number, such as a flat's price, it is regression; if it is a category, such as approved or rejected, it is classification. Linear regression, logistic regression, decision trees, random forests and KNN are supervised.

In unsupervised learning there is no label. The algorithm finds structure in the features alone: clustering (KMeans) groups similar customers into segments, and dimensionality reduction (PCA) compresses many columns into a few. In scikit-learn the difference is visible in the code: supervised models call fit(X, y), unsupervised ones call fit(X). Because there is no right answer to compare with, unsupervised results need human interpretation.

Why we use it

The first decision in any ML task is which family of methods applies. Knowing whether you have labels tells you which algorithms, metrics and validation methods you can use.

When to use it

Use supervised learning when historical data has the outcome you want to predict (price, churn, fraud). Use unsupervised learning to explore unlabelled data, segment customers, detect unusual records or reduce features before another model.

How it works

  1. Dataset with features X
  2. Does each row have a known outcome y? Yes: Supervised learning. No: Unsupervised learning.
  3. Supervised: is y a number? Yes: Regression (e.g. price). No: Classification (e.g. approved).
  4. Unsupervised: cluster rows or reduce columns
  5. Choose algorithms and metrics for that family

Syntax

# supervised: learn X -> y
model.fit(X, y); model.predict(X_new)
# unsupervised: structure in X only
kmeans.fit(X); kmeans.labels_
pca.fit_transform(X)

Example

LinearRegression().fit(X, y)          # supervised
KMeans(n_clusters=2, n_init=10).fit(X) # unsupervised

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic