numpy.corrcoef#

numpy.corrcoef(x,y=None,rowvar=True,bias=<novalue>,ddof=<novalue>,*,dtype=None)[source]#

Return Pearson product-moment correlation coefficients.

Please refer to the documentation forcov for more detail. Therelationship between the correlation coefficient matrix,R, and thecovariance matrix,C, is

\[R_{ij} = \frac{ C_{ij} } { \sqrt{ C_{ii} C_{jj} } }\]

The values ofR are between -1 and 1, inclusive.

Parameters:
xarray_like

A 1-D or 2-D array containing multiple variables and observations.Each row ofx represents a variable, and each column a singleobservation of all those variables. Also seerowvar below.

yarray_like, optional

An additional set of variables and observations.y has the sameshape asx.

rowvarbool, optional

Ifrowvar is True (default), then each row represents avariable, with observations in the columns. Otherwise, the relationshipis transposed: each column represents a variable, while the rowscontain observations.

bias_NoValue, optional

Has no effect, do not use.

Deprecated since version 1.10.0.

ddof_NoValue, optional

Has no effect, do not use.

Deprecated since version 1.10.0.

dtypedata-type, optional

Data-type of the result. By default, the return data-type will haveat leastnumpy.float64 precision.

New in version 1.20.

Returns:
Rndarray

The correlation coefficient matrix of the variables.

See also

cov

Covariance matrix

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 theinequality abs(a) <= 1. The real and imaginary parts are clipped to theinterval [-1, 1] in an attempt to improve on that situation but is notmuch help in the complex case.

This function accepts but discards argumentsbias andddof. This isfor backwards compatibility with previous versions of this function. Thesearguments had no effect on the return values of the function and can besafely ignored in this and previous versions of numpy.

Examples

>>>importnumpyasnp

In this example we generate two random arrays,xarr andyarr, andcompute the row-wise and column-wise Pearson correlation coefficients,R. Sincerowvar is true by default, we first find the row-wisePearson correlation coefficients between the variables ofxarr.

>>>importnumpyasnp>>>rng=np.random.default_rng(seed=42)>>>xarr=rng.random((3,3))>>>xarrarray([[0.77395605, 0.43887844, 0.85859792],       [0.69736803, 0.09417735, 0.97562235],       [0.7611397 , 0.78606431, 0.12811363]])>>>R1=np.corrcoef(xarr)>>>R1array([[ 1.        ,  0.99256089, -0.68080986],       [ 0.99256089,  1.        , -0.76492172],       [-0.68080986, -0.76492172,  1.        ]])

If we add another set of variables and observationsyarr, we cancompute the row-wise Pearson correlation coefficients between thevariables inxarr andyarr.

>>>yarr=rng.random((3,3))>>>yarrarray([[0.45038594, 0.37079802, 0.92676499],       [0.64386512, 0.82276161, 0.4434142 ],       [0.22723872, 0.55458479, 0.06381726]])>>>R2=np.corrcoef(xarr,yarr)>>>R2array([[ 1.        ,  0.99256089, -0.68080986,  0.75008178, -0.934284  ,        -0.99004057],       [ 0.99256089,  1.        , -0.76492172,  0.82502011, -0.97074098,        -0.99981569],       [-0.68080986, -0.76492172,  1.        , -0.99507202,  0.89721355,         0.77714685],       [ 0.75008178,  0.82502011, -0.99507202,  1.        , -0.93657855,        -0.83571711],       [-0.934284  , -0.97074098,  0.89721355, -0.93657855,  1.        ,         0.97517215],       [-0.99004057, -0.99981569,  0.77714685, -0.83571711,  0.97517215,         1.        ]])

Finally if we use the optionrowvar=False, the columns are nowbeing treated as the variables and we will find the column-wise Pearsoncorrelation coefficients between variables inxarr andyarr.

>>>R3=np.corrcoef(xarr,yarr,rowvar=False)>>>R3array([[ 1.        ,  0.77598074, -0.47458546, -0.75078643, -0.9665554 ,         0.22423734],       [ 0.77598074,  1.        , -0.92346708, -0.99923895, -0.58826587,        -0.44069024],       [-0.47458546, -0.92346708,  1.        ,  0.93773029,  0.23297648,         0.75137473],       [-0.75078643, -0.99923895,  0.93773029,  1.        ,  0.55627469,         0.47536961],       [-0.9665554 , -0.58826587,  0.23297648,  0.55627469,  1.        ,        -0.46666491],       [ 0.22423734, -0.44069024,  0.75137473,  0.47536961, -0.46666491,         1.        ]])
On this page