Sitemap
Tilak Mudgal

Tilak Mudgal

AI Engineer

Outliers

9 min readNov 4, 2024

--

Outliers are data points that are noticeably different from the rest of the data. They may be extremely high or low values that don’t follow the usual pattern. Outliers can show up due to errors, unique events, or simply natural variations in data.

Handling outliers is essential because they can skew your results and make analysis or models less accurate. For instance, one unusually high sales figure might make it look like sales are generally higher than they are. Identifying and addressing outliers ensures your data is as reliable and accurate as possible, leading to better insights and predictions.

Press enter or click to view image in full size

Types of Outliers

Outliers come in different forms, and identifying the type helps us decide how best to handle them. Here are the main types with real-world examples:

1. Univariate Outliers

  • What they are: Outliers that are unusual when looking at a single variable on its own. These values stand out without considering any other data points.
  • Example: Imagine a dataset of people’s ages, where most ages are between 20 and 60. If a few entries show ages over 100 or below 5 in an adult dataset, those could be univariate outliers.
  • Impact: Univariate outliers can skew averages and make data seem different than it is. For example, an unusually high income can make the average income appear higher, giving a misleading view.

2. Multivariate Outliers

  • What they are: Outliers that are unusual only when looking at relationships between two or more variables. These might look normal in one variable but stand out when combined with another.
  • Example: In a customer dataset, a 16-year-old spending $10,000 might not be typical, though 16-year-olds and high spenders are both common individually. It’s the unusual combination that makes it a multivariate outlier.
  • Impact: Multivariate outliers can reveal unusual patterns, like potential fraud or unique customer behaviors. Ignoring them could mean missing critical insights.

3. Context-Based Outliers

  • What they are: Outliers that are unusual only in certain situations or contexts. They may appear extreme, but they make sense given the context.
  • Example: High sales figures around Black Friday might seem like outliers if you look at the whole year’s data. However, these spikes are expected during holiday shopping seasons.
  • Impact: Context-based outliers often represent important seasonal or event-based patterns. Instead of removing them, it’s usually better to understand and factor them into the analysis.

By identifying these types, we can better understand whether an outlier is truly problematic or if it adds valuable information to our data.

Techniques for Handling Outliers

Handling outliers effectively depends on the method you choose. Here’s an overview of techniques from basic to advanced, along with examples of when they’re most useful and their limitations.

Simple Methods

1. Z-Score

  • What it does: Z-score measures how many standard deviations a data point is from the mean. Typically, data points with a Z-score above 3 or below -3 are considered outliers.
  • Example: In a dataset of people’s heights, a Z-score can help flag heights that are much taller or shorter than the average.
  • Limitations: Z-score assumes data is normally distributed. For skewed data, it may incorrectly flag valid points as outliers.

2. IQR (Interquartile Range) Method

  • What it does: IQR measures the spread of the middle 50% of data. Outliers are defined as values below Q1–1.5IQR or above Q3 + 1.5IQR.
  • Example: In a dataset of product prices, you can use IQR to identify prices that are exceptionally high or low compared to the rest.
  • Limitations: IQR works well for data with no extreme skew. However, it may not be suitable for data with long tails, as it may ignore some significant outliers.
Press enter or click to view image in full size

These simple methods are effective for detecting outliers in straightforward datasets but may not work as well with complex data patterns or non-normal distributions.

Moderate Techniques

1. Winsorization

  • What it does: Caps extreme values by setting them to a specific percentile (e.g., the 5th and 95th percentiles), reducing their influence without removing them.
  • Example: In a dataset of income levels, Winsorizing values at the 95th percentile caps extreme incomes, making them less likely to skew results.
  • Limitations: Winsorization can distort data if applied too broadly, so it’s best for cases where outliers are known to be extreme but not particularly informative.

2. Log Transformation

  • What it does: Reduces the impact of large values by applying a logarithmic transformation, which compresses data ranges and helps handle skewed data.
  • Example: In sales data with some very high sales figures, log transformation can reduce the effect of these outliers.
  • Limitations: Log transformation only works for positive values and may not be suitable for data with zero or negative values.

3. Square Root and Cube Root Transformations

  • What they do: Similar to log transformation, square root and cube root transformations reduce the effect of extreme values but are less aggressive.
  • Example: In a dataset with outliers in distances (like city-to-city distances), square root or cube root can help smooth the data without compressing it as much as log transformation.
  • Limitations: These transformations can only be applied to positive values and may still not be sufficient for highly skewed data.

Advanced Techniques

1. Isolation Forests

  • What it does: Isolation Forest is a machine learning technique used to detect outliers by isolating data points. The algorithm creates multiple “trees,” which are decision paths that separate data points. Isolation Forests identify outliers by finding points that are isolated quickly (with fewer splits). Outliers are different from most data points, so they need fewer splits to be isolated.

How it Works:

  1. The algorithm randomly splits data points in each “tree.”
  2. Points that require fewer splits to be isolated are likely outliers, while normal points need more splits to get separated.

Think of it like a game of “20 Questions.” If a data point stands out a lot, you’ll find it faster by asking only a few questions. The same concept applies here: outliers are separated quickly in each tree.

Get Tilak Mudgal’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:
Imagine a dataset of network login activities. Most users have a typical pattern, but an unusual login attempt might differ from the usual pattern. Isolation Forest would quickly flag this unusual login as an outlier, helping detect potential security threats.

