← Back to list

3-Understanding Classification in Machine Learning: A Comprehensive Guide

After discovering [in the previous chapters] the world of Artificial Intelligence (AI), and comparing it with Machine Learning (ML) and…

Imane Abasalah · 2026-04-16 22:18 · 2 claps · 16.4 min read
#mls #sklearn #ai #swe #python
Open on Medium ↗
Wiki topics: ML · Machine Learning AI · AI · General EDU · Education & Learning

3-Understanding Classification in Machine Learning: A Comprehensive Guide

After discovering [in the previous chapters] the world of Artificial Intelligence (AI), and comparing it with Machine Learning (ML) and Deep Learning (DL), we explored the subsets of ML. We started with Supervised Learning, focusing initially on regression and it’s type . Now, we will delve into classification.

classification VS regression

classification VS regression

In the vast landscape of machine learning, classifiers play a pivotal role in transforming raw data into actionable insights. Whether you’re a novice or an experienced practitioner, understanding the different types of classifiers is essential for tackling various predictive modeling tasks.

From the simplicity and efficiency of Naive Bayes and the probabilistic elegance of Stochastic Gradient Descent, to the intuitive, distance-based approach of K-Nearest Neighbors and the hierarchical decision-making of Decision Trees, each classifier brings unique strengths to the table.

For those seeking ensemble methods, Random Forest offers robust performance by aggregating multiple decision trees, while Boruta helps in selecting all relevant features to enhance model accuracy. And let’s not forget the powerhouse Support Vector Machine, renowned for its effectiveness in high-dimensional spaces.

Join us as we take a deeper look at these diverse classifiers, exploring their mechanisms, advantages, and ideal use cases to empower your machine learning journey.

Types of Classifiers

There are various types of classifiers, each with its strengths and ideal use cases. Here are some of the most popular ones:

  1. Naive Bayes:
  • Based on Bayes’ theorem, Naive Bayes assumes that the presence of a particular feature in a class is unrelated to the presence of any other feature.
  • Ideal for text classification and spam filtering.

2. Logistic Regression:

  • Despite its name, logistic regression is a classification algorithm used for predicting binary outcomes.
  • Commonly used in medical diagnosis, marketing, and other binary classification tasks.

3. Decision Trees:

  • Decision trees make decisions based on a series of hierarchical questions about the data features.
  • Easy to interpret and visualize, making them suitable for decision-making processes.

4. K-Nearest Neighbors (K-NN):

  • K-NN classifies data points based on the majority class of their k-nearest neighbors.
  • Effective for small datasets and when the data has a clear, separable structure.

5. Stochastic Gradient Descent (SGD) Classifier:

  • SGD is an optimization method used to train linear classifiers efficiently on large datasets.
  • Often used in text classification and natural language processing.

6. Random Forests:

  • Random forests are ensemble methods that combine multiple decision trees to improve accuracy and control over-fitting.
  • Suitable for complex datasets with high dimensionality.

How Classification Works

Step 1: Read the Nature of Input Data

Understanding the nature of your input data is crucial. This involves exploring the features, identifying patterns, and determining the type of data (structured or unstructured).

Step 2: Determine the Nature of the Problem

Identify whether your problem is a binary classification (two classes) or multi-class classification (more than two classes). This will guide your choice of classifier and evaluation metrics.

Step 3: Decide the Classifier to be Used

Based on the nature of your data and problem, choose an appropriate classifier. Consider factors such as interpretability, computational efficiency, and the classifier’s ability to handle your data type.

Classification on Structured and Unstructured Data

Classifiers can operate on both structured and unstructured data:

  • Structured Data: Data that is organized in a tabular format, such as spreadsheets or SQL tables. Examples include customer records, sales data, and sensor readings.
  • Unstructured Data: Data that does not follow a predefined format, such as text, images, and videos. Examples include social media posts, emails, and satellite images.

Choosing the Right Classifier

