This is a pre-release version (2.5.0.dev0+git20251130.2de293a). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / corrcoef

_ArrayFunctionDispatcher

numpy:corrcoef

source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py :2897

Signature

def   corrcoef ( x y = None rowvar = True * dtype = None )

Summary

Return Pearson product-moment correlation coefficients.

Extended Summary

Please refer to the documentation for cov for more detail. The relationship between the correlation coefficient matrix, R, and the covariance matrix, C, is

The values of R are between -1 and 1, inclusive.

Parameters

x : array_like

A 1-D or 2-D array containing multiple variables and observations. Each row of x represents a variable, and each column a single observation of all those variables. Also see rowvar below.

y : array_like, optional

An additional set of variables and observations. y has the same shape as x.

rowvar : bool, optional

If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.

dtype : data-type, optional

Data-type of the result. By default, the return data-type will have at least numpy.float64 precision.

Returns

R : ndarray

The correlation coefficient matrix of the variables.

Notes

Due to floating point rounding the resulting array may not be Hermitian, the diagonal elements may not be 1, and the elements may not satisfy the inequality abs(a) <= 1. The real and imaginary parts are clipped to the interval [-1, 1] in an attempt to improve on that situation but is not much help in the complex case.

Examples

import numpy as np
In this example we generate two random arrays, ``xarr`` and ``yarr``, and compute the row-wise and column-wise Pearson correlation coefficients, ``R``. Since ``rowvar`` is true by default, we first find the row-wise Pearson correlation coefficients between the variables of ``xarr``.
import numpy as np
rng = np.random.default_rng(seed=42)
xarr = rng.random((3, 3))
xarr
R1 = np.corrcoef(xarr)
R1
If we add another set of variables and observations ``yarr``, we can compute the row-wise Pearson correlation coefficients between the variables in ``xarr`` and ``yarr``.
yarr = rng.random((3, 3))
yarr
R2 = np.corrcoef(xarr, yarr)
R2
Finally if we use the option ``rowvar=False``, the columns are now being treated as the variables and we will find the column-wise Pearson correlation coefficients between variables in ``xarr`` and ``yarr``.
R3 = np.corrcoef(xarr, yarr, rowvar=False)
R3

See also

cov

Covariance matrix

Aliases

  • numpy.corrcoef

Referenced by