forum.alglib.net

ALGLIB forum
It is currently Sun Feb 25, 2024 7:38 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Trouble using PCA...
PostPosted: Thu Aug 04, 2011 12:15 am 
Offline

Joined: Wed Aug 03, 2011 11:25 pm
Posts: 2
Hi all,

can someone help me with using pcabuildbasis?
I'm wondering why the following code doesn't reproduce the original matrix "faces":

Code:
  //matrix faces: a column is a "face"-image represented in greyscale-pixels (rows=numFaces)

  pcabuildbasis(faces, numPixels, numFaces, info, coeffs, eigenFaces);

  // reproduce faces by scalar-multiplying coeffs with eigenFaces and adding mean:
  for (int face = 0; face<numFaces; ++face) {
    for (int pixel = 0; pixel<numPixels; ++pixel) {
      double reconstruct = 0.0;
      // reconstruct pixel using all basisfunctions
      for (int eigenface = 0; eigenface<numPixels; ++eigenface) {         
        reconstruct += eigenFaces[pixel][eigenface] * coeffs[eigenface];
      }
      reconstruct += means[face];
      matrix[pixel][face] = reconstruct;     
    }
  }
  // Why is matrix!=faces ?


The values stored in matrix are not even in the range of the values stored in faces.
What am I doing wrong?
Thanks for hints!
Matze


Top
 Profile  
 
 Post subject: Re: Trouble using PCA...
PostPosted: Sat Aug 06, 2011 6:31 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 900
Basis vectors built by pcabuildbasis() are orthonormal, i.e. elements of eigenFaces are always in [-1,+1]. These elements describe directions of spread of your dataset, but they do not contain offset/scaling information. For example, if you try to apply pcabuildbasis() to 1-dimensional dataset [1000,1001,1002] then you will get [+1] or [-1] as eigenvector. As you see - no offset information.

And you've misunderstood meaning of coeffs - this array is not array of coefficients. It is array of variances.

If you want to reproduce your matrix (just to make sure that basis was correctly built), your code should look like:

Code:
// here I assume that all elements were correctly initialized
pcabuildbasis(faces, numPixels, numFaces, info, coeffs, eigenFaces);
for (int face_to_reconstruct = 0; face_to_reconstruct<numFaces; ++face_to_reconstruct)
{
    for(pixel = 0; pixel<numPixels; ++pixel)
        reconstructed[pixel] = 0.0;
    for (int eigenface = 0; eigenface<numPixels; ++eigenface)
    {
        // coefficient before eigenface
        double coefficient = 0.0;

        // calculate coefficient
        for (int pixel = 0; pixel<numPixels; ++pixel)
            coefficient += eigenFaces[pixel][eigenface]*faces[face_to_reconstruct];

        // update reconstructed face
        for (int pixel = 0; pixel<numPixels; ++pixel)
            reconstructed[pixel] += eigenFaces[pixel][eigenface]*coefficient;
    }
}


I may have some errors because I've never actually executed this code, but I hope that idea should be clear.


Top
 Profile  
 
 Post subject: Re: Trouble using PCA...
PostPosted: Sat Aug 06, 2011 9:14 pm 
Offline

Joined: Wed Aug 03, 2011 11:25 pm
Posts: 2
I see, I was totally wrong. Now it works, thanks for your support!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group