Search code examples
javawekapca

processing data using weka PCA


I would like to do PCA for my dataset using weka's PCA. I saw online the java code is:

  1. PrincipalComponents pca = new PrincipalComponents();
  2. pca.setMaximumAttributeNames(300);
  3. pca.setInputFormat(Data);
  4. Instances newData = Filter.useFilter(Data, pca);

I import both import weka.attributeSelection.PrincipalComponents and import weka.filters.Filter in the code, but eclipse always shows that in the third line, pca need to be cast to Filter. After I cast pca to Filter, it is still wrong. The fourth line is also showed wrong. I would like to know what is the right code to do pca for an existing dataset?

Thanks!


Solution

  • The problem you're running into is that there are two classes called PrincipalComponents in the weka API:

    weka.attributeSelection.PrincipalComponents and weka.filters.unsupervised.attribute.PrincipalComponents.

    The latter is a kind of Filter, but you are importing the former, which is not.

    Just change your import statement and your code should work.