Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/88#discussion_r10738948
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/PCA.scala ---
    @@ -0,0 +1,129 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.linalg
    +
    +import org.apache.spark.SparkContext
    +import org.apache.spark.SparkContext._
    +import org.apache.spark.rdd.RDD
    +
    +import org.apache.spark.mllib.util._
    +
    +import org.jblas.{DoubleMatrix, Singular, MatrixFunctions}
    +
    +
    +/**
    + * Class used to obtain principal components
    + */
    +class PCA {
    +  private var k: Int = 1
    +
    +  /**
    +   * Set the number of top-k principle components to return
    +   */
    +  def setK(k: Int): PCA = {
    +    this.k = k
    +    this
    +  }
    +
    +   /**
    +    * Compute PCA using the current set parameters
    +    */
    +  def compute(matrix: TallSkinnyDenseMatrix): Array[Array[Double]] = {
    +    computePCA(matrix, k)
    +  }
    +
    +  /**
    +   * Compute PCA using the parameters currently set
    +   * See computePCA() for more details
    +   */
    +  def compute(matrix: RDD[Array[Double]]): Array[Array[Double]] = {
    +    computePCA(matrix, k)
    +  }
    +
    + /**
    +  * Principal Component Analysis.
    +  * Computes the top k principal component coefficients for the m-by-n 
data matrix X.
    +  * Rows of X correspond to observations and columns correspond to 
variables. 
    +  * The coefficient matrix is n-by-k. Each column of coeff contains 
coefficients
    +  * for one principal component, and the columns are in descending 
    +  * order of component variance.
    +  * This function centers the data and uses the 
    +  * singular value decomposition (SVD) algorithm. 
    +  *
    +  * @param matrix dense matrix to perform pca on
    +  * @param k Recover k principal components
    +  * @return An nxk matrix with principal components in columns
    +  */
    +  private def computePCA(matrix: TallSkinnyDenseMatrix, k: Int): 
Array[Array[Double]] = {
    +    val m = matrix.m
    +    val n = matrix.n
    +    val sc = matrix.rows.sparkContext
    +
    +    if (m <= 0 || n <= 0) {
    +      throw new IllegalArgumentException(
    +                "Expecting a well-formed matrix: m=" + m + " n=" + n)
    +    }
    +
    +    computePCA(matrix.rows.map(_.data), k)
    +  }
    +
    +  /**
    +  * Principal Component Analysis.
    +  * Computes the top k principal component coefficients for the m-by-n 
data matrix X.
    +  * Rows of X correspond to observations and columns correspond to 
variables. 
    +  * The coefficient matrix is n-by-k. Each column of coeff contains 
coefficients
    +  * for one principal component, and the columns are in descending 
    +  * order of component variance.
    +  * This function centers the data and uses the 
    +  * singular value decomposition (SVD) algorithm. 
    +  *
    +  * @param matrix dense matrix to perform pca on
    +  * @param k Recover k principal components
    +  * @return An nxk matrix of principal components
    +  */
    +  private def computePCA(matrix: RDD[Array[Double]], k: Int): 
Array[Array[Double]] = {
    +    val n = matrix.first.size
    +    val sc = matrix.sparkContext
    +
    +    // compute column sums and normalize matrix
    +    val colSumsTemp = matrix.map(x => (x, 1))
    --- End diff --
    
    The way this code block is indented is a bit hard to read. You can move the 
fold to the same line as the map to improve readability slightly
    ```scala
        // Compute column sums and normalize matrix
        val colSumsTemp = matrix.map(x => (x, 1)).fold((Array.ofDim[Double](n), 
0)) { (a, b) => 
          val am = new DoubleMatrix(a._1)
          val bm = new DoubleMatrix(b._1)
          am.addi(bm)
          (a._1, a._2 + b._2)
        }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to