Search code examples
c++opencvpca

Dimension reduction with PCA in OpenCV, wrong dimensions of eigenvectors


I am not sure if this problem is already on stackoverflow, but I could not find it so i decided to open a new question. I am trying to reduce the dimension of a feature Matrix. I have 58 features and 30 instances / measurements. I want to reduce the number of features to 40. But there seems to be a problem with my matrices' dimensions.

  • featureMatrix_cv is my feature matrix with 30 rows and 58 columns

    PCA pca_analysis(featureMatrix_cv, cv::Mat(), cv::PCA::DATA_AS_ROW, 40);
    
    cv::Mat neu = pca_analysis.project(featureMatrix_cv.row(0));
    
  • The first problem is, pca_analysis.eigenvectors has the wrong dimensions I think (30 rows and 58 columns). I read in several tutorial, that i should get N N-dimensional eigenvectors, where N is the number of features (here 58). Same problem for pca_analysis.eigenvalues (30 rows and 1 column), It should heave the size (58, 1).

  • In the second line, I tried to project the first instance into the new dimension space, but this isn't working because instead of 40 values, pca_analysis.project returns a matrix with 30 values. I read in a tutorial that the projected vector/matix should have 40 values, that is the dimension of the feature space.

Is there anybody that could help me or had a similar problem?


Solution

  • Okay, so after finding this thread, I know now what the problem was: I need more instances than features! If I have 58 features, I just need at least 58 samples. This is not a problem for me, because I have enough data, I was just using 30 samples all the time for testing.