Adventures in Machine Learning

Unlocking the Power of Mann-Whitney U Test in Python

Conducting Mann-Whitney U Test in Python: Your Comprehensive Guide

Statistical analysis helps researchers understand and draw conclusions from their data. Among the common statistical techniques available, Mann-Whitney U Test, also called Wilcoxon Rank-Sum Test, is a non-parametric test to compare two independent samples.

Are you unsure of what this test is and how to perform it in Python? This article is for you.

Overview of Mann-Whitney U Test

The Mann-Whitney U Test is used to determine if two independent groups have a significant difference in their central tendency, such as the medians of their data sets. Commonly used in the social and medical sciences, it is useful when the normality assumption of data distribution is violated.

Example of Mann-Whitney U Test in Python

For instance, we have two fuel treatments, treatment A and treatment B, that we want to compare using their miles per gallon (mpg) as the test variable. We have two sets of data that we want to test if they have a significant difference in their mpg based on their fuel treatment.

We will use Mann-Whitney U Test in Python’s Scipy.stats module to analyze the data. We have 10 observations or data points from each group or sample, and therefore, each sample has the same size.

Let us start by importing the scipy.stats library. “`

# Importing the necessary libraries

from scipy.stats import mannwhitneyu

“`

Next, we create the arrays for our sample observations.

In this case, we will name them sampleA and sampleB. “`

# Sample observations for fuel treatment A

sampleA=[25, 23, 27, 21, 32, 33, 19, 26, 28, 24]

# Sample observations for fuel treatment B

sampleB=[29, 19, 22, 27, 20, 31, 30, 26, 24, 28]

“`

We can now run the function mannwhitneyu() to perform the Mann-Whitney U Test.

“`

# Perform Mann-Whitney U Test

stat, p = mannwhitneyu(sampleA, sampleB)

print(“Statistics=%.3f, p=%.3f” % (stat, p))

“`

The output of the above code is:

“`

Statistics=44.000, p=0.422

“`

The p-value of 0.422 is above the significance level of 0.05, which means that there is not enough evidence to reject the null hypothesis that the fuel treatments have no significant effect on mpg. In simple terms, we cannot say that one fuel treatment has a significant mpg increase over the other.

Performing Mann-Whitney U Test in Python

In performing Mann-Whitney U Test in Python, the first step is to create arrays for the sample observations. Ensure that the size of each sample is the same.

The syntax for creating arrays is:

“`

# Sample observations for group A

sampleA=[data point_1, data point_2, data point_3, ……]

# Sample observations for group B

sampleB=[data point_1, data point_2, data point_3, ……]

“`

The next step is to import the scipy.stats library, which will enable us to use the mannwhitneyu() function. “`

# Importing the necessary libraries

from scipy.stats import mannwhitneyu

“`

To perform Mann-Whitney U Test using the Mann-Whitney U Test function in Python’s Scipy.stats module, the syntax is straightforward.

The function takes two arrays or sample data points as input arguments and returns two outputs: the test statistic and the p-value. Below is the syntax:

“`

# Perform Mann-Whitney U Test

stat, p = mannwhitneyu(sampleA, sampleB)

“`

In conclusion, the Mann-Whitney U Test is a useful statistical technique to test for significant differences between two independent samples.

If you are conducting a non-parametric test that violates the normality assumption of data distribution, the Mann-Whitney U Test in Python’s Scipy.stats module can be used to analyze the data. It is essential to create arrays for the sample observations, ensure that they have the same size, and import necessary libraries before running the mannwhitneyu() function to obtain the test statistic and p-value.

Interpreting Results of a Mann-Whitney U Test in Python

After performing a Mann-Whitney U Test in Python, you will obtain two output values, the test statistic and the p-value. These values provide useful information needed to interpret the results of the test and make informed conclusions from the analyzed data.

In this article, we will explore the significance level and the null and alternative hypotheses in more detail to better understand the interpretations of the Mann-Whitney U Test in Python.

Null and Alternative Hypotheses

The null hypothesis, denoted as H0 in statistics, is a statement that assumes no significant difference between two independent groups or samples. In our Mann-Whitney U Test example, the null hypothesis states that there is no significant difference in mpg between two fuel treatments.

The alternative hypothesis, also known as H1, is the opposite of the null hypothesis. It states that there is a significant difference between the two samples.

When conducting a Mann-Whitney U Test in Python, we set the null hypothesis as the default hypothesis. It is the hypothesis that we are testing against.

If we have enough evidence to reject the null hypothesis, we can conclude that there is a statistically significant difference between the two samples. Conversely, if we do not have strong enough evidence, we can fail to reject the null hypothesis, meaning that we do not have enough evidence to conclude that there is a significant difference between the two samples.

P-value and Significance Level

The p-value is the probability of observing a test statistic at least as extreme as the one calculated from the sample data, assuming that the null hypothesis is true. It helps us determine whether or not the null hypothesis should be rejected.

In statistical hypothesis testing, a smaller p-value indicates stronger evidence against the null hypothesis, favoring the alternative hypothesis.

The significance level, also known as alpha, is the threshold for rejecting the null hypothesis.

It is typically set at 5% or 0.05, meaning that if the p-value is below this threshold, we reject the null hypothesis. In other words, we are saying that the likelihood that the observed difference in samples is due to chance is 5% or less.

Interpretation of Results

To interpret the results of a Mann-Whitney U Test in Python, we must compare the p-value obtained to the predetermined significance level (alpha). If the p-value is less than the significance level, we reject the null hypothesis in favor of the alternative hypothesis.

On the other hand, if the p-value is greater than the significance level, we fail to reject the null hypothesis.

In our example of comparing two fuel treatments, we obtained a p-value of 0.422, which is greater than the significance level of 0.05.

Therefore, we fail to reject the null hypothesis, implying that there is not a significant difference between fuel treatments A and B. It is important to understand that failing to reject the null hypothesis does not mean that the null hypothesis is true.

It just means that we do not have enough evidence to reject it. We could have obtained a result with a p-value close to or slightly above the significance level.

In such cases, we must be cautious in interpreting the results and consider additional factors such as effect size, sample size, and the purpose of the study.

Conclusion

In conclusion, the Mann-Whitney U Test is a useful statistical technique employed to test for significant differences between two independent samples. Interpreting the results of a Mann-Whitney U Test in Python requires an understanding of the p-value, the significance level, and the null and alternative hypotheses.

The p-value represents the probability of observing a test statistic as extreme or more extreme than the one calculated from the sample data if the null hypothesis is true. The significance level is the threshold for rejecting the null hypothesis.

Failing to reject the null hypothesis does not mean that the null hypothesis is true, but rather that we do not have enough evidence to reject it. We should interpret the results cautiously and take into account additional factors that could impact the validity of the results.

In conclusion, the Mann-Whitney U Test is an effective non-parametric statistical technique used to compare two independent samples. Interpreting the results requires an understanding of the null and alternative hypotheses, the significance level, and the p-value.

The null hypothesis assumes no significant difference between the samples, while the alternative hypothesis assumes a significant difference. The p-value is the probability of observing a test statistic as extreme or more extreme than the one calculated from the sample data if the null hypothesis is true.

A significance level is set, and if the p-value is less than the significance level, we reject the null hypothesis. Careful interpretation should be done to account for other factors that may impact the validity of results.

Knowing how to conduct and interpret the Mann-Whitney U Test in Python is vital in making informed conclusions from analyzed data.

Popular Posts