我写了一个返回给定矩阵的协方差矩阵的方法,我认为我写的代码缺少某种规范化,因为我返回的矩阵正好是我从 np.cov(( 获得的矩阵的常数倍
import numpy as np
def calculate_covariance(matrix: np.array):
mean_rows = np.zeros(matrix.shape[1])
for rows in matrix:
mean_rows += rows
mean_rows /= matrix.shape[0]
mean_matrix = []
for _ in range(matrix.shape[0]):
mean_matrix.append(mean_rows)
mean_matrix = np.array(mean_matrix)
subtracted_matrix = matrix - mean_matrix
cov_matrix = np.dot(subtracted_matrix.T, subtracted_matrix)
return cov_matrix
事实上,如果您阅读文档,您会发现默认情况下np.cov
除以N-1
.
此外,通过让 numpy 计算行意味着,可以大大改进您的代码:
def cov(mat):
# covariance of the *rows* of the given matrix
rowmeans = mat.mean(axis=1, keepdims=True)
norm = mat - rowmeans
return norm.dot(norm.T) / (mat.shape[1] - 1)