Published on

๐Ÿง  AI Exploration #5: Classification in Supervised Learning

Authors

๐Ÿง  AI Exploration #5: Classification in Supervised Learning

Classification is a key branch of supervised learning focused on predicting categories or class labels. Whether you're building a spam detector or a disease classifier, classification is everywhere.

In this post, we'll explore how classification works, common algorithms, loss functions, evaluation metrics, and a practical code example.


๐Ÿง  What is Classification?

Classification is a supervised learning task where the goal is to assign inputs to one of several discrete categories.

In contrast to regression (which predicts continuous values), classification outputs labels like spam / not spam, cat / dog, or positive / negative.


๐Ÿ“ฆ Real-Life Example: Email Spam Detection

  • Input (X): Email text content, sender, subject line
  • Output (Y): Label (Spam or Not Spam)
  • The model learns from thousands of labeled emails to identify spam patterns.

๐Ÿงช Types of Classification

TypeDescriptionExamples
Binary ClassificationTwo possible classesSpam detection, tumor: benign/malignant
Multiclass ClassificationMore than two classesDigit recognition (0โ€“9), animal type
Multilabel ClassificationAssign multiple labels to one instanceTagging articles with multiple topics

๐Ÿ”‘ Common Algorithms

AlgorithmDescriptionBest For
Logistic RegressionLinear classifier for binary/multiclassText, tabular data
Decision TreeTree-based splits on featuresInterpretable models
Random ForestEnsemble of treesRobust multiclass tasks
Naive BayesProbabilistic modelSpam filtering, sentiment analysis
k-NNClassifies based on nearest neighborsSmall datasets
SVMFinds optimal margin between classesHigh-dimensional data
Neural NetworksDeep models for complex dataImage, audio, text

๐Ÿ“‰ Loss Functions

๐Ÿ”น Cross-Entropy Loss (Log Loss)

For binary classification:

Loss=โˆ’[ylogโก(y^)+(1โˆ’y)logโก(1โˆ’y^)]\text{Loss} = -[y \log(\hat{y}) + (1 - y) \log(1 - \hat{y})]

For multiclass:

Loss=โˆ’โˆ‘i=1Cyilogโก(y^i)\text{Loss} = -\sum_{i=1}^{C} y_i \log(\hat{y}_i)

This penalizes confident but incorrect predictions more heavily.


๐Ÿ“Š Evaluation Metrics

MetricUse CaseNotes
AccuracyOverall correctnessGood for balanced datasets
PrecisionOf predicted positives, how many are correctImportant for reducing false positives
RecallOf actual positives, how many are foundImportant for catching all positives
F1 ScoreHarmonic mean of precision and recallBest when classes are imbalanced
Confusion MatrixDetailed view of TP, FP, FN, TNGreat for multi-class diagnostics

๐Ÿงช Code Example: Classifying Iris Species

Weโ€™ll use the classic Iris dataset to classify flower species using LogisticRegression.

import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix

# ๐ŸŒธ Load the Iris dataset
data = load_iris()
X = data.data
y = data.target
class_names = data.target_names

# ๐Ÿ”€ Train-test split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# ๐Ÿง  Train a classifier
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)

# ๐Ÿ“ˆ Make predictions
y_pred = model.predict(X_test)

# ๐Ÿ“Š Evaluate
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))

# ๐Ÿ–ผ๏ธ Save confusion matrix plot
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 5))
sns.heatmap(
    cm,
    annot=True,
    fmt="d",
    cmap="Blues",
    xticklabels=class_names,
    yticklabels=class_names,
)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix - Iris Classification")
plt.tight_layout()
plt.savefig("iris_confusion_matrix.png")

๐Ÿ“Š The confusion matrix below (achieved after running the code) shows that the model classified all test samples correctly across all three classes (setosa, versicolor, virginica). This indicates excellent performance on the Iris dataset, with no misclassifications.

Confusion Matrix

This example shows how simple it is to train a multiclass classifier and measure precision, recall, and F1-score.


โœ… When to Use Classification

  • When your output is a class label
  • When you're solving problems like diagnosis, fraud detection, tagging, or recognition
  • When accuracy, precision, or recall is more meaningful than raw numeric error

๐Ÿ”š Recap

Classification is essential for teaching machines how to recognize and label the world. With the right models and evaluation metrics, it powers countless applications - from medical screening to language processing.


๐Ÿ”œ Coming Next

In the next post, weโ€™ll explore Unsupervised Learning - where models learn from unlabeled data to find hidden structures.

Stay curious and keep exploring ๐Ÿ‘‡

๐Ÿ™ Acknowledgments

Special thanks to ChatGPT for enhancing this post with suggestions, formatting, and emojis.