Ask Your Question
1

How can I reduce vector dimension using PCA?

asked May 23 '13

Nihad gravatar image

updated May 23 '13

I am trying to reduced vector size usig PCA. I am dealing with 907 objects(row), and each object has 300 feature vector(col.). I want to reduce feature vector size 50% which means resultant feature vector would be 150(col.) I use following code segment, but it retains all col. How can I reduce using PCA? Thnx in advance.

code:

int main(..)
{
.......Read read_feature two dimensional array////////////////
number_of_lines=907;
feature_vector_size=300;
Mat input_feature_vector(number_of_lines,feature_vector_size,CV_32F);

    for(i=0;i<number_of_lines;i++)
    {
        for(j=0;j<feature_vector_size;j++)
        {
            input_feature_vector.at<float>(i,j)=read_feature[i][j];

        }

    }

    cout<<input_feature_vector<<endl;

    Mat projection_result;

    PCA pca(input_feature_vector,Mat(),CV_PCA_DATA_AS_ROW, 0); 

    /*cout<<"PCA Mean:"<<endl;
    cout<<pca.mean<<endl;*/

    pca.project(input_feature_vector,projection_result);

    cout<<"PCA Projection Result:"<<endl;
    cout<<projection_result<<endl;
}
Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered May 23 '13

Guanta gravatar image

you need to specify the maxComponents that PCA should retain, i.e. change

PCA pca(input_feature_vector,Mat(),CV_PCA_DATA_AS_ROW, 0);

to

PCA pca(input_feature_vector,Mat(),CV_PCA_DATA_AS_ROW, 150);
Preview: (hide)

Comments

thank you. it works fine.

Nihad gravatar imageNihad (May 23 '13)edit

Question Tools

Stats

Asked: May 23 '13

Seen: 2,982 times

Last updated: May 23 '13