哪些 PCA 结果是正确的?

问题描述 投票:0回答:1

我的目标是找到我的数据中哪些方向“变化很大”。为此,我了解一种称为 PCA 的方法,该方法使用协变矩阵的特征向量来查找它们。

我使用了不同的实现来计算主成分分析。

matrix = np.array([
    [-0.5, 0.5],
    [1.5, 1.5]
])
covariance_matrix = np.cov(matrix[:,0], matrix[:,1])
eigen_values, eigen_vectors = np.linalg.eig(covariance_matrix)

print("eigen_vectors: ", eigen_vectors)
print("Eigen values: ", eigen_values)

输出是

eigen_vectors:  [[ 0.89442719 -0.4472136 ]
 [ 0.4472136   0.89442719]]
Eigen values:  [2.5 0. ]

我还使用了javascript库来计算它:

const output = PCA.getEigenVectors([
    [-0.5, 0.5],
    [1.5, 1.5]
])
console.log(output)

哪个输出是

[
  {
    eigenvalue: 1.2499999999999998,
    vector: [ 0.8944271909999157, 0.4472135954999579 ]
  },
  {
    eigenvalue: 0,
    vector: [ 0.4472135954999579, -0.8944271909999159 ]
  }
]

虽然我期望由于内部语言实现而得到不同的小数,但我还有其他符号和特征值。

为什么会出现这种情况?哪些结果对于我的案例来说是正确的(或更准确),为什么? 或者这两个结果都有效,我可以在我的案例中使用它们吗?

matrix pca algebra eigenvalue eigenvector
1个回答
0
投票
  • 该 JavaScript 库可能对协方差矩阵使用不同的标准化。
  • 如果必须使用 JS,可以使用
    ml-pca
    来实现 JS。否则,我就使用 python。

JS

const P = require("ml-pca").PCA;

const data = [
  [-0.5, 0.5],
  [1.5, 1.5],
];

const pca = new P(data);
console.log(pca.getEigenvalues(), pca.getEigenvectors());

打印

[ 2.5000000000000004, 0 ] Matrix {
  [
     0.89443  0.44721
     0.44721 -0.89443
  ]
  rows: 2
  columns: 2
}

Python


import numpy as np

matrix = np.array([
    [-0.5, 0.5],
    [1.5, 1.5]
])
covariance_matrix = np.cov(matrix[:, 0], matrix[:, 1])
eigen_values, eigen_vectors = np.linalg.eig(covariance_matrix)

# eigen_values /= 2

print("eigen_vectors: ", eigen_vectors)
print("Eigen values: ", eigen_values)
# help(np.linalg.eig)
# help(np.cov)

打印

eigen_vectors:  [[ 0.89442719 -0.4472136 ]
 [ 0.4472136   0.89442719]]
Eigen values:  [2.5 0. ]

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.