Hello! Welcome to our next lesson.
In our last session, we explored the geometric intuition behind Support Vector Machines (SVMs), focusing on how they maximize the margin between classes. We also saw how evaluation metrics like ROC/AUC are crucial for understanding a model's true performance.
Today, we'll tackle a very common and practical problem in machine learning classification: class imbalance. This occurs when one class is heavily outnumbered by another, which can seriously mislead our models and our metrics. For example, a model trained to detect a rare disease might learn to simply predict "no disease" for everyone, achieving high accuracy but being completely useless in practice.
Your learning outcome for this lesson is to diagnose class imbalance and apply resampling or class weight techniques to address it. This is a critical skill for building robust and fair machine learning models, aligning perfectly with your goal to justify your modeling choices in a professional setting.
Part 1: Diagnosing Class Imbalance
Class imbalance refers to a situation in a classification dataset where the number of observations per class is not equally distributed. In many real-world problems like fraud detection, medical diagnosis, or anomaly detection, the class of interest (e.g., "fraud") is very rare compared to the "normal" class.
Why is this a problem?
Most standard machine learning algorithms are designed to maximize overall accuracy. When one class dominates, the model can achieve high accuracy by simply predicting the majority class all the time. This creates a model with high bias towards the majority class, which will perform poorly when it matters most—identifying the rare minority class instances.
How do we spot it?
Diagnosing class imbalance is straightforward. You can:
- Use
value_counts(): In pandas, applying.value_counts()to your target variable column will give you a direct count of samples in each class. - Visualize the distribution: A bar chart or a pie chart can provide an immediate visual sense of the imbalance.

To see a practical demonstration of how to identify imbalance in a real dataset using Python, the following short video is very helpful.
How to handle imbalanced datasets in Python
The video 'How to handle imbalanced datasets in Python' by Data Professor starts by clearly defining class imbalance and then shows how to use pandas and Matplotlib to diagnose it in a dataset.
Watch the first four minutes of the video (00:00 - 03:57). Focus on how the presenter uses .value_counts() and a pie chart to quantify and visualize the imbalance.
Part 2: Resampling Strategies: Modifying the Data
Once we've diagnosed imbalance, we need to correct for it. One family of techniques, called resampling, involves modifying the dataset before training the model to create a more balanced class distribution.
There are two main approaches to resampling:

