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()
)
2 | No.2 Revision |
"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()
)
3 | No.3 Revision |
"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()
)
4 | No.4 Revision |
"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();
5 | No.5 Revision |
"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();