Limitations:

  • Computationally Intensive: Isolation Forests can be slow on large datasets, as they build many trees.
  • Not Ideal for Small Data: They work best with large datasets, as more data gives better accuracy for isolating unusual points.
Press enter or click to view image in full size

2. DBSCAN (Density-Based Spatial Clustering of Applications with Noise)

  • What it does: DBSCAN is a clustering algorithm that groups data points based on how close they are to each other. It forms clusters where points are packed closely together. Points that are isolated or far from others are labeled as “noise,” which means they’re treated as outliers.

How it Works:

  1. DBSCAN checks the neighborhood around each point to see how many points are nearby.
  2. If a point has many neighbors close by, it becomes part of a cluster.
  3. Points with few or no neighbors are labeled as “noise” and treated as outliers.

Example:
Imagine a GPS dataset showing the locations of cars in a city. Most cars are clustered on roads, but some may be far off (like in a remote area or field). DBSCAN would group the cars on the roads into clusters and mark the isolated points as outliers.

Limitations:

  • Works Best with Well-Separated Clusters: DBSCAN performs well when clusters are distinct and well-separated. However, it may not work as well if clusters are close together or vary a lot in density.
  • Sensitive to Parameters: DBSCAN requires careful tuning of parameters (like the minimum number of points for a cluster) to get the best results.

3. Model-Based Outlier Detection (Autoencoders, One-Class SVM)

Model-based methods like Autoencoders and One-Class SVM learn the general pattern of “normal” data and identify anything that deviates from this pattern as an outlier.

  • Autoencoders: These are neural networks designed to compress data into a smaller form and then reconstruct it back. If the model struggles to reconstruct a data point accurately, it’s likely an outlier, as it doesn’t fit the typical pattern.
  • One-Class SVM: This technique builds a boundary around the “normal” data points. If a new point falls outside this boundary, it’s flagged as an outlier.
    Example: Imagine a dataset of credit card transactions where most purchases follow a predictable pattern. An Autoencoder model, trained on typical spending, can flag unusual or suspicious transactions, such as a sudden high-value purchase from an unknown location, as potential fraud. Similarly, a One-Class SVM model would label any point outside the normal spending boundary as an anomaly.

Limitations:

  • Complexity: These methods require careful setup, tuning, and training, making them less ideal for small or simple datasets.
  • Data Size: They perform best with large datasets where patterns are well-defined, as they need enough data to learn what “normal” looks like.

4. Domain-Specific Handling

  • What it does: Advanced outlier handling also involves understanding the context. Sometimes outliers are valuable and should be kept, such as seasonal peaks in retail sales.
  • Example: In retail, spikes in sales around holidays are expected and shouldn’t be removed as outliers; instead, they should be analyzed separately.
  • Limitations: This approach requires deep domain knowledge to understand when an outlier is meaningful

Best Practices and Pitfalls

Handling outliers can improve your analysis, but it’s important to approach this task carefully. Here are some best practices and common pitfalls to avoid:

1. Avoid Removing Outliers Without Context

  • Pitfall: Simply removing outliers without understanding their context can lead to incorrect conclusions. Sometimes, outliers represent valuable information rather than errors.
  • Best Practice: Analyze outliers in relation to the context of the data. For example, high sales during a holiday season are expected and should not be removed as anomalies.

2. Consider the Type of Outliers

  • Pitfall: Using the same method for all outliers, regardless of type, can lead to biased results.
  • Best Practice: Identify the type of outliers (univariate, multivariate, or context-based) and choose handling techniques accordingly. Simple methods work for univariate outliers, while advanced techniques like Isolation Forests are better for complex, multivariate outliers.

3. Assess the Impact on Analysis Results

  • Pitfall: Assuming that any outlier handling method will improve the analysis without validating its impact can lead to poor results.
  • Best Practice: Test how outlier handling affects model performance or statistical summaries. For example, try running your model with and without outliers and compare results. This helps ensure that the chosen method improves, rather than distorts, the analysis.

4. Document Your Process

  • Pitfall: Not keeping track of outlier handling decisions can lead to confusion, especially in collaborative projects.
  • Best Practice: Document which methods were used, why, and the impact on the data. This makes it easier for others to understand and reproduce your analysis.

5. Use Domain Knowledge When Possible

  • Pitfall: Ignoring domain knowledge can lead to inappropriate handling of outliers, particularly in fields like finance or healthcare where context is essential.
  • Best Practice: Leverage domain insights to understand if outliers are genuinely problematic or if they carry meaningful information. In fraud detection, for instance, unusual spending patterns could indicate important trends rather than errors.

Conclusion

Outliers are an important aspect of data analysis and modeling, as they can either reveal valuable insights or distort results if handled improperly. Here are the key takeaways:

  • Understand the Types of Outliers: Recognize whether outliers are univariate, multivariate, or context-based to choose the right handling method.
  • Select Techniques Carefully: Start with simple methods for small datasets and use advanced techniques like Isolation Forests or Autoencoders for complex data.
  • Assess the Impact: Always evaluate how outlier handling affects your analysis or model to ensure that it improves accuracy and reliability.
  • Leverage Domain Knowledge: Outliers often make sense within a specific context, so consult domain experts or use industry insights to interpret them accurately.

By thinking critically about outliers and choosing appropriate handling techniques, you can improve data quality and make more accurate, insightful analyses. Each dataset is unique, so tailor your approach to the context and goals of your project.

--

--