Let's dive into these methods. The imbalanced-learn (or imblearn) library in Python is the standard tool for implementing them.
A crucial rule to remember: Resampling should only ever be applied to the training data. If you resample the entire dataset before splitting it into training and testing sets, you will introduce data leakage. Your model will be tested on data that is a direct copy or a synthetic derivative of data it was trained on, leading to overly optimistic and unreliable performance metrics.
Undersampling: Reducing the Majority
Undersampling methods balance the dataset by removing samples from the majority class.
-
Random Undersampling: This is the simplest method. It randomly discards samples from the majority class until the classes are balanced.
- Pro: Can significantly speed up training time, which is useful for very large datasets.
- Con: Can discard potentially useful information, as the discarded samples might be important for defining the decision boundary.
-
Tomek Links: A more advanced method. A Tomek Link is a pair of samples from different classes that are each other's nearest neighbor. In undersampling, we identify these links and remove the majority-class sample from the pair. The intuition is to "clean" the space between the classes, making the decision boundary clearer.
Class Imbalance Strategies — A Visual Guide with Code
The article 'Class Imbalance Strategies — A Visual Guide with Code' provides excellent visualizations and code examples for these techniques.
Read 'Strategy 2. Random Undersampling' to see how it works visually. Then, read 'Strategy 5. Under-sampling with Tomek Links' to understand its more targeted approach.
Oversampling: Increasing the Minority
Oversampling methods balance the dataset by adding samples to the minority class.
-
Random Oversampling: Duplicates random samples from the minority class.
- Pro: No information is lost from the original dataset.
- Con: Since it just copies existing data, it can lead to overfitting. The model learns the specific minority samples too well, instead of a general pattern.
-
SMOTE (Synthetic Minority Over-sampling Technique): This is a much smarter approach. Instead of duplicating samples, SMOTE generates new, synthetic samples. It works like this:
- It takes a minority class sample.
- It finds its 'k' nearest neighbors from the same minority class.
- It creates a new synthetic sample at a random point along the line segment connecting the original sample and one of its neighbors.
This creates new, plausible minority samples, expanding the decision region for the minority class without just copying data.
SMOTE (Synthetic Minority Oversampling Technique) for Handling Imbalanced Datasets
To really understand the intuition behind SMOTE, let's watch this short, visual explanation.
Watch from the beginning to 06:17 to see how SMOTE works conceptually and visually. Then, pay close attention to the critical advice from 08:43 to 09:30, which explains why you must only apply SMOTE to your training data.
The article you read earlier, 'Class Imbalance Strategies', also covers SMOTE and a related technique called ADASYN in sections 5 and 6, and a combined approach (SMOTE-Tomek) in section 8, which are great for further reference.
Test your understanding!
You are building a fraud detection model for an e-commerce platform. The dataset contains 10 million transactions, of which only 5,000 are fraudulent. Your team has limited computational resources, and training models is slow.
Would you be more inclined to start with an undersampling or an oversampling strategy? Why?
Show answer
You would be more inclined to start with undersampling.
Reasoning:
- Computational Cost: Oversampling (like with SMOTE) would increase the size of your already large 10-million-record dataset, making training even slower.
- Efficiency: Undersampling would create a much smaller, balanced dataset (e.g., 5,000 fraud cases and 5,000 non-fraud cases). This would dramatically reduce training time, allowing you to iterate on models much faster. While there is a risk of losing information, the practical benefit of faster training is a major advantage in this scenario.
Part 3: Class Weighting: Modifying the Algorithm
Resampling modifies the data. An alternative approach is to modify the algorithm's learning process. This is known as class weighting or cost-sensitive learning.
Remember in our last lesson on SVMs, we discussed the C hyperparameter? It sets the penalty for misclassifying a data point. A higher C means a higher penalty.
Class weighting extends this idea. Instead of one C for all samples, we can assign different penalties for misclassifying samples from different classes. To handle imbalance, we would place a much higher penalty on misclassifying a minority class sample. This forces the model's optimization process to "pay more attention" to getting the minority class right.
Many scikit-learn classifiers, including LogisticRegression and SVC, have a class_weight parameter. By simply setting class_weight='balanced', the library automatically calculates and applies weights that are inversely proportional to the class frequencies.
class_weight='balanced' in practice:
- Majority Class: Gets a small weight. The penalty for misclassifying it is low.
- Minority Class: Gets a large weight. The penalty for misclassifying it is high.
This is an elegant and computationally efficient way to handle imbalance without altering the dataset itself. It directly influences the model's loss function during training.
Conclusion
You now have a toolkit of powerful strategies to combat class imbalance, a problem you will frequently encounter in applied machine learning.
Key Takeaways:
- Class Imbalance: Occurs when classes are not represented equally, causing models to become biased towards the majority class. It's diagnosed with value counts and visualizations.
- Resampling: A data-level solution.
- Undersampling removes majority class samples. It's fast but can cause information loss.
- Oversampling adds minority class samples. SMOTE is a powerful method that creates synthetic data, but it increases training time and can risk overfitting.
- Crucial Rule: Always perform resampling only on the training data to prevent data leakage.
- Class Weighting: An algorithm-level solution. It modifies the loss function to penalize misclassifications of the minority class more heavily. It's often as simple as setting
class_weight='balanced'inscikit-learn.
Preview of the Next Lesson:
Now that we've built and evaluated classifiers, and even learned how to handle imbalanced data, there's a final practical step. A classification model often outputs a probability, and we use a threshold (typically 0.5) to make a final decision. But is 0.5 always the best choice? In the next lesson, we will learn how to select an appropriate classification threshold based on business objectives, connecting our model's output directly to real-world impact.