# How to Remove Outliers in Python

### Introduction

Outliers are data points that significantly differ from the rest of the data in a dataset. These extreme values can skew the results of data analysis, leading to incorrect conclusions. In this article, we will discuss various techniques for identifying and removing outliers in Python using different libraries such as NumPy, Pandas, and SciPy. We will also explore graphical methods for visualizing outliers, such as box plots and scatter plots.

1. Understanding Outliers
2. Standard Deviation Method
3. Interquartile Range (IQR) Method
4. Z-Score Method
5. Tukey’s Fences
6. Robust Z-Score
7. Isolation Forest
8. DBSCAN Clustering
9. Visualizing Outliers with Box Plots and Scatter Plots
10. Choosing the Right Method for Removing Outliers
11. Conclusion

### 1. Understanding Outliers

Before diving into the techniques for removing outliers, it is crucial to understand the types of outliers and their impact on the dataset. Outliers can be classified into the following categories:

• Point outliers: Individual data points that deviate significantly from the rest of the data.
• Contextual outliers: Data points that appear to be anomalous within a specific context or subset of the data.
• Collective outliers: A group of data points that exhibit unusual behavior collectively, even if each data point might not be an outlier individually.

Outliers can be caused by various factors, such as data entry errors, measurement errors, or natural variations in the data. Identifying and addressing these issues is essential to ensure the accuracy and reliability of the data analysis.

### 2. Standard Deviation Method

One of the simplest ways to identify and remove outliers is by using the standard deviation method. In this approach, we calculate the mean and standard deviation of the dataset and remove data points that fall outside a specified range (usually 2 or 3 standard deviations from the mean). Here’s how to implement the standard deviation method using Python and NumPy:

import numpy as np

data = np.array([1, 2, 3, 4, 100])
mean = np.mean(data)
std_dev = np.std(data)

threshold = 2 * std_dev
lower_bound, upper_bound = mean - threshold, mean + threshold

filtered_data = [x for x in data if x > lower_bound and x < upper_bound]

### 3. Interquartile Range (IQR) Method

The interquartile range (IQR) is the range between the first quartile (25th percentile) and the third quartile (75th percentile) of a dataset. The IQR method involves calculating the IQR and then removing data points that fall outside a specified range (usually 1.5 times the IQR). Here’s how to implement the IQR method using Python and Pandas:

import pandas as pd

data = pd.Series([1, 2, 3, 4, 100])
Q1, Q3 = data.quantile(0.25), data.quantile(0.75)
IQR = Q3 - Q1

threshold = 1.5 * IQR
lower_bound, upper_bound = Q1 - threshold, Q3 + threshold

filtered_data = data[(data > lower_bound) & (data < upper_bound)]

### 4. Z-Score Method

The Z-score is a measure of how far a data point is from the mean, expressed in units of standard deviation. Data points with Z-scores greater than a specified threshold (usually 2 or 3) are considered outliers. Here’s how to implement the Z-score method using Python and SciPy.

from scipy import stats
import numpy as np

data = np.array([1, 2, 3, 4, 100])
z_scores = np.abs(stats.zscore(data))

threshold = 2
filtered_data = data[z_scores < threshold]

### 5. Tukey’s Fences

Tukey’s Fences is a method that extends the concept of IQR to identify outliers. It defines ‘fences’ as thresholds beyond which data points are considered outliers. These fences are typically set at 1.5IQR (the ‘inner’ fence) and 3IQR (the ‘outer’ fence) above the third quartile or below the first quartile. Here’s how to implement Tukey’s Fences with Python and Pandas.

import pandas as pd

data = pd.Series([1, 2, 3, 4, 100])
Q1, Q3 = data.quantile(0.25), data.quantile(0.75)
IQR = Q3 - Q1

inner_fence = 1.5 * IQR
outer_fence = 3 * IQR
lower_bound, upper_bound = Q1 - outer_fence, Q3 + outer_fence

filtered_data = data[(data > lower_bound) & (data < upper_bound)]

### 6. Robust Z-Score

While the Z-score method is powerful, it can be sensitive to outliers, as it uses the mean and standard deviation. The Robust Z-score, or Modified Z-score method, is a modification of the Z-score method that uses the median and Median Absolute Deviation (MAD), which are less sensitive to outliers. Here’s how to implement the Robust Z-score method in Python.

import numpy as np

data = np.array([1, 2, 3, 4, 100])
median = np.median(data)
mad = np.median(np.abs(data - median))

# Constant 0.6745 is used to achieve consistency with the standard deviation method
modified_z_scores = 0.6745 * (data - median) / mad

threshold = 3.5
filtered_data = data[modified_z_scores < threshold]

### 7. Isolation Forest

Isolation Forest is a machine learning algorithm that’s used for anomaly detection. It isolates observations by randomly selecting a feature and then randomly selecting a split value between the maximum and minimum values of the selected feature. Here’s how to use it with Python’s sklearn.ensemble library.

from sklearn.ensemble import IsolationForest
import numpy as np

data = np.array([1, 2, 3, 4, 100]).reshape(-1, 1)
clf = IsolationForest(contamination=0.01)
preds = clf.fit_predict(data)
filtered_data = data[preds > 0]

### 8. DBSCAN Clustering

Density-Based Spatial Clustering of Applications with Noise (DBSCAN) is a clustering method that can be used to detect outliers. It groups together points that are packed closely together, and points in low-density regions are classified as outliers. Here’s how to use it with sklearn.cluster.

from sklearn.cluster import DBSCAN
import numpy as np

data = np.array([1, 2, 3, 4, 100]).reshape(-1, 1)
clustering = DBSCAN(eps=3, min_samples=2).fit(data)

labels = clustering.labels_
filtered_data = data[labels >= 0]

### 9. Visualizing Outliers with Box Plots and Scatter Plots

Visualizing your data is a powerful tool for identifying outliers. Box plots and scatter plots are two types of visualizations that can help detect outliers.

Box plots graphically depict groups of numerical data through their quartiles. Outliers may be plotted as individual points that are distant from the boxes. Here’s how to create a box plot using matplotlib.

import matplotlib.pyplot as plt

data = [1, 2, 3, 4, 100]
plt.boxplot(data)
plt.show()

In this plot, the box represents the IQR, the line in the middle of the box is the median, and the dots are the outliers.

Scatter plots are another visualization tool where the values of two variables are plotted along two axes. This plot is helpful when you are dealing with multi-dimensional data and want to check outliers in a pair of features. Here’s how to create a scatter plot.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 100]
plt.scatter(x, y)
plt.show()

In this plot, most points are close to each other, but the point (5,100) is far away from others, indicating it is an outlier.

### 10. Choosing the Right Method for Removing Outliers

The choice of method for detecting and removing outliers largely depends on the specific characteristics of your dataset.

• If your data is normally distributed, the standard deviation method, Z-score method, or even the Robust Z-score method can work well.
• If you suspect your data contains extreme values or it doesn’t follow a normal distribution, the IQR method or Tukey’s Fences could be a better choice.
• If you are working with multidimensional data, you may consider using machine learning-based methods like Isolation Forest or DBSCAN Clustering.

In all cases, visualizing your data with box plots or scatter plots can provide valuable insights into the presence and nature of outliers.

### 11. Conclusion

Outliers can significantly impact the results of your data analysis, and it’s crucial to handle them appropriately. Python, with its powerful libraries like NumPy, Pandas, SciPy, and sklearn, provides a wide range of methods for detecting and removing outliers from your data. Remember, it’s essential to understand the source and nature of outliers in your data before deciding on the method to handle them. Always explore your data thoroughly, and choose the method that best fits the specific characteristics of your dataset.