Monotonic association
Assess whether two ordered measurements move together without assuming a linear relationship.
SciPy · Function
Computes Spearman rank-order correlation and its associated p-value. It measures monotonic association and supports one- or two-dimensional array-like inputs.
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.
scipy.stats.spearmanr(a, b=None, axis=0, nan_policy='propagate', alternative='two-sided')| Name | Type | Description |
|---|---|---|
a, b | 1D or 2D array_like | One or two one-dimensional or two-dimensional array-like inputs containing variables and observations; b is optional. |
axis | int or None, default 0 | Dimension 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. |
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.
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.
Assess whether two ordered measurements move together without assuming a linear relationship.
Compare many variables at once and inspect pairwise rank correlations.
Test whether paired samples show evidence of ordinal association.
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.
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.
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.
pearsonrkendalltaurankdata