寻找协方差矩阵的特征值

时间:2019-02-05 15:51:45

标签: python numpy scipy statistics

假设有一些双变量正态分布数据,我想找到其协方差矩阵的特征向量。我计算出的特征向量不适合数据,我的代码有问题吗?

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
import scipy.linalg as la

# Generate a positive-definite covariance matrix & generate data
A          = np.random.random([2,2])
cov_given  = np.dot(A,A.T)
mean_given = np.random.random([2])
data       = np.random.multivariate_normal(mean_given,cov_given,10000)

# Find the numerical mean and covariance, diagonalize
mean = np.mean(data, axis = 0 )
cov  = np.cov(data.T)
w, v = la.eig(cov)

# Plotting procedures
fig, ax = plt.subplots()
xmin, ymin = np.min(data,axis=0)
xmax, ymax = np.max(data,axis=0)
x, y = np.mgrid[xmin:xmax:.01, ymin:ymax:.01]
pos  = np.dstack((x, y))
ax.contourf(x,y, multivariate_normal(mean=mean, cov=cov).pdf(pos))
ax.plot([mean[0], mean[0] + v[0][1]],[mean[1],mean[1] + v[1][1]])
ax.plot([mean[0], mean[0] + v[0][0]],[mean[1],mean[1] + v[1][0]])
ax.set_xlim(xmin,xmax)
ax.set_ylim(ymin,ymax)
plt.show()

data with bad covariance eigenvectors

首先我想,我混合了v的列和行,但事实证明,它不能更好地工作。你发现问题了吗?

1 个答案:

答案 0 :(得分:1)

没有问题。已正确找到向量(请使用v[:,0].dot(v[:, 1])进行检查,如果它们正交,则为0)。那是重要的检查。您可以通过使用

将纵横比设置为1来在图中看到这一点。
xmin, ymin = -3, -3
xmax, ymax = 3, 3