这个问题可能很愚蠢,但我找不到对此的解释。
我正在从头开始编写多元概率密度函数(用于研究目的),我需要计算的其中一件事是数据的协方差矩阵。我正在使用 Iris 数据集(150 个样本,4 个特征),当我编码时:
cov_matrix = np.cov(X)
print(cov_matrix.shape) // (150,150)
我不明白为什么它返回一个 150x150 矩阵,这是一个“逐元素协方差矩阵”吗?它不应该是一个 4x4 的协方差矩阵吗?
提前致谢。
最佳答案
默认 Numpy assumes变量在行中,而观察值在列中:
rowvar : bool, optional
If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.
关于python - 无法正确计算 np.cov(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65494140/