Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
How to Perform Dunn’s Test in Python
Next article icon

How to Perform Dunn’s Test in Python

Last Updated : 29 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Dunn's test is a statistical procedure used for multiple comparisons following a Kruskal-Wallis test. Here's a breakdown of what it does and when it's used:

Table of Content

  • Dunn’s Test
  • What is the Kruskal-Wallis test?
  • Key points about Dunn's test
  • How to Perform Dunn’s Test with Python
  • Step-by-Step Guide to Perform Dunn’s Test in Python
  • Frequently Asked Questions on Dunn’s Test

Dunn’s Test

Dunn’s Test is used after the Kruskal-Wallis one-way analysis of variance by ranks to identify which groups differ from each other. It determines whether the difference between the medians of various groups is statistically significant. Dunn’s Test adjusts for multiple comparisons, making it suitable for analyzing data with several groups.

Dunn’s Test is a non-parametric statistical test used for comparing multiple groups to each other. It's particularly useful when analyzing data with unequal sample sizes or when the assumption of normality is violated.

What is the Kruskal-Wallis test?

The Kruskal-Wallis test is a non-parametric statistical test used to determine whether there are statistically significant differences between three or more independent groups. If the Kruskal-Wallis test indicates significant differences, Dunn's test can be applied post-hoc to identify which specific pairs of groups differ significantly from each other. Dunn's test is tailored for pairwise comparisons following a significant result in the Kruskal-Wallis test, providing insights into specific group differences.

Key points about Dunn's test

  1. Purpose: Dunn's test is used to identify which specific groups differ from each other when there are statistically significant differences detected between groups in the omnibus test.
  2. Non-parametric: Like the Kruskal-Wallis and Friedman tests, Dunn's test is non-parametric, meaning it does not rely on assumptions about the distribution of the data.
  3. Procedure: Dunn's test calculates pairwise comparisons between all groups using a rank-based approach. It computes the difference in ranks between pairs of groups and adjusts the p-values for multiple comparisons using methods such as the Bonferroni correction.
  4. Interpretation: If the adjusted p-value for a pairwise comparison is below a predetermined significance level (e.g., 0.05), it indicates that the difference between those two groups is statistically significant.
  5. Interpretation: If the adjusted p-value for a pairwise comparison is below a predetermined significance level (e.g., 0.05), it indicates that the difference between those two groups is statistically significant.

Overall, Dunn's test provides a valuable tool for identifying specific group differences in situations where traditional parametric tests are not appropriate or when dealing with ranked data. It helps researchers gain deeper insights into the relationships between multiple groups in their data.

How to Perform Dunn’s Test with Python

In Python, the scikit-posthocs library provides an efficient way to conduct Dunn’s Test. This article will guide you through the process of performing Dunn’s Test in Python, step by step.

Syntax to install posthocs library:

! pip install scikit-posthocs

posthoc_dunn() Function:

Syntax:

scikit_posthocs.posthoc_dunn(a, val_col: str = None, group_col: str = None, p_adjust: str = None, sort: bool = True)

Parameters:

  • a : it's an array type object or a dataframe object or series.
  •  group_col : column of the predictor or the dependent variable
  • p_adjust: P values can be adjusted using this method. it's a string type possible values are :
    • 'bonferroni'
    • hommel
    • holm-sidak
    • holm
    • simes-hochberg and more...

Returns: p-values.

Hypotheses:

This is a hypotheses test and the two hypotheses are as follows:

  • Null hypothesis:  The given sample have the same median
  • Alternative hypothesis:  The given sample has a different median.

Step-by-Step Guide to Perform Dunn’s Test in Python

1. Import Necessary Libraries

Import the required libraries for data manipulation and Dunn’s Test:

Python3
# Importing necessary packages and modules import pandas as pd import scikit_posthocs as sp from sklearn.datasets import load_iris 

2. Load Your Dataset

Load your dataset into a pandas DataFrame. Ensure your data is structured appropriately for comparison:

