In this article, we will learn how to utilize the functionalities provided by excel and python libraries to calculate IQR,
In this article, we will learn how to utilize the functionalities provided by excel and python libraries to calculate IQR,
In previous article we saw how to calculate the IQR by hand with multiple methods. In this article, we will learn how to utilize the functionalities provided by excel and python libraries to achieve the same.
Calculating Interquartile Range with Excel and python libraries.
In the previous article, how to find IQR with mathematical formulae. Now we will learn to use these mathematical formulas implemented in Excel and various python libraries.
In all the quartile functions below, the format for the argument is the same.
QUARTILE(range, Qno)
QUARTILE(A1:A9, 1)
QUARTILE.INC(A1:A9, 1)
QUARTILE.EXC(A1:A9, 1)
Excel provides three functions to calculate quartile, quartile.exc and quartile.inc.
Quartile provides answers similar to quartile.inc.
Quartile.inc uses median inclusion method to find quartiles of the provided range of numbers and number of quartile in the function.
Inversely, Quartile.exc uses median exclusion to find quartiles of the provided range of numbers and numbers of quartile in the function.
In the following example, series column shows a2:a9 cells are filled with dataset.
We are showing a quartile calculated left side table. We are calculating quartiles 1,2 and 3. Which are 25%, 50% and 75% respectively.
We show formulas and arguments for calculating the IQR with each method.
In the following image, we show actual calculations and answers of the series
Following is an example for calculating the quartiles and IQR using python NumPy library.
# import libraries
import numpy as np
import pandas as pd
# create list data series
series=[15,36,39,40,41,42,43,47,49]
# create dataaaframe and insert series as a column
df=pd.DataFrame(data=series,columns=['series'])
print(df)
series
0 15
1 36
2 39
3 40
4 41
5 42
6 43
7 47
8 49
Python# calculate first quartile
q1=np.quantile(df,0.25)
# calculate second quartile
q2=np.quantile(df,0.5)
# calculate three quartile
q3=np.quantile(df,0.75)
print(f'Q1: {q1}\nQ2: {q2}\nQ3: {q3}')
# calculate IQR by substracting Q3 with Q1
print(f'IQR: {q3-q1}')
Q1: 39.0
Q2: 41.0
Q3: 43.0
IQR: 4.0
Python# Load all the available methods provided by library.
methods=['inverted_cdf','averaged_inverted_cdf','closest_observation','interpolated_inverted_cdf','hazen','weibull','linear','median_unbiased','normal_unbiased']
# traverse through all methods and calculate IQR with each method.
for i in methods:
print(i.upper())
q1=np.quantile(df,0.25,method=i)
q2=np.quantile(df,0.5,method=i)
q3=np.quantile(df,0.75,method=i)
print(f'Q1: {q1}\nQ2: {q2}\nQ3: {q3}')
print(f'IQR: {q3-q1}\n\n')
INVERTED_CDF Q1: 39 Q2: 41 Q3: 43 IQR: 4 | INTERPOLATED_INVERTED_CDF Q1: 36.75 Q2: 40.5 Q3: 42.75 IQR: 6.0 | LINEAR Q1: 39.0 Q2: 41.0 Q3: 43.0 IQR: 4.0 |
AVERAGED_INVERTED_CDF Q1: 39.0 Q2: 41.0 Q3: 43.0 IQR: 4.0 | HAZEN Q1: 38.25 Q2: 41.0 Q3: 44.0 IQR: 5.75 | MEDIAN_UNBIASED Q1: 38.0 Q2: 41.0 Q3: 44.33333333333333 IQR: 6.333333333333329 |
CLOSEST_OBSERVATION Q1: 36 Q2: 41 Q3: 43 IQR: 7 | WEIBULL Q1: 37.5 Q2: 41.0 Q3: 45.0 IQR: 7.5 | NORMAL_UNBIASED Q1: 38.0625 Q2: 41.0 Q3: 44.25 IQR: 6.1875 |
In the above example, we calculated IQR with NumPy’s quartile function. Let’s calculate IQR directly using SciPy library. This library directly provides ability to calculate IQR of a series. It also provides different interpolation techniques for calculations. Linear, Lower, Higher, Nearest, Midpoint.
interpol = ['linear', 'lower', 'higher', 'nearest', 'midpoint']
import scipy.stats
for i in interpol:
print(i.upper())
print(f'IQR: {scipy.stats.iqr(df,interpolation=i)}')
LINEAR
IQR: 4.0
LOWER
IQR: 4
HIGHER
IQR: 4
NEAREST
IQR: 4
MIDPOINT
IQR: 4.0
PythonANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.
What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!
Start using NotebookLM today and embark on a smarter, more efficient learning journey!
This can be a super guide for you to start and excel in your data science career.
After tourism was established as a motivator of local economies (country, state), many governments stepped up to the plate.
Sentiment analysis can determine the polarity of sentiments from given sentences. We can classify them into certain categories.
Traverse a dictionary with for loop Accessing keys and values in dictionary. Use Dict.values() and Dict.keys() to generate keys and values as iterable. Nested Dictionaries with for loop Access Nested values of Nested Dictionaries How useful was this post? Click on a star to rate it! Submit Rating
For loop is one of the most useful methods to reuse a code for repetitive execution.
These all metrics are revolving around visits and hits which we are getting on websites. Single page visits, Bounce, Cart Additions, Bounce Rate, Exit rate,
Hypothesis testing is a statistical method for determining whether or not a given hypothesis is true. A hypothesis can be any assumption based on data.
A/B tests are randomly controlled experiments. In A/B testing, you get user response on various versions of the product, and users are split within multiple versions of the product to figure out the “winner” of the version.
This article covers ‘for’ loops and how they are used with tuples. Even if the tuples are immutable, the accessibility of the tuples is similar to that of the list.
MANOVA is an update of ANOVA, where we use a minimum of two dependent variables.
You only need to understand two or three concepts if you have read the one-way ANOVA article. We use two factors instead of one in a two-way ANOVA.
Leave a Reply
You must be logged in to post a comment.