PCA returns wrong eigenvector (Bug #4335)
Description
#include <iostream> #include "opencv2/core.hpp" #include "opencv2/ml.hpp" int main() { // ingredients matrix from hald data in MATLAB cv::Mat data = (cv::Mat_<float>(13, 4) <<\ 7, 26, 6, 60,\ 1, 29, 15, 52,\ 11, 56, 8, 20,\ 11, 31, 8, 47,\ 7, 52, 6, 33,\ 11, 55, 9, 22,\ 3, 71, 17, 6,\ 1, 31, 22, 44,\ 2, 54, 18, 22,\ 21, 47, 4, 26,\ 1, 40, 23, 34,\ 11, 66, 9, 12,\ 10, 68, 8, 12); cv::PCA pca(data, cv::Mat(), cv::PCA::DATA_AS_ROW); cv::Mat eigenvectors; cv::Mat mean; eigenvectors = pca.eigenvectors.t(); mean = pca.mean; // row vector std::cout << eigenvectors << std::endl; // std::cout << mean << std::endl; // std::cout << (data - cv::repeat(mean, 13, 1)) * eigenvectors.t() << std::endl; return 0; }
The correct eigenvector matrix is
-0.0678 -0.6460 0.5673 0.5062
-0.6785 -0.0200 -0.5440 0.4933
0.0290 0.7553 0.4036 0.5156
0.7309 -0.1085 -0.4684 0.4844
but pca returns
-0.0678 -0.6460 -0.5673 0.5062
-0.6785 -0.0200 0.5440 0.4933
0.0290 0.7553 -0.4036 0.5156
0.7309 -0.1085 0.4684 0.4844
The third column has opposite sign.
History
Updated by Vadim Pisarevsky almost 10 years ago
if v is an eigenvector of matrix A, then -v is also an eigenvector of matrix A.
- Status changed from New to Cancelled
Updated by Hyunjun Kim almost 10 years ago
Vadim Pisarevsky wrote:
if v is an eigenvector of matrix A, then -v is also an eigenvector of matrix A.
You are correct. I didn't notice that.