Selecting the right classifier depends on several factors, including the nature of your data, the complexity of the problem, and the desired interpretability of the model. Here are some guidelines:

  • Naive Bayes: Best for text classification and when features are independent.
  • Logistic Regression: Ideal for binary classification tasks and when you need a probabilistic interpretation.
  • Decision Trees: Suitable for problems requiring interpretability and when features have a hierarchical relationship.
  • K-Nearest Neighbors: Effective for small datasets with clear separable classes.
  • Stochastic Gradient Descent (SGD): Useful for large datasets and when computational efficiency is important.
  • Random Forests: Best for complex datasets with high dimensionality

and when you need to avoid overfitting.

Classification is a fundamental concept in machine learning that involves categorizing data into distinct classes or categories. It’s a powerful tool used in various applications, from email spam detection to medical diagnosis. Let’s dive into the different types of classification and the algorithms used for each type.

Types of Classification

Classification can be broadly categorized into four main types:

  1. Binary Classification
  2. Multi-class Classification
  3. Multi-label Classification
  4. Imbalanced Classification

Let’s explore each type in detail.

1. Binary Classification

Binary Classification involves categorizing data into one of two possible classes. It’s the simplest form of classification and is widely used in various applications.

Popular Algorithms for Binary Classification:

  • Logistic Regression: Despite its name, logistic regression is used for classification tasks. It predicts the probability that a given input point belongs to a particular class.
  • K-Nearest Neighbors (K-NN): This algorithm classifies data points based on the majority class of their k-nearest neighbors.
  • Decision Tree: Decision trees make decisions based on a series of hierarchical questions about the data features.
  • Support Vector Machine (SVM): SVM finds the optimal hyperplane that best separates the classes in the feature space.
  • Naive Bayes: Naive Bayes classifiers are based on Bayes’ theorem and assume that the presence of a particular feature in a class is unrelated to the presence of any other feature.

2. Multi-class Classification

Multi-class Classification involves categorizing data into one of three or more classes. This type of classification is more complex than binary classification but is essential for many real-world applications.

Examples of Multi-class Classification:

  • Plant Classification: Identifying different species of plants based on their features.
  • Optical Character Recognition (OCR): Recognizing different characters in an image.

Popular Algorithms for Multi-class Classification:

  • K-Nearest Neighbors (K-NN): Works well for multi-class problems by classifying data points based on the majority class of their k-nearest neighbors.
  • Decision Tree: Can handle multiple classes by creating a tree structure where each node represents a feature, each branch represents a decision rule, and each leaf represents a class label.
  • Naive Bayes: Can be extended to multi-class problems by calculating the probability of each class and selecting the class with the highest probability.
  • Random Forest: An ensemble method that combines multiple decision trees to improve accuracy and control over-fitting.
  • Gradient Boost: Another ensemble method that builds trees sequentially, where each new tree corrects the errors of the previous ones.

3. Multi-label Classification

Multi-label Classification involves assigning multiple labels to a single data point. This type of classification is useful when a data point can belong to more than one category simultaneously.

Popular Algorithms for Multi-label Classification:

  • Multi-label Decision Trees: Extends decision trees to handle multiple labels by allowing each leaf node to have multiple class labels.
  • Multi-label Random Forests: An ensemble method that combines multiple multi-label decision trees to improve accuracy.
  • Multi-label Gradient Boosting: Extends gradient boosting to handle multiple labels by building trees sequentially and correcting errors from previous trees.

4. Imbalanced Classification

Imbalanced Classification deals with datasets where the distribution of classes is uneven. This type of classification is challenging because the model can become biased towards the majority class.

Specialized Techniques for Imbalanced Classification:

  • Random Undersampling: Reduces the number of samples in the majority class to balance the class distribution.
  • SMOTE (Synthetic Minority Over-sampling Technique): Generates synthetic samples for the minority class to balance the class distribution.

Choosing the Right Classifier

Selecting the right classifier depends on several factors, including the nature of your data, the complexity of the problem, and the desired interpretability of the model. Here are some guidelines:

  • Binary Classification: Use logistic regression, decision trees, or SVM for simple binary classification tasks.
  • Multi-class Classification: Use K-NN, decision trees, or random forests for multi-class problems.
  • Multi-label Classification: Use multi-label decision trees, random forests, or gradient boosting for problems where a data point can belong to multiple classes.
  • Imbalanced Classification: Use random undersampling or SMOTE to handle datasets with uneven class distributions.