Python3
# Load the dataset iris_dataset = load_iris(as_frame=True) dataset = iris_dataset.frame print(dataset.head()) 

Output:

   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  \ 0                5.1               3.5                1.4               0.2    1                4.9               3.0                1.4               0.2    2                4.7               3.2                1.3               0.2    3                4.6               3.1                1.5               0.2    4                5.0               3.6                1.4               0.2        target   0       0   1       0   2       0   3       0   4       0

3. Prepare Your Data

Extract the data you want to compare. In this example, let's compare sepal widths among different species:

Python3
# Data containing sepal width of the three species data = [dataset[dataset['target'] == 0]['sepal width (cm)'],         dataset[dataset['target'] == 1]['sepal width (cm)'],         dataset[dataset['target'] == 2]['sepal width (cm)']] 

4. Perform Dunn’s Test:

Python3
# Using the posthoc_dunn() function p_values = sp.posthoc_dunn(data, p_adjust='holm')  print(p_values) 

Output:

              1             2             3 1  1.000000e+00  2.047087e-14  1.536598e-07 2  2.047087e-14  1.000000e+00  1.580934e-02 3  1.536598e-07  1.580934e-02  1.000000e+00
  • For the difference between groups 1 and 2, the adjusted p-value is 2.047087e-14
  • For the difference between groups 1 and 3, the adjusted p-value is 1.536598e-07
  • For the difference between groups 2 and 3, the adjusted p-value is 1.580934e-02

5. Compare with significance level

Let's assume our significance level is 0.05, So, we will check if p_values less than the chosen significance level indicate statistically significant differences between groups.

Python3
print(p_values <0.05) 

 Output:

       1      2      3 1  False   True   True 2   True  False   True 3   True   True  False

This indicates that:

  • Group 1 is significantly different from Group 2 and Group 3.
  • Group 2 is significantly different from Group 1 and Group 3.
  • Group 3 is significantly different from Group 1 and Group 2.

Conclusion

Performing Dunn’s Test in Python using the scikit-posthocs library is straightforward and efficient. By following the steps outlined in this article, you can accurately assess the differences between multiple groups in your dataset. Dunn’s Test is a valuable tool for post hoc analysis, providing insights into group comparisons beyond traditional statistical methods.


Next Article
How to Perform Dunn’s Test in Python

S

sarahjane3102
Improve
Article Tags :
  • Machine Learning
  • Geeks Premier League
  • Statistics
  • AI-ML-DS
  • Geeks-Premier-League-2022
  • Python-pandas
  • Python scikit-module
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    How to Perform an F-Test in Python
    In statistics, Many tests are used to compare the different samples or groups and draw conclusions about populations. These techniques are commonly known as Statistical Tests or hypothesis Tests. It focuses on analyzing the likelihood or probability of obtaining the observed data that they are rando
    10 min read
    How to Perform Grubbs’ Test in Python
    Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing  In this article, we will be discussing the different approaches to perform Grubbs’ Test in Python programming language.  Grubbs’ Test is also known as the maximum normalized residual test or extreme studentized deviate test is
    3 min read
    How to Perform a Shapiro-Wilk Test in Python
    In this article, we will be looking at the various approaches to perform a Shapiro-wilk test in Python. Shapiro-Wilk test is a test of normality, it determines whether the given sample comes from the normal distribution or not. Shapiro-Wilk’s test or Shapiro test is a normality test in frequentist s
    2 min read
    How to Perform a Brown – Forsythe Test in Python
    Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing  In this article, we will be looking at the approach to perform a brown-Forsythe test in the Python programming language. Brown–Forsythe test is a statistical test for the equality of group variances based on performing an Anal
    4 min read
    How to Perform Welch’s ANOVA in Python
    When the assumption of equal variances is violated, Welch's ANOVA is used as an alternative to the standard one-way ANOVA. A one-way ANOVA ("analysis of variance") is used to see if there is a statistically significant difference in the means of three or more independent groups. Steps to perform Wel
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences