Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Choose a statistical test by your study design and the quantity you want to compare—not simply by whether your data look normal. In SciPy, use an independent-samples t-test to compare means, Mann–Whitney U to compare two independent distributions, Wilcoxon signed-rank for paired differences, and Kruskal–Wallis for a rank-based comparison of several independent groups. These tests have different assumptions and null hypotheses, so they are not interchangeable versions of the same test.

Start with the design and the question

Before choosing a parametric or non-parametric method, answer two questions: are the observations independent or paired, and what feature of the data should the test compare? A parametric test such as a t-test targets means under a model with specified assumptions. Rank-based tests work with the ordering of observations and test different hypotheses.

  • Independent samples: Each observation in one group comes from a different unit than observations in the other group.
  • Paired samples: Each value in one sample is meaningfully matched to a value in the other, such as measurements from the same person before and after an intervention.
  • Target: Decide whether the question concerns average values, paired changes, or a broader difference between distributions.

SciPy’s statistical functions reference groups tests by common uses and sample structures, but its categories are a guide rather than a complete decision rule.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a test for two groups

Design and target SciPy function What to keep in mind
Two independent groups; compare means scipy.stats.ttest_ind Tests equality of average values. Its default assumes equal population variances.
Two independent groups; compare distributions using ranks scipy.stats.mannwhitneyu Tests whether the underlying distributions are the same; it is not universally a test of medians.
Two paired groups; test paired differences scipy.stats.wilcoxon Works on paired differences; the signed-rank test’s null concerns differences symmetric about zero.

Independent means: t-test

Use scipy.stats.ttest_ind when the target is the difference in average values between independent groups. By default, equal_var=True assumes the populations have identical variances. If that assumption is not appropriate, set equal_var=False to use Welch’s t-test instead. That changes the variance assumption while retaining a mean-comparison target; it does not make the test non-parametric.

from scipy import stats

result = stats.ttest_ind(group_a, group_b, equal_var=False)
print(result.statistic, result.pvalue)

The statistic and p-value are returned in the result. Interpret the p-value in relation to the stated null hypothesis and study design; it does not measure the size or practical importance of a difference. SciPy’s documentation also describes a permutation method for this test, so consult the reference for the current version and available options.

Independent distributions: Mann–Whitney U

Use scipy.stats.mannwhitneyu for two independent samples when a rank-based comparison of their distributions matches the question. Its null hypothesis is that the underlying distributions are the same. A result may often be discussed as evidence of a location difference when the distributions have comparable shapes, but the test should not be described as a universal median test.

from scipy import stats

result = stats.mannwhitneyu(group_a, group_b, alternative="two-sided")
print(result.statistic, result.pvalue)

Choose the alternative hypothesis to match the analysis plan. Check the SciPy reference for the version-specific method and handling of sample size and ties rather than assuming one setting suits every dataset.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Paired measurements: Wilcoxon signed-rank

For two related samples, use scipy.stats.wilcoxon to test paired differences. The analysis is not an independent comparison of the two raw samples: the matching between values is central. SciPy describes the null in terms of the paired differences being symmetric about zero.

from scipy import stats

result = stats.wilcoxon(before, after)
print(result.statistic, result.pvalue)

Supply corresponding observations in the same order. If the data do not have meaningful pairs, use an independent-samples method instead; if they do, discarding the pairing changes the design being analyzed.

Choose a test for more than two groups

Several independent groups: Kruskal–Wallis

scipy.stats.kruskal is a rank-based omnibus test for several independent groups. Its null concerns the groups’ distributions. SciPy cautions that group sizes must not be too small for the test’s chi-square approximation to be appropriate.

from scipy import stats

result = stats.kruskal(group_a, group_b, group_c)
print(result.statistic, result.pvalue)

A significant omnibus result indicates evidence that the groups are not all alike under the test’s null; it does not identify which groups differ. Plan any follow-up comparisons separately, including how you will account for multiple testing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Several groups; compare means

SciPy’s statistics reference lists one-way ANOVA for mean-based comparisons across groups. Choose it when the model, design, target, and assumptions fit the study. The reference listing alone is not a complete guide to every ANOVA assumption or design variant.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why “normal or not?” is not enough

A test choice cannot be made reliably from a normality label alone. A distribution-based rank test and a mean-based test may address different questions, even when applied to the same observations. First preserve the independent or paired structure, then select a method whose null hypothesis matches the quantity of interest. Also review the implementation’s assumptions and approximation method for the SciPy version you are using.

  • For a mean difference between independent groups, consider the t-test and make the equal-variance choice explicit.
  • For a rank-based comparison of independent distributions, consider Mann–Whitney U without calling it a median test by default.
  • For matched observations, test the paired differences rather than treating values as independent.
  • For several independent groups, distinguish an omnibus test from the follow-up comparisons needed to locate differences.

Check the SciPy version before adapting code

Function options can change between SciPy releases. The examples above use documented function names and explicit choices where they matter; consult the linked reference for the version installed in your environment before relying on optional arguments. In particular, do not copy older permutation or random-state argument forms into code intended for a newer release.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.