Finding the outlier points from Matplotlib
Outliers are the data points that differ from other observations or those which lie at a distance from the other data. They are mainly generated due to some experimental error which may cause several problems in statistical analysis. While in a big dataset it is quite obvious that some data will be further from the sample mean. These outliers need to be found and handle wisely.
We can use boxplots for the necessary.
Above is a diagram of boxplot created to display the summary of data values along with its median, first quartile, third quartile, minimum and maximum. And the data points out of the lower and upper whiskers are outliers. In between the first and third quartile of whisker lies the interquartile region above which a vertical line passes known as the median. For further details refer to the blog Box plot using python. Following are the methods to find outliers from a boxplot :
1.Visualizing through matplotlib boxplot using plt.boxplot(). 2.Using 1.5 IQR rule.
array([4, 12, 15, 7, 13, 2, 12, 11, 10, 12, 15, 5, 9, 16, 17, 2, 10, 15, 4, 16, 14, 19, 12, 8, 13, 3, 16, 10, 1, 13, 27, 30])
Visualizing by matplotlib boxplot using plt.boxplot()
So from the above figure, we can witness the two outliers.
1.5 IQR Rule
Steps in 1.5IQR rule:-
- Finding the median, quartile, and interquartile regions
- Calculate 1.5*IQR below the first quartile and check for low outliers.
- Calculate 1.5*IQR above the third quartile and check for outliers.
8.25 26.375 -6.625
The following are the outliers in the boxplot:[27 30]
Thus, the outliers have been detected using the rule. Now eliminating them and plotting a graph with the data points-