Python

SciPy ANOVA One-Way

The term “ANOVA” refers to “Analysis of Variance” and it is a statistical test that compares the means of multiple groups. The one-way ANOVA, known as one-factor ANOVA, examines the null hypothesis that all groups have the equivalent population mean. If the test is substantial, then at least one group differs. In Python, the “scipy.stats.f_oneway()” function executes a one-way ANOVA.

This write-up provides a comprehensive guide on “SciPy ANOVA One-Way” using the below contents:

What is ANOVA?

ANOVA” or “Analysis Of Variance” is a test comparing the means of various groups to see if they are distinct. It has two hypotheses: one assumes that all groups have equal means, and the other assumes that at least one group has a different mean.

ANOVA” is an omnibus test, meaning it can detect any difference among the groups but cannot tell which group is different. We need to use another test after ANOVA to determine which group is different.

What is the “scipy.stats.f_oneway()” Function in Python?

In Python, the “scipy.stats.f_oneway()” function performs a one-way analysis of variance test. This test determines whether a substantial difference exists between the means of multiple groups.

Syntax

scipy.stats.f_oneway(*samples, axis=0)

 

Parameters

In the above syntax:

  • The “*samples” parameter accepts the data sample list with at least two datasets, and multidimensional arrays should have the same dimensions.
  • The optional parameter “axis” indicates the array axis the test applies to.

Return Value

This function returns two values:

  • F-statistic”: The “F-statistic” measures the group variation.
  • P-value”: The “P-value” is the probability of getting a result as unusual as that observed if the null hypothesis value becomes “True”.

Note: A low P-value indicates that the null hypothesis is likely to be false, meaning that there is a substantial difference among group means.

Example 1: Compute the ANOVA One-Way Using the “scipy.stats.f_oneway()” Function

Here is an example of using the “scipy.stats.f_oneway()” function:

import scipy.stats
x1 = [41, 12, 43, 54, 45]
x2 = [26, 47, 18, 79, 10]
x3 = [15, 22, 43, 54, 25]
f_statistic, p_value = scipy.stats.f_oneway(x1, x2, x3)
print(f"F-statistic: {f_statistic}")
print(f"p-value: {p_value}")

 

In the above code:

  • The “scipy.stats” module is imported.
  • The three integers list x1, x2, and x3, are created, respectively. These lists represent the data for three different groups.
  • The “scipy.stats.f_oneway()” function takes the three data lists as its arguments and performs a one-way ANOVA test to evaluate if there is a substantial difference between the means of the three groups.
  • The F-statistic and P-value of the test are presented using the “print()” function.

Output

The “F-statistic” and “P-value” are shown in the above output.

Example 2: Compute the ANOVA One-Way of Multi-Dimensional Array Using the “scipy.stats.f_oneway()” Function

The following code computes the ANOVA one-way analysis of the multi-dimensional array:

import numpy
import scipy.stats
x1 = numpy.array([[1, 5, 5, 9, 5],[5, 15, 20, 5, 3],
                  [5, 2, 2, 3, 5],[2, 5, 3, 5, 4]])
x2 = numpy.array([[5, 10, 5, 20, 0],[10, 5, 2, 2, 30],
                  [15, 0, 5, 0, 3],[0, 2, 30, 3, 4]])
x3 = numpy.array([[5, 1, 1, 2, 30],[10, 1, 20, 2, 30],
                  [15, 2, 5, 3, 35],[2, 2, 3, 3, 4]])
f_statistic, p_value = scipy.stats.f_oneway(x1, x2, x3)
print(f"F-statistic: {f_statistic}")
print(f"p-value: {p_value}")

 

In the above code:

  • The “numpy” and “stats” libraries are imported.
  • The three NumPy arrays, x1, x2, and x3, containing different groups’ data, are initialized.
  • The “scipy.stats.f_oneway()” function tests the three groups employing the one-way ANOVA method.
  • The “f_oneway()” function takes three NumPy arrays as input and returns the F-statistic and the p-value, respectively.

Output

The “F-statistic” and the “p-value” are displayed in the above output.

Conclusion

The “scipy.stats.f_oneway()” function of the “seaborn” library is employed to perform a one-way analysis of variance test. This test aims to compute if there is a substantial difference among the means of multiple groups. We can also compute the one-way analysis on a multidimensional array using the “scipy.stats.f_oneway()” function. This post presented an in-depth guide on “SciPy ANOVA One-Way” using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.