Applications of Classification

Classification is used in various real-world applications, including:

  • Healthcare: Detecting diseases and classifying medical images.
  • Finance: Fraud detection and credit scoring.
  • Marketing: Customer segmentation and targeted advertising.
  • Autonomous Vehicles: Object detection and navigation.
  • Natural Language Processing: Sentiment analysis and text classification.

Let me break down the relationship between confusion matrices, accuracy, precision, recall, and when to use them in a simplified way.

What is a Confusion Matrix?

A confusion matrix is a tool used to evaluate the performance of a classification model. It provides a summary of the predictions made by the model compared to the actual values in other words a confusion matrix is a table that shows the performance of a classification model by comparing the actual target values with the predicted target values. It helps you see where your model is making mistakes and what kinds of errors it is making.

Structure of a Confusion Matrix

For a binary classification problem (two classes), the confusion matrix looks like this:

Predicted Positive (PP) Predicted Negative (PN) Actual Positive (AP) True Positive (TP) False Negative (FN) Actual Negative (AN) False Positive (FP) True Negative (TN)

  • True Positive (TP): Correctly predicted positive instances.
  • False Positive (FP): Negative instances incorrectly predicted as positive.
  • False Negative (FN): Positive instances incorrectly predicted as negative.
  • True Negative (TN): Correctly predicted negative instances.

Metrics Derived from the Confusion Matrix (Performance Metrics)

1. Accuracy

What it is: The proportion of correct predictions (both true positives and true negatives) among the total number of cases examined.

When to use: When you want a general idea of how often the model is correct. However, accuracy can be misleading if the classes are imbalanced (e.g., 95% of the data belongs to one class).

2. Precision:

”Of all the instances predicted as positive, how many are actually positive?”

What it is: The proportion of positive identifications (predicted positives) that were actually correct.

When to use: When the cost of False Positives is high. For example, in email spam detection, you want to minimize the number of legitimate emails marked as spam (False Positives).

3. Recall (Sensitivity or True Positive Rate):

”Of all the actual positive instances, how many did the model correctly identify?”

What it is: The proportion of actual positives that were identified correctly.

When to use: When the cost of False Negatives is high. For example, in medical testing, you want to minimize the number of actual sick people marked as healthy (False Negatives).

4. F1 Score

What it is: The harmonic mean of precision and recall. It provides a balance between precision and recall.

When to use: When you need a balance between precision and recall, especially if you have an uneven class distribution.

When to Use a Confusion Matrix?

  1. Error Analysis: To understand not just the errors but the types of errors the model is making.
  2. Performance Measurement: To get a more detailed view of the model’s performance beyond just accuracy.
  3. Class Imbalance: Particularly useful in cases of imbalanced datasets where accuracy alone can be misleading.

Relation with Classification

A confusion matrix is specifically used in classification problems to evaluate the performance of a classifier. It provides a detailed breakdown of correct and incorrect classifications, which is essential for:

  • Understanding Model Performance: Beyond just knowing the accuracy, you can see how many of each type of error (FP, FN) your model is making.
  • Improving Models: By identifying which types of errors are most common, you can focus on improving those specific areas.
  • Choosing Metrics: Depending on the problem, you might care more about precision, recall, or a balance of both (F1 score).

Practical Example

Suppose you have a model to detect fraudulent transactions (binary classification: fraud or not fraud).

  • True Positive (TP): Fraudulent transactions correctly identified as fraud.
  • False Positive (FP): Legitimate transactions incorrectly identified as fraud.
  • False Negative (FN): Fraudulent transactions incorrectly identified as legitimate.
  • True Negative (TN): Legitimate transactions correctly identified as legitimate.

Confusion Matrix:

Metrics:

  • Accuracy: (50+100)/(50+100+5+10)=0.91(50 + 100) / (50 + 100 + 5 + 10) = 0.91(50+100)/(50+100+5+10)=0.91 or 91%
  • Precision: 50/(50+5)=0.9150 / (50 + 5) = 0.9150/(50+5)=0.91 or 91%
  • Recall: 50/(50+10)=0.8350 / (50 + 10) = 0.8350/(50+10)=0.83 or 83%
  • F1 Score: 2×(0.91×0.83)/(0.91+0.83)=0.872 \times (0.91 \times 0.83) / (0.91 + 0.83) = 0.872×(0.91×0.83)/(0.91+0.83)=0.87 or 87%

Summary

Quand tu utilises un classificateur comme Naive Bayes, les arbres de décision, KNN, ou tout autre algorithme de classification, la matrice de confusion est un outil essentiel pour évaluer si ton modèle prédit bien ou non.

La matrice de confusion est un outil puissant pour évaluer les performances d’un classificateur. Elle te permet de voir non seulement la précision globale du modèle, mais aussi les types spécifiques d’erreurs qu’il commet. Cela te donne des informations précieuses pour améliorer ton modèle et le rendre plus performant.

  • Confusion Matrix: A table showing correct and incorrect predictions.
  • Accuracy: Overall correctness of the model.
  • Precision: Correctness of positive predictions (Measures the accuracy of positive predictions. Use when False Positives are costly.).
  • Recall: Ability to find all positive instances ( Measures the ability to find all positive instances. Use when False Negatives are costly.).
  • F1 Score: Balance between precision and recall (Balances precision and recall. Use when you need a single metric that considers both precision and recall.).

The confusion matrix and its derived metrics are essential tools for evaluating and improving classification models, especially when dealing with imbalanced datasets or when different types of errors have different costs.

1-Naive Bayes

Naive Bayes is a family of probabilistic algorithms based on Bayes’ Theorem with an assumption of independence between predictors. It is particularly useful for classification tasks.

Bayes’ Theorem

The core of Naive Bayes is Bayes’ Theorem, which describes the probability of an event based on prior knowledge of conditions that might be related to the event.

The formula is:

Where:

  • P(A∣B)P(A|B)P(A∣B): Probability of hypothesis AAA given the evidence BBB.
  • P(B∣A)P(B|A)P(B∣A): Probability of the evidence given that the hypothesis is true.
  • P(A)P(A)P(A): Probability of the hypothesis being true (prior probability).
  • P(B)P(B)P(B): Probability of the evidence.

Principle of Contingent Probability

The first image you provided explains the principle of contingent probability, which is essentially Bayes’ Theorem.

  • P(A)P(A)P(A): Probability of hypothesis HHH being true.
  • P(B)P(B)P(B): Probability of evidence.
  • P(B∣A)P(B|A)P(B∣A): Probability of the evidence given that the hypothesis is true.
  • P(A∣B)P(A|B)P(A∣B): Probability of the hypothesis given the evidence.

Types of Naive Bayes Algorithms

The second image outlines the types of Naive Bayes algorithms:

  1. Gaussian Naive Bayes:
  • Assumes that features follow a normal distribution.
  • Suitable for continuous data.
  1. Multinomial Naive Bayes:
  • Suitable for discrete counts (e.g., text classification with word counts).
  1. Bernoulli Naive Bayes:
  • Suitable for binary/boolean features.
  • Used when features are binary (e.g., presence or absence of a word in a document).

Applications of Naive Bayes

The third and fourth images highlight some applications of Naive Bayes:

  1. Job Fit Classification:
  • Identifies the right fit for a job by classifying the skills mentioned in resumes or job descriptions.
  1. Content Classification:
  • Classifies content available in the publishing field based on information sources such as keywords, metadata, etc.

Other common applications include:

  • Spam Filtering: Classifying emails as spam or not spam.
  • Sentiment Analysis: Determining if a piece of text expresses positive or negative sentiment.
  • Medical Diagnosis: Predicting the presence of a disease based on symptoms.

How Naive Bayes Works

a. Assumption of Independence

Naive Bayes assumes that the presence of a particular feature in a class is unrelated to the presence of any other feature. This is called the “naive” assumption, which is why it’s called Naive Bayes.

b. Steps in Naive Bayes Classification

  1. Calculate Prior Probabilities:
  • Calculate the prior probability of each class.

