Treating Outliers in Python

7 / 16

IQR Method

In this method we will use the Inter Quartile Range(IQR) to detect outliers. IQR tells us the variation in the data set. Any value, which is beyond the range of -1.5 x IQR to 1.5 x IQR are treated as outliers

  • Q1 represents the 1st quartile/25th percentile of the data.
  • Q2 represents the 2nd quartile/median/50th percentile of the data.
  • Q3 represents the 3rd quartile/75th percentile of the data.
  • (Q1–1.5IQR) represent the smallest value in the data set and (Q3+1.5IQR) represent the largest value in the data set.
INSTRUCTIONS
  • First, we will import Numpy as np

    import numpy as <<your code goes here>>
    
  • Next, we will define the datapoints we had used previously

    x = [5, 5, 5, -99, 5, 5, 5, 5, 5, 5, 88, 5, 5, 5]
    
  • Now, we will define a function calculate_iqr to detect the outliers using the IQR method

    def <<your code goes here>>(data):
        Q1 = np.percentile(data, 25, interpolation = 'midpoint')  
        Q2 = np.percentile(data, 50, interpolation = 'midpoint')  
        Q3 = np.percentile(data, 75, interpolation = 'midpoint')
        IQR = Q3 - Q1
        low_lim = Q1 - 1.5 * IQR
        up_lim = Q3 + 1.5 * IQR
        outlier =[]
        for num in x:
            if ((num> up_lim) or (num<low_lim)):
                outlier.append(num)
        print("Outliers:",outlier)
    
  • Finally, we will call the function using our datapoints

    <<your code goes here>>(x)
    
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...