Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

"is this correct ?"

yes, looks fine.

"And how would I now use prediction with PCA ? "

the very same way you treated your train data. you will have to keep the pca object around (at least the transposed eigenvectors and the mean) and project your test vector into pca space:

Mat testData(1, 23040, CV_32F);
Mat test_projected;
pca.project(testData, test_projected); // should result in a 512 x 1 Mat
int label = SVM.predict(test_projected);

(actually, all PCA::project() does is: result = (input - mean) * eigenvecs.t())

"is this correct ?"

yes, looks fine.

"And how would I now use prediction with PCA ? "

the very same way you treated your train data. you will have to keep the pca object around (at least the transposed eigenvectors and the mean) and project your test vector into pca space:

Mat testData(1, 23040, CV_32F);
Mat test_projected;
pca.project(testData, test_projected); // should result in a 512 x 1 Mat
int label = SVM.predict(test_projected);
// ^ btw, naming your instance "SVM" is a bad idea, since there is already a class with that name

(actually, all PCA::project() does is: result = (input - mean) * eigenvecs.t())

"is this correct ?"

yes, looks fine.

"And how would I now use prediction with PCA ? "

the very same way you treated your train data. you will have to keep the pca object around (at least the transposed eigenvectors and the mean) and project your test vector into pca space:

Mat testData(1, 23040, CV_32F);
Mat test_projected;
pca.project(testData, test_projected); // should result in a 512 x 1 Mat
int label = SVM.predict(test_projected);
// ^ btw, naming your instance "SVM" is a bad idea, since there is already a class with that name

(actually, all PCA::project() does is: result = (input - mean) * eigenvecs.t())

"is this correct ?"

yes, looks fine.

"And how would I now use prediction with PCA ? "

the very same way you treated your train data. you will have to keep the pca object around (at least the transposed eigenvectors and the mean) and project your test vector into pca space:

Mat testData(1, 23040, CV_32F);
Mat test_projected;
pca.project(testData, test_projected); // should result in a 512 x 1 Mat
int label = SVM.predict(test_projected);
// btw, naming your instance "SVM" is a bad idea, since there is already a class with that name

(actually, all PCA::project() does is: result = (input - mean) * eigenvecs.t())

[Edit:]

since you probably want to save the trained svm model, you need to save the PCA, too:

// to disk:
FileStorage fs("pca.xml", FileStorage::WRITE);
pca.write(fs);
fs.release(); // flush

// and back:
PCA pca;
FileStorage fs("pca.xml", FileStorage::READ);
pca.read(fs);
fs.release();

"is this correct ?"

yes, looks fine.

"And how would I now use prediction with PCA ? "

the very same way you treated your train data. you will have to keep the pca object around (at least the transposed eigenvectors and the mean) and project your test vector into pca space:

Mat testData(1, 23040, CV_32F);
Mat test_projected;
pca.project(testData, test_projected); // should result in a 512 x 1 Mat
int label = SVM.predict(test_projected);
// btw, naming your instance "SVM" is a bad idea, since there is already a class with that name

(actually, all PCA::project() does is: result = (input - mean) * eigenvecs.t())

[Edit:]

since you probably want to save the trained svm model, you need to save the PCA, too:

// to disk:
FileStorage fs("pca.xml", FileStorage::WRITE);
pca.write(fs);
fs.release(); // flush

// and back:
PCA pca;
FileStorage fs("pca.xml", FileStorage::READ);
pca.read(fs);
pca.read(fs.root());
fs.release();