2. Calculate Likelihood:

  • Calculate the likelihood of each feature given each class.

3. Calculate Posterior Probability:

  • Use Bayes’ Theorem to calculate the posterior probability of each class given the input features.

4. Predict the Class:

  • Predict the class with the highest posterior probability.

Example: Email Spam Classification

Let’s go through a simple example to illustrate how Naive Bayes works.

a. Dataset:

b. Calculate Prior Probabilities

c. Calculate Likelihood

d. Calculate Posterior Probability

Suppose we have a new email with “Free=Yes” and “Win=Yes”. We want to classify it as Spam or Not Spam.

  • Posterior Probability of Spam:

P(Spam∣Free=Yes,Win=Yes)∝P(Free=Yes∣Spam)⋅P(Win=Yes∣Spam)⋅P(Spam)=0.5⋅1⋅0.5=0.25

  • Posterior Probability of Not Spam:

P(Not Spam∣Free=Yes,Win=Yes)∝P(Free=Yes∣Not Spam)⋅P(Win=Yes∣Not Spam)⋅P(Not Spam)=0.5⋅0⋅0.5=0

since P(Not Spam∣Free=Yes,Win=Yes)=0, the email is classified as Spam.

2-Stochastic Gradient Descent (SGD)

Imagine you are on a mountain and you want to get to the lowest point in the valley. You look around and take a step in the direction that goes downhill the fastest. Stochastic Gradient Descent is an optimization algorithm that does something similar but for mathematical functions.

Objective

SGD aims to minimize a function, often called the “loss function” or “cost function,” which measures how far the predictions of a model are from the actual data.

The Problem SGD Solves

In machine learning, we often have a model that makes predictions based on input data. The model has parameters (like weights in a neural network or coefficients in linear regression) that we need to adjust to make the predictions as accurate as possible.

The problem is: How do we find the best parameters that minimize the error between the model’s predictions and the actual data?

Advantages of SGD

  1. Speed: SGD is much faster than traditional gradient descent because it uses only one data point per iteration instead of the entire dataset.
  2. Efficiency for Large Datasets: It is particularly efficient for large datasets since it doesn’t need to load all the data into memory at once.
  3. Escape Local Minima: The stochastic nature of SGD can help escape local minima because the randomness in the updates can jiggle the parameters out of shallow minima.

Disadvantages of SGD

  1. Noisy Updates: Because SGD uses only one data point per iteration, the updates can be noisy, leading to fluctuations in the cost function.
  2. Learning Rate Sensitivity: The performance of SGD is highly sensitive to the learning rate. If the learning rate is too small, the algorithm will take too long to converge. If it is too large, the algorithm may diverge.
  3. Convergence: SGD may not converge to the exact minimum but will oscillate around it.

3-K-NN:Nearest Neighbors

K-NN is a simple, intuitive algorithm that classifies or predicts the value of a new data point based on the values of its nearest neighbors in the feature space.

Classification with K-NN

How it Works:

  1. Load the Data: You have a training dataset with labeled classes.
  2. Choose the Value of K: Decide how many nearest neighbors to consider.
  3. Calculate Distances: For a new data point, calculate the distance to every point in the training dataset.
  4. Find Nearest Neighbors: Identify the K nearest neighbors based on the calculated distances.
  5. Majority Vote: Assign the new data point to the class that is most common among its K nearest neighbors.

Example:

If you have a dataset of fruits with features like weight and color, and you want to classify a new fruit, K-NN will look at the K closest fruits in your dataset and assign the new fruit to the class that appears most frequently among these neighbors.

4-Decision tree:

Decision Trees are a versatile and widely-used supervised learning method for both classification and regression tasks. They simulate human decision-making processes by breaking down complex datasets into simpler subsets, creating a tree-like structure of decisions and their possible consequences.

Components of a Decision Tree

  1. Root Node: The topmost decision node that represents the best attribute to split the data initially.
  • Example: “Is age > 30?”

2. Decision Nodes: Internal nodes where the dataset is split based on specific conditions or questions.

  • Example: “Is salary > $50,000?”

