Log Transformation to Mitigate the Effect of Outliers | Data Cleaning | Exploratory Data Analysis

Rina Mondal
Women in Technology
3 min readJul 3, 2024
Photo by Suzanne D. Williams on Unsplash

Handling outliers is a crucial aspect of data analysis. Outliers can skew results and lead to misleading conclusions. Instead of removing outliers, log transformations offer a robust method to mitigate their effects without losing valuable data.

In this blog post, we’ll define outliers, explain why log transformations are preferred over removal, and demonstrate how to apply log transformations using a dataset with outliers.

What Are Outliers?

Outliers are data points that deviate significantly from the majority of a dataset. They can arise due to variability in the data, measurement errors, or experimental errors. Outliers can impact statistical analyses, such as mean and standard deviation, and influence model performance.

Examples of Outliers

In a dataset of human heights, an individual with a height of 250 cm (approximately 8 feet) would be considered an outlier.
In a dataset of house prices, a mansion worth $50 million in a neighborhood where most houses are priced between $200,000 and $500,000 would be an outlier.

Why Not Simply Remove Outliers? (To Remove Outliers using IQR)

Removing outliers might seem like a straightforward solution, but it has several drawbacks:

1. Loss of Information: Outliers can contain important information about the variability and potential anomalies in the data.
2. Bias: Removing outliers can introduce bias, especially if they represent a legitimate aspect of the data.
3. Repeatability: Outlier removal is often subjective, which can lead to inconsistent results when the analysis is repeated.

The Role of Log Transformations

Log transformations are a powerful technique to reduce the impact of outliers. By applying a logarithmic function to the data, we can compress the range of values, making outliers less extreme. This transformation can make the data more suitable for analysis and improve the performance of statistical models.

How Log Transformations Work

A log transformation applies the logarithm to each data point:

y = log(x)

Example: Applying Log Transformations

Let’s use a sample dataset to demonstrate the effect of log transformations on outliers. Consider the following dataset of house prices (in thousands of dollars):

Step 1: Create the Dataset

We’ll create a dataset with house prices that include more pronounced outliers.

import numpy as np
import pandas as pd

# Creating a synthetic dataset with more realistic values and pronounced outliers
np.random.seed(42) # For reproducibility
data = {
'House_ID': range(1, 101),
'Price': np.concatenate([np.random.normal(300, 50, 95), np.random.normal(5000, 1000, 5)])
}
df = pd.DataFrame(data)
print("Original Dataset:")
print(df.head())

Step 2: Visualize the Original Data

Visualize the original data using KDE plots to observe the distribution and the impact of the outliers.

import matplotlib.pyplot as plt
import seaborn as sns

# Visualize original data using KDE plot
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
sns.kdeplot(df['Price'], shade=True, color='blue')
plt.title('KDE of Original House Prices')
plt.xlabel('Price (in thousands)')
plt.ylabel('Density')

Step 3: Apply Log Transformation

Apply the log transformation to the price column.

# Apply log transformation to the 'Price' column
df['Log_Price'] = np.log(df['Price'])
print("Dataset with Log Transformed Prices:")
print(df.head())

Step 4: Visualize the Transformed Data

Visualize the transformed data using KDE plots to see the effect of the log transformation.

# Visualize log-transformed data using KDE plot
plt.subplot(1, 2, 2)
sns.kdeplot(df['Log_Price'], shade=True, color='red')
plt.title('KDE of Log-Transformed House Prices')
plt.xlabel('Log(Price)')
plt.ylabel('Density')
plt.tight_layout()
plt.show()

Results and Interpretation

By using a dataset with a clear distinction between regular data points and outliers, the log transformation effectively normalizes the distribution, making the dataset more suitable for analysis without the skewness caused by outliers. The KDE plots clearly show how the log transformation reduces the impact of outliers and normalizes the data.

Log transformations are an effective method to handle outliers without discarding valuable data. By compressing the range of values, log transformations mitigate the impact of outliers and improve the robustness of statistical analyses and models. Next time you encounter outliers in your dataset, consider applying log transformations as a powerful alternative to outlier removal.

Complete Data Science Roadmap.

Blogs Related to Data Cleaning:

  1. Complete Data Cleaning.
  2. Remove Outliers using InterQuartile Range
  3. Remove Outliers using Z-Score

Give it :👏👏👏👏:
If you found this guide helpful , why not show some love? Give it a Clap 👏, and if you have questions or topics you’d like to explore further, drop a comment 💬 below 👇. If you appreciate my hard work please follow me. That is the only way I can continue my passion.

--

--

Rina Mondal
Women in Technology

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.