# forum.alglib.net

ALGLIB forum
 It is currently Sun Feb 25, 2024 8:08 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.

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Trouble using PCA...Posted: Thu Aug 04, 2011 12:15 am

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

 Post subject: Re: Trouble using PCA...Posted: Sat Aug 06, 2011 6:31 pm

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

 Post subject: Re: Trouble using PCA...Posted: Sat Aug 06, 2011 9:14 pm

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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Bing [Bot] and 8 guests

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

Search for: