Return Pearson product-moment correlation coefficients. Please refer to the documentation for `cov` for more detail. The relationship between the correlation coefficient matrix, `R`, and the covariance matrix, `C`, is .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} C_{jj
(x, y=None, rowvar=True, *,
dtype=None)
| 2909 | |
| 2910 | @array_function_dispatch(_corrcoef_dispatcher) |
| 2911 | def corrcoef(x, y=None, rowvar=True, *, |
| 2912 | dtype=None): |
| 2913 | """ |
| 2914 | Return Pearson product-moment correlation coefficients. |
| 2915 | |
| 2916 | Please refer to the documentation for `cov` for more detail. The |
| 2917 | relationship between the correlation coefficient matrix, `R`, and the |
| 2918 | covariance matrix, `C`, is |
| 2919 | |
| 2920 | .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} C_{jj} } } |
| 2921 | |
| 2922 | The values of `R` are between -1 and 1, inclusive. |
| 2923 | |
| 2924 | Parameters |
| 2925 | ---------- |
| 2926 | x : array_like |
| 2927 | A 1-D or 2-D array containing multiple variables and observations. |
| 2928 | Each row of `x` represents a variable, and each column a single |
| 2929 | observation of all those variables. Also see `rowvar` below. |
| 2930 | y : array_like, optional |
| 2931 | An additional set of variables and observations. `y` has the same |
| 2932 | shape as `x`. |
| 2933 | rowvar : bool, optional |
| 2934 | If `rowvar` is True (default), then each row represents a |
| 2935 | variable, with observations in the columns. Otherwise, the relationship |
| 2936 | is transposed: each column represents a variable, while the rows |
| 2937 | contain observations. |
| 2938 | |
| 2939 | dtype : data-type, optional |
| 2940 | Data-type of the result. By default, the return data-type will have |
| 2941 | at least `numpy.float64` precision. |
| 2942 | |
| 2943 | .. versionadded:: 1.20 |
| 2944 | |
| 2945 | Returns |
| 2946 | ------- |
| 2947 | R : ndarray |
| 2948 | The correlation coefficient matrix of the variables. |
| 2949 | |
| 2950 | See Also |
| 2951 | -------- |
| 2952 | cov : Covariance matrix |
| 2953 | |
| 2954 | Notes |
| 2955 | ----- |
| 2956 | Due to floating point rounding the resulting array may not be Hermitian, |
| 2957 | the diagonal elements may not be 1, and the elements may not satisfy the |
| 2958 | inequality abs(a) <= 1. The real and imaginary parts are clipped to the |
| 2959 | interval [-1, 1] in an attempt to improve on that situation but is not |
| 2960 | much help in the complex case. |
| 2961 | |
| 2962 | Examples |
| 2963 | -------- |
| 2964 | >>> import numpy as np |
| 2965 | |
| 2966 | In this example we generate two random arrays, ``xarr`` and ``yarr``, and |
| 2967 | compute the row-wise and column-wise Pearson correlation coefficients, |
| 2968 | ``R``. Since ``rowvar`` is true by default, we first find the row-wise |
searching dependent graphs…