均值,方差,协方差的运算公式
举例说明
代码演示
import numpy as np
a = np.mat([[10, 15, 29], [15, 46, 13], [23, 21, 30], [11, 9, 35]])
a_mean = a.mean(axis = 0) # 求每列的均值
a_d_mean = a - a_mean # 去均值的矩阵
cov_x_y = np.dot(a_d_mean[:, 0].T, a_d_mean[:, 1]) / 3 # 26.58333333
cov_x_z = np.dot(a_d_mean[:, 0].T, a_d_mean[:, 2]) / 3 # -6.08333333
cov_y_z = np.dot(a_d_mean[:, 1].T, a_d_mean[:, 2]) / 3 # -152.08333333
a_cov = np.cov(a.T) # np 自带的矩阵计算公式 记住一定要转置
a_cov2 = np.dot(a_d_mean.T, a_d_mean) / (4 - 1) # 协方差矩阵还可以这样计算,先让样本矩阵中心化,即每一维度减去该维度的均值,使每一维度上的均值为0,然后直接用新的到的样本矩阵乘上它的转置,然后除以(N-1)即可
a_cov == a_cov2 # 结果相等
来源:CSDN
作者:artscrafts
链接:https://blog.csdn.net/ArtsCrafts/article/details/51692359