From f672e454592eea7deedc02b9185269e8988359c3 Mon Sep 17 00:00:00 2001 From: Davis King Date: Fri, 12 Dec 2008 00:31:47 +0000 Subject: [PATCH] Cleaned up the PCA code slightly. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402724 --- dlib/statistics/statistics.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dlib/statistics/statistics.h b/dlib/statistics/statistics.h index e934bd3ed..2f8666ea0 100644 --- a/dlib/statistics/statistics.h +++ b/dlib/statistics/statistics.h @@ -188,6 +188,11 @@ namespace dlib const double eps = 0.99 ) { + // You are getting an error here because you are trying to apply PCA + // to a vector of fixed length. But PCA is going to try and do + // dimensionality reduction so you can't use a vector with a fixed dimension. + COMPILE_TIME_ASSERT(matrix_type::NR == 0); + // make sure requires clause is not broken DLIB_ASSERT(samples.size() > 0, "\tvoid vector_normalizer::train_pca()" @@ -327,13 +332,9 @@ namespace dlib total += eigenvalues(r); } - // so now we know we want to use num_vectors of the first eigenvectors. - temp.set_size(num_vectors, eigen.nr()); - for (long i = 0; i < num_vectors; ++i) - { - set_rowm(temp,i) = trans(colm(pca,i)); - } - temp.swap(pca); + // So now we know we want to use num_vectors of the first eigenvectors. So + // pull those out and discard the rest. + pca = trans(colm(pca,range(0,num_vectors-1))); // Apply the pca transform to the data in x. Then we will normalize the // pca matrix below.