Now Reading
A Complete Guide To Outlier Detection With Hands-On Implementation For Beginners

A Complete Guide To Outlier Detection With Hands-On Implementation For Beginners

Complete Guide To Outlier Detection

To start off, one must need to know what an outlier is. An outlier is basically the value of a point or a data point who largely differs from the rest of the crowd. Let us try to understand this with an example: in a class of 20 students with heights ranging from 170cm to 215 cm. The average height is 175cm and the maximum is 195cm. So here, the one with 215cm largely differs from the group and thus the data point is called an outlier. Another term that is used prominently for an outlier is, “anomaly”. Also, a person with a height of 125 cm would have been considered as an outlier too.

Through this article, we will be discussing outliers, their impact and methods to treat the outlier present in the data. We will also demonstrate the hands-on implementation of these methods.

Impact of outliers

Now, let us understand why it is important to identify outliers when it comes to machine learning.

As we know that the data we use for training machine learning models is highly sensitive to missing or faux values, as it severely limits the accuracy of the model. For the same reason, we need to have a great understanding of the data we have. In that having a good understanding of outliers is equally important because outliers can severely affect the accuracy of the model that we desire to train.

Source: Medium

Here we see, 300 is clearly an outlier. I have also tried to show how an outlier really impacts the statistical dimensions of the data. An outlier largely impacts mean and thus standard deviation and obviously would do the same to variance. 

Here just to give briefings: mean can be understood as the average of all the values, the median indicates the middlemost value in the data, the mode is the most repetitive value in the data. Standard deviation is the amount of variation of the values in the data that we are having.

It is easier to identify the outlier when the number of given values is not that humongous but what to do when the number of data points is in tens of thousands. In such a case we can opt to identify outliers in the following ways: 

How to identify Outliers and remove them?

We simply cannot always remove outliers just by seeing them. The method of removing the outliers differs from the type of data set you are working on.

But you can surely get hands of knowledge of a few really important methods. 

You can surely try it out yourself too. I will demonstrate it on the New York City AirBnB Data Set

A few methods over which we will be looking at are:

  1. Percentile 
  2. Using Scatter/Box Plots
  3. Using Standard Deviation
  4. Z test

NOTE: You can also find the link to Google Colab at the end of this article.

import pandas as pd  #pip install pandas
import numpy as np   #pip install numpy
df=pd.read_csv('/content/AB_NYC_2019.csv') #reading the csv file
df.sample(10) #randomly gives out the rows from the data set


df.shape  # in the format of (row,columns)

# here we will be studying the price column from the data set



  1. Removing Outliers using Percentile
#setting the limits or our criteria for an item to be called as an outlier
upper_bound=df['price'].quantile(0.9995)   #value at 99.99 percentile
print('Upper bound:',upper_bound)
lower_bound=df['price'].quantile(0.0005)   #value at 0.05 percentile
print('Lower bound:',lower_bound)


df_percentile=df[(df['price']<upper_bound) & (df['price']>lower_bound)]

print(df_percentile) #removing outliers with the help of percentile


After removing the outliers we have 48841 entries which earlier was 48895.

  1. Identifying and removing outlier with scatter plot
import matplotlib.pyplot as plt
plt.title('Price of accomodation')


x_inlier=list(df[(df['price']<upper_bound) & (df['price']>lower_bound)].index)
y_inlier=df[(df['price']<upper_bound) & (df['price']>lower_bound)]


plt.scatter(x_upper,y_upper['price'],color='black',marker='d',label='Above Upper Quartile')
plt.scatter(x_lower,y_lower['price'],color='red',label='Below Lower Quartile')
plt.title('Price of accomodation')


If you look carefully at the bottom of the graph there you will see some red data points, these are exactly the ones who are under the lower quartile. We are not able to see them distinguishably because the number of green data points is the way for you to see the red ones. And moreover, the difference between the lower bound and the normal range is not that much for it to be clearly visible.


  1. here we wanted to use the same parameters so as to maintain uniformity
  2. also remember not to use scatter plot when there are a lot of data points and especially when these data points lack variance.

We will be seeing and analyzing the rest two on a different dataset because not every data set can be used to show and implement all the methods.

Link to dataset:

3. Standard Deviation


Outlier Detection for Beginners

Here, n is the number of samples. X is the value and x̄ is the mean of all values.

from scipy.stats import norm
Outlier Detection for Beginners
rng = np.arange(df.Height.min(), df.Height.max(), 0.1)
plt.plot(rng, norm.pdf(rng,df.Height.mean(),df.Height.std()))
Outlier Detection for Beginners


Here standard deviation is helpful when there’s a normal distribution (a bell-shaped curve) that easily can be observed.

Outlier Detection for Beginners

(10000×3 is the shape of the data frame)

#setting limits for outliers 
#constraining by using 3 std dev technique
#one may take 2 std dev or even 4-5 std dev for the same , it totally depends on the type of the data being used





See Also
Most Learning Is Slow In The Field Of Machine Learning: Sara Hooker, Researcher at Google Brain



Outlier Detection for Beginners

Removing the outliers

Observe the shape of the data frame, now. (9993X3)

4. Z-Test


df['zscore'] = ( df.Height - df.Height.mean() ) / df.Height.std()



Outlier Detection for Beginners

The agenda:

Get data points that have z score higher than 3 or lower than -3.

Another way of saying the same thing is to get data points that are more than 3  standard deviations away.

#the outliers

df[(df.zscore<-3) | (df.zscore>3)]


Outlier Detection for Beginners

Outliers Removed

Outlier Detection for Beginners

This is exactly how one can identify outliers and according to the data and how the removal will impact, can remove the outliers.


This article was plotted on the very idea to help beginners understand the concept of outliers, how to identify outliers and how to remove outliers. There are many more methods like IQR etc but these are sufficient for a beginner. Hope this adds a considerable amount of value to your work. 

The complete code for the above implementation is available at the AIM’s GitHub repository. Please visit this link for this complete code.

Join Our Telegram Group. Be part of an engaging online community. Join Here.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top