GitHub user catap opened a pull request:

    https://github.com/apache/spark/pull/4304

    PCA wrapper for easy transform vectors

    I implement a simple PCA wrapper for easy transform of vectors by PCA for 
example LabeledPoint or another complicated structure.
    
    Example of usage:
    ```
      import org.apache.spark.mllib.regression.LinearRegressionWithSGD
      import org.apache.spark.mllib.regression.LabeledPoint
      import org.apache.spark.mllib.linalg.Vectors
      import org.apache.spark.mllib.feature.PCA
    
      val data = sc.textFile("data/mllib/ridge-data/lpsa.data").map { line =>
        val parts = line.split(',')
        LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' 
').map(_.toDouble)))
      }.cache()
    
      val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
      val training = splits(0).cache()
      val test = splits(1)
    
      val pca = PCA.create(training.first().features.size/2, 
data.map(_.features))
      val training_pca = training.map(p => p.copy(features = 
pca.transform(p.features)))
      val test_pca = test.map(p => p.copy(features = pca.transform(p.features)))
    
      val numIterations = 100
      val model = LinearRegressionWithSGD.train(training, numIterations)
      val model_pca = LinearRegressionWithSGD.train(training_pca, numIterations)
    
      val valuesAndPreds = test.map { point =>
        val score = model.predict(point.features)
        (score, point.label)
      }
    
      val valuesAndPreds_pca = test_pca.map { point =>
        val score = model_pca.predict(point.features)
        (score, point.label)
      }
    
      val MSE = valuesAndPreds.map{case(v, p) => math.pow((v - p), 2)}.mean()
      val MSE_pca = valuesAndPreds_pca.map{case(v, p) => math.pow((v - p), 
2)}.mean()
    
      println("Mean Squared Error = " + MSE)
      println("PCA Mean Squared Error = " + MSE_pca)
    ```

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/catap/spark pca

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/4304.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #4304
    
----
commit c71af4ad718be60e231bb10e39211f1acb1b04ab
Author: Kirill A. Korinskiy <[email protected]>
Date:   2015-02-02T04:24:52Z

    PCA wrapper for easy transform vectors
    
    I implement a simple PCA wrapper for easy transform of vectors by PCA for 
example LabeledPoint or another complicated structure.
    
    Example of usage:
    ```
      import org.apache.spark.mllib.regression.LinearRegressionWithSGD
      import org.apache.spark.mllib.regression.LabeledPoint
      import org.apache.spark.mllib.linalg.Vectors
      import org.apache.spark.mllib.feature.PCA
    
      val data = sc.textFile("data/mllib/ridge-data/lpsa.data").map { line =>
        val parts = line.split(',')
        LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' 
').map(_.toDouble)))
      }.cache()
    
      val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
      val training = splits(0).cache()
      val test = splits(1)
    
      val pca = PCA.create(training.first().features.size/2, 
data.map(_.features))
      val training_pca = training.map(p => p.copy(features = 
pca.transform(p.features)))
      val test_pca = test.map(p => p.copy(features = pca.transform(p.features)))
    
      val numIterations = 100
      val model = LinearRegressionWithSGD.train(training, numIterations)
      val model_pca = LinearRegressionWithSGD.train(training_pca, numIterations)
    
      val valuesAndPreds = test.map { point =>
        val score = model.predict(point.features)
        (score, point.label)
      }
    
      val valuesAndPreds_pca = test_pca.map { point =>
        val score = model_pca.predict(point.features)
        (score, point.label)
      }
    
      val MSE = valuesAndPreds.map{case(v, p) => math.pow((v - p), 2)}.mean()
      val MSE_pca = valuesAndPreds_pca.map{case(v, p) => math.pow((v - p), 
2)}.mean()
    
      println("Mean Squared Error = " + MSE)
      println("PCA Mean Squared Error = " + MSE_pca)
    ```

----


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to