计算两个给定 NumPy 数组的 pearson 积矩相关系数
在 NumPy 中,我们可以借助numpy.corrcoef()函数计算两个给定数组的皮尔逊积矩相关系数。
在这个函数中,我们将数组作为参数传递,它将返回两个给定数组的 pearson 积矩相关系数。
Syntax: numpy.corrcoef(x, y=None, rowvar=True, bias=, ddof=)
Return: Pearson product-moment correlation coefficients
让我们看一个例子:
示例 1:
Python
# import library
import numpy as np
# create numpy 1d-array
array1 = np.array([0, 1, 2])
array2 = np.array([3, 4, 5])
# pearson product-moment correlation
# coefficients of the arrays
rslt = np.corrcoef(array1, array2)
print(rslt)
Python
# import numpy library
import numpy as np
# create a numpy 1d-array
array1 = np.array([ 2, 4, 8])
array2 = np.array([ 3, 2,1])
# pearson product-moment correlation
# coefficients of the arrays
rslt2 = np.corrcoef(array1, array2)
print(rslt2)
输出
[[1. 1.]
[1. 1.]]
示例 2:
Python
# import numpy library
import numpy as np
# create a numpy 1d-array
array1 = np.array([ 2, 4, 8])
array2 = np.array([ 3, 2,1])
# pearson product-moment correlation
# coefficients of the arrays
rslt2 = np.corrcoef(array1, array2)
print(rslt2)
输出
[[ 1. -0.98198051]
[-0.98198051 1. ]]