3. Branches: The outcomes of each decision node, leading to further nodes or leaves.

  • Example: Yes/No branches from a decision node.

4. Leaf Nodes: Terminal nodes that represent the final decision or prediction.

  • Example: “Will buy” or “Will not buy.”

Algorithms Used in Decision Trees

Several algorithms can be used to construct Decision Trees, each with its own method for selecting the best splits:

  1. ID3 (Iterative Dichotomiser 3):
  • Uses information gain to decide the best attribute for splitting the data.
  • Only works with categorical data and does not handle missing values.

2. C4.5:

  • An extension of ID3 that uses gain ratio to improve upon information gain.
  • Can handle both categorical and numerical data and missing values.

3. CART (Classification and Regression Trees):

  • Uses the Gini impurity for classification tasks and mean squared error for regression tasks.
  • Supports both numerical and categorical data and can handle missing values.

How Decision Trees Work

  1. Start at the Root Node: The tree begins with a single node that contains the entire dataset.
  2. Split the Data: The dataset is split based on attribute values to create the purest subsets (homogeneous groups). The choice of attribute is determined by the algorithm used (e.g., information gain, Gini impurity).
  3. Repeat: The process is repeated on each derived subset in a recursive manner until one of the stopping criteria is met (e.g., maximum depth reached, minimum number of samples in a node).
  4. Predict: For a new data point, start at the root node and follow the branches based on the data point’s attributes until a leaf node is reached, which gives the prediction.

Example Use Cases

  1. Classification:
  • Predicting whether a customer will buy a product (Yes/No).
  • Diagnosing a disease based on symptoms (Positive/Negative).

2. Regression:

  • Predicting house prices based on features like size and location.
  • Estimating a person’s salary based on their experience and education level.

Advantages of Decision Trees

  1. Easy to Understand and Interpret: Decision Trees are intuitive and can be visualized, making them easy to explain to non-technical stakeholders.
  2. Minimal Data Preparation: They require little data preprocessing compared to other algorithms.
  3. Versatile: Can handle both numerical and categorical data, making them suitable for a wide range of problems.
  4. Non-parametric: They do not assume any specific distribution of the data, making them flexible for various types of datasets.

Disadvantages of Decision Trees

  1. Overfitting: Decision Trees can create overly complex trees that do not generalize well to new data.
  • Solution: Use techniques like pruning, setting a maximum depth, or using ensemble methods like Random Forests.

2. Instability: Small variations in data can lead to a completely different tree structure.

  • Solution: Use ensemble methods to average multiple trees.

3. Bias in Unbalanced Data: If some classes dominate, the Decision Tree can be biased towards the majority classes.

  • Solution: Use techniques like balancing the dataset or adjusting class weights.

Conclusion

Decision Trees are a powerful and intuitive method for both classification and regression tasks. They are easy to understand and interpret, making them a popular choice for many machine learning applications. By leveraging algorithms like ID3, C4.5, and CART, Decision Trees can effectively handle a wide range of problems. However, care must be taken to avoid overfitting and ensure the tree is well-balanced for accurate predictions. Understanding the components and working of Decision Trees allows for their effective use in solving complex problems.

That’s all for this chapter!

In the next chapter, we’ll dive into unsupervised learning, where we’ll uncover techniques to find hidden patterns and structures in unlabeled data.

Practical Exploration: If you’re interested in seeing these algorithms in action, feel free to check out my GitHub repository, where I’ve implemented and practiced these concepts using Python. You can find the code, experiments, and additional resources there.

amyaby/Machine-learning: keep walking no matter how dark the path may seem.


메타데이터
post_id
63f2ebea15d6
slug
3-understanding-classification-in-machine-learning-a-comprehensive-guide-63f2ebea15d6
url
https://medium.com/@imane_louve/3-understanding-classification-in-machine-learning-a-comprehensive-guide-63f2ebea15d6
canonical_url
https://medium.com/@imane_louve/3-understanding-classification-in-machine-learning-a-comprehensive-guide-63f2ebea15d6
author_url
https://medium.com/@imane_louve
status
ok
fetched_at
2026-06-13 16:00:06