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

SciPy · Function

stats.pearsonr

Computes the Pearson correlation coefficient between two datasets and tests the null hypothesis of non-correlation. It returns the coefficient and a p-value for the selected alternative hypothesis.

Since
1.9.0

How it works

pearsonr compares two equally sized datasets, computing their Pearson product-moment correlation and testing the null hypothesis that the underlying distributions are uncorrelated and normally distributed. The returned result exposes the coefficient as statistic and the selected hypothesis-test p-value as pvalue.

Syntax

scipy.stats.pearsonr(x, y, *, alternative='two-sided', method=None)

Parameters

NameTypeDescription
x(N,) array_likeInput array.
y(N,) array_likeInput array.
alternative{'two-sided', 'greater', 'less'}, optionalDefines the alternative hypothesis. Options are two-sided, greater, and less; the default is two-sided.
methodResamplingMethod, optionalDefines the method used to compute the p-value.

Returns

A PearsonRResult with statistic, the Pearson product-moment correlation coefficient, and pvalue, the p-value associated with the chosen alternative.

Key points

  • Use pearsonr to quantify linear association between paired observations.
  • The statistic ranges from -1 through 1; zero represents no linear correlation.
  • Values at either endpoint indicate a perfect linear relationship.
  • A positive coefficient means the variables tend to increase together.
  • A negative coefficient means larger x values tend to accompany smaller y values.
  • Use the p-value to assess extremeness under the uncorrelated null model.

Common mistakes

  • Interpret the p-value under the documentation's normality assumption for both datasets.
  • Choose alternative to match the hypothesis; the default tests for a nonzero correlation.

Example: Correlation and significance test

from scipy.stats import pearsonr

x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 8, 10]

result = pearsonr(x, y)
print(result.statistic)
print(result.pvalue)

Computes the sample Pearson correlation and the default two-sided p-value.

When to use it

Linear association

Measure the strength and direction of a linear relationship between two numeric datasets.

Hypothesis testing

Test whether observed linear correlation is compatible with an uncorrelated population under the documented assumptions.

Directional testing

Use a one-sided alternative when the scientific question specifies positive or negative correlation in advance.

Interview questions

QWhat does scipy.stats.pearsonr compute?

It returns a Pearson product-moment correlation coefficient and a p-value for testing non-correlation. The coefficient describes the linear relationship, while the p-value indicates how likely an uncorrelated system is to produce a result at least as extreme.

Follow-up: What does a coefficient of -1 mean? It indicates an exact negative linear relationship: as one variable increases, the other decreases exactly.

QHow does alternative affect the p-value?

The alternative argument selects the hypothesis direction. two-sided tests whether the correlation is nonzero, less tests whether it is negative, and greater tests whether it is positive.

Follow-up: When should greater be used? Use it when the planned alternative hypothesis is a positive correlation, rather than selecting it after inspecting the data.

QWhat does the returned object contain?

The result has statistic and pvalue attributes. statistic is the Pearson correlation coefficient, and pvalue corresponds to the selected alternative hypothesis.

Follow-up: Why use named attributes instead of positional unpacking? They make the meaning of each returned value explicit and match the documented result object.

  • scipy.stats.spearmanr
  • scipy.stats.kendalltau
  • scipy.stats.PearsonRResult

Source: scipy.stats.pearsonr#. Checked 2026-10-15.