Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.spearmanr

Computes Spearman rank-order correlation and its associated p-value. It measures monotonic association and supports one- or two-dimensional array-like inputs.

How it works

The function ranks the observations and evaluates whether two variables have a monotonic relationship. It returns the correlation coefficient together with a p-value for the null hypothesis that the samples have no ordinal correlation; with multiple variables, it produces a square correlation matrix.

Syntax

scipy.stats.spearmanr(a, b=None, axis=0, nan_policy='propagate', alternative='two-sided')

Parameters

NameTypeDescription
a, b1D or 2D array_likeOne or two one-dimensional or two-dimensional array-like inputs containing variables and observations; b is optional.
axisint or None, default 0Dimension along which observations are arranged. If None, both inputs are flattened.
nan_policy{'propagate', 'raise', 'omit'}, default 'propagate'How missing values are handled: propagate, raise, or omit.
alternative{'two-sided', 'less', 'greater'}, default 'two-sided'Defines the alternative hypothesis for the correlation test.

Returns

A result containing the Spearman correlation coefficient in statistic and the hypothesis-test p-value in pvalue; scalar values are returned for two variables, while multivariable input yields square matrices.

Key points

  • It is a rank-based, nonparametric measure of how consistently two datasets move in the same or opposite order.
  • The coefficient is bounded from -1 to 1; values near zero indicate little monotonic association.
  • A value of 1 or -1 represents a perfectly increasing or decreasing monotonic relationship.
  • Inputs may be one- or two-dimensional array-like objects, with an optional second input.
  • For multivariable input, the returned correlation matrix is square and covers every variable from both inputs.

Common mistakes

  • When supplying two arrays, their sizes must agree along the selected axis.
  • Setting axis=None flattens each input before calculating the statistic, which changes how observations are paired.
  • Interpret the p-value as an approximate extremeness probability under no association, not as the probability that the null hypothesis is true.

Example: Compute a rank correlation

from scipy.stats import spearmanr

study_hours = [1, 2, 3, 4, 5]
scores = [52, 58, 65, 73, 81]
result = spearmanr(study_hours, scores)
print(result.statistic, result.pvalue)

Computes the rank correlation and its associated two-sided p-value for two paired samples.

When to use it

Monotonic association

Assess whether two ordered measurements move together without assuming a linear relationship.

Correlation matrix

Compare many variables at once and inspect pairwise rank correlations.

Hypothesis testing

Test whether paired samples show evidence of ordinal association.

Interview questions

QWhat does spearmanr measure, and what does it return?

It measures monotonic association by replacing values with their ranks, rather than requiring a linear relationship. The coefficient ranges from -1 to 1, and the result also includes a p-value for no ordinal correlation.

Follow-up: How does it differ from Pearson correlation? Spearman is nonparametric and rank-based, so it does not require normally distributed variables and can capture monotonic relationships that are not linear.

QWhat happens when the inputs contain multiple variables?

With one two-dimensional input, each column or row, according to axis, is treated as a variable and the function returns a square correlation matrix. Its size equals the total number of variables supplied across a and b.

Follow-up: What shape should you expect when combining two multivariable inputs? The matrix dimension is the combined variable count, while both inputs must match in their observation axis length.

QHow do axis and input layout affect the calculation?

The default axis is 0, so observations are generally arranged along rows and variables along columns for two-dimensional arrays. Passing axis=None ravels both inputs before computation; axis can also select another dimension.

Follow-up: How would you compute correlations across rows instead? Set axis=1, ensuring the corresponding observation dimension has compatible lengths.

  • pearsonr
  • kendalltau
  • rankdata

Source: spearmanr — SciPy v1.18.0 Manual. Checked 2026-10-15.