An observation which differs from an overall pattern on a sample dataset is called an outlier.
The outliers may suggest experimental errors, variability in a measurement, or an anomaly. The age of a person may wrongly be recorded as 200 rather than 20 Years. Such an outlier should definitely be discarded from the dataset.
However, not all outliers are bad. Some outliers signify that data is significantly different from others. For example, it may indicate an anomaly like bank fraud or a rare disease.
Significance of outliers:
- Outliers badly affect mean and standard deviation of the dataset. These may statistically give erroneous results.
- Most machine learning algorithms do not work well in the presence of outlier. So it is desirable to detect and remove outliers.
- Outliers are highly useful in anomaly detection like fraud detection where the fraud transactions are very different from normal transactions.
What is Interquartile Range IQR?
IQR is used to measure variability by dividing a data set into quartiles. The data is sorted in ascending order and split into 4 equal parts. Q1, Q2, Q3 called first, second and third quartiles are the values which separate the 4 equal parts.
- Q1 represents the 25th percentile of the data.
- Q2 represents the 50th percentile of the data.
- Q3 represents the 75th percentile of the data.
If a dataset has 2n / 2n+1 data points, then
Q1 = median of the dataset.
Q2 = median of n smallest data points.
Q3 = median of n highest data points.
IQR is the range between the first and the third quartiles namely Q1 and Q3: IQR = Q3 – Q1. The data points which fall below Q1 – 1.5 IQR or above Q3 + 1.5 IQR are outliers.
Assume the data 6, 2, 1, 5, 4, 3, 50. If these values represent the number of chapatis eaten in lunch, then 50 is clearly an outlier.
Step by step way to detect outlier in this dataset using Python:
Step 1: Import necessary libraries.
Step 2: Take the data and sort it in ascending order.
array([ 1, 2, 3, 4, 5, 6, 50])
Step 3: Calculate Q1, Q2, Q3 and IQR.
Q1 25 percentile of the given data is, 2.5 Q1 50 percentile of the given data is, 4.0 Q1 75 percentile of the given data is, 5.5 Interquartile range is 3.0
Step 4: Find the lower and upper limits as Q1 – 1.5 IQR and Q3 + 1.5 IQR, respectively.
low_limit is -2.0 up_limit is 10.0
Step 5: Data points greater than the upper limit or less than the lower limit are outliers
outlier in the dataset is 
Step 6: Plot the box plot to highlight outliers.
Step 7: Following code can also be used to calculate IQR
Conclusion: IQR and box plot are effective techniques to detect outliers in data.
- What is Box plot and the condition of outliers?
- Interquartile Range and Quartile Deviation using NumPy and SciPy
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Python | Detect corner of an image using OpenCV
- Python Program for Detect Cycle in a Directed Graph
- Python | Detect Polygons in an Image using OpenCV
- Detect an Unknown Language using Python
- Detect an object with OpenCV-Python
- Detect and Recognize Car License Plate from a video in real time
- Python - Detect loop in Dictionaries
- Detect Cat Faces in Real-Time using Python-OpenCV
- Detect the RGB color from a webcam using Python - OpenCV
- Processing of Raw Data to Tidy Data in R
- Data Integration in Data Mining
- Python - Convert Tick-by-Tick data into OHLC (Open-High-Low-Close) Data
- Difference between Data Cleaning and Data Processing
- range() vs xrange() in Python
- Python Program for Range sum queries without updates
- Python - Test if List contains elements in Range
- Python | range() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.