2012-05-15 45 views
8

我需要在Java中實現PCA。我有興趣找到有據可查,實用且易於使用的東西。任何建議?Java中的PCA實現

回答

8

這裏是一個:PCA Class

該類包含具有varimax旋轉的基本主分量分析所必需的方法。選項可用於使用協方差或相關性矩陣進行分析。使用蒙特卡洛模擬進行平行分析。基於大於1的特徵值的提取標準,大於蒙特卡羅特徵值百分點或大於蒙特卡羅特徵值均值是可用的。

8

現在有一些Java的主成分分析實現。

  1. Apache的火花:https://spark.apache.org/docs/2.1.0/mllib-dimensionality-reduction.html#principal-component-analysis-pca

    SparkConf conf = new SparkConf().setAppName("PCAExample").setMaster("local"); 
    try (JavaSparkContext sc = new JavaSparkContext(conf)) { 
        //Create points as Spark Vectors 
        List<Vector> vectors = Arrays.asList(
          Vectors.dense(-1.0, -1.0), 
          Vectors.dense(-1.0, 1.0), 
          Vectors.dense(1.0, 1.0)); 
    
        //Create Spark MLLib RDD 
        JavaRDD<Vector> distData = sc.parallelize(vectors); 
        RDD<Vector> vectorRDD = distData.rdd(); 
    
        //Execute PCA Projection to 2 dimensions 
        PCA pca = new PCA(2); 
        PCAModel pcaModel = pca.fit(vectorRDD); 
        Matrix matrix = pcaModel.pc(); 
    } 
    
  2. ND4J:http://nd4j.org/doc/org/nd4j/linalg/dimensionalityreduction/PCA.html

    //Create points as NDArray instances 
    List<INDArray> ndArrays = Arrays.asList(
         new NDArray(new float [] {-1.0F, -1.0F}), 
         new NDArray(new float [] {-1.0F, 1.0F}), 
         new NDArray(new float [] {1.0F, 1.0F})); 
    
    //Create matrix of points (rows are observations; columns are features) 
    INDArray matrix = new NDArray(ndArrays, new int [] {3,2}); 
    
    //Execute PCA - again to 2 dimensions 
    INDArray factors = PCA.pca_factor(matrix, 2, false); 
    
  3. Apache的百科全書數學(單線程;無框架)

    //create points in a double array 
    double[][] pointsArray = new double[][] { 
        new double[] { -1.0, -1.0 }, 
        new double[] { -1.0, 1.0 }, 
        new double[] { 1.0, 1.0 } }; 
    
    //create real matrix 
    RealMatrix realMatrix = MatrixUtils.createRealMatrix(pointsArray); 
    
    //create covariance matrix of points, then find eigen vectors 
    //see https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues 
    
    Covariance covariance = new Covariance(realMatrix); 
    RealMatrix covarianceMatrix = covariance.getCovarianceMatrix(); 
    EigenDecomposition ed = new EigenDecomposition(covarianceMatrix); 
    

注意,奇異值分解也可用於查找主元件,具有等效的實現。