Adventures in Machine Learning

Identifying Outliers in Python Made Easy with Grubbs’ Test

Grubbs’ Test in Python: Outlier Detection Made Easy

Outliers are data points that differ significantly from other observations in a dataset. They can be the result of data entry errors or can represent genuine anomalies, but in either case, they can have a significant impact on the analysis of the dataset.

Thus, identifying them is critical to ensuring the accuracy and quality of the data used. One method for identifying outliers is Grubbs’ Test, a statistical technique developed in the 1960s.

This test helps identify statistically significant outliers in datasets that are approximately normally distributed. In this article, we will explore the use of Grubbs’ Test in Python, including performing one and two-sided tests, identifying outlier index positions, extracting the value of outliers, and how to handle outliers.

Performing Grubbs’ Test using outlier_utils package

The outlier_utils package provides a simple implementation of the Grubbs’ Test in Python. The package includes the smirnov_grubbs() function, which is the syntax for Grubbs’ Test that returns an array with the outlier values removed according to a defined significance level.

Example 1: Two-Sided Grubbs Test

To perform the two-sided Grubbs’ Test, we need to define a significance level (alpha) and provide a numeric vector of values to test. We start by importing the numpy package and defining an array with our values.

Then, we call the smirnov_grubbs() function on the array and apply the alpha parameter for the significance level. Example 2: One-Sided Grubbs Test

One-sided Grubbs Test is used when detecting an outlier on either the lower or upper end of the dataset.

To detect an outlier on the lower or minimum end of the dataset, we can use the function min_test(). This function can detect and remove the smallest value that exceeds a given significance level.

Similarly, the max_test() function can detect and remove the largest value that exceeds the specified significance level. Example 3: Identifying outlier index position

In some cases, we may only need to know the location of the outliers in the dataset.

Using the function max_test_indices(), which returns the index position of the detected outlier, we can take the next steps to remove or replace it. Example 4: Extracting value of outlier

If we need to extract the value of the outlier(s) from a dataset, we can use the max_test_outliers() function to retrieve a tuple containing the values of the identified outlier(s).

How to handle outliers

Once outliers have been identified, we have several options for handling them. One option is to remove them completely from the dataset.

This is generally a good choice if the outliers are the result of data-entry errors or other non-systematic sources of noise. Another option is to replace them with reasonable estimates, such as the mean or median value of the dataset, based on the context and nature of the dataset.


Through the use of Grubbs’ Test implemented in Python, identifying statistical outliers within a dataset can be an efficient, highly flexible option for analyzing data. The ability to use this package to detect outliers, and the high flexibility to determine significance, can help to control for likely sources of systematic and non-systematic noise in data analysis.

In summary, Grubbs’ Test in Python provides a reliable statistical technique for identifying outliers in datasets that are approximately normally distributed. Through the use of outlier_utils, Python developers can perform one and two-sided tests, identify outlier index positions, extract the value of outliers, and ultimately handle outliers using either removal or replacement techniques.

The importance of this topic cannot be understated as outliers can significantly impact data quality and accuracy. By applying Grubbs’ Test, data analysts can ensure the validity of their dataset and make informed decisions.

Thus, it is crucial to have an understanding of this technique and its implementation in Python.