Skip to content

Commit

Permalink
rename SparkPCA/SVD to TallSkinnyPCA/SVD
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Apr 6, 2014
1 parent e7d0d4a commit b881506
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 105 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.examples.mllib

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.linalg.rdd.RowRDDMatrix
import org.apache.spark.mllib.linalg.Vectors

/**
* Compute the principal components of a tall-and-skinny matrix, whose rows are observations.
*
* The input matrix must be stored in row-oriented dense format, one line per row with its entries
* separated by space. For example,
* {{{
* 0 1
* 2 3
* 4 5
* }}}
* represents a 3-by-2 matrix, whose first row is (0, 1).
*/
object TallSkinnyPCA {
def main(args: Array[String]) {
if (args.length != 2) {
System.err.println("Usage: TallSkinnyPCA <master> <file>")
System.exit(1)
}

val conf = new SparkConf()
.setMaster(args(0))
.setAppName("TallSkinnyPCA")
.setSparkHome(System.getenv("SPARK_HOME"))
.setJars(SparkContext.jarOfClass(this.getClass))
val sc = new SparkContext(conf)

// Load and parse the data file.
val rows = sc.textFile(args(1)).map { line =>
val values = line.split(' ').map(_.toDouble)
Vectors.dense(values)
}
val mat = new RowRDDMatrix(rows)

// Compute principal components.
val pc = mat.computePrincipalComponents(mat.numCols().toInt)

println("Principal components are:\n" + pc)

sc.stop()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.examples.mllib

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.linalg.rdd.RowRDDMatrix
import org.apache.spark.mllib.linalg.Vectors

/**
* Compute the singular value decomposition (SVD) of a tall-and-skinny matrix.
*
* The input matrix must be stored in row-oriented dense format, one line per row with its entries
* separated by space. For example,
* {{{
* 0 1
* 2 3
* 4 5
* }}}
* represents a 3-by-2 matrix, whose first row is (0, 1).
*/
object TallSkinnySVD {
def main(args: Array[String]) {
if (args.length != 2) {
System.err.println("Usage: TallSkinnySVD <master> <file>")
System.exit(1)
}

val conf = new SparkConf()
.setMaster(args(0))
.setAppName("TallSkinnySVD")
.setSparkHome(System.getenv("SPARK_HOME"))
.setJars(SparkContext.jarOfClass(this.getClass))
val sc = new SparkContext(conf)

// Load and parse the data file.
val rows = sc.textFile(args(1)).map { line =>
val values = line.split(' ').map(_.toDouble)
Vectors.dense(values)
}
val mat = new RowRDDMatrix(rows)

// Compute SVD.
val svd = mat.computeSVD(mat.numCols().toInt)

println("Singular values are " + svd.s)

sc.stop()
}
}

0 comments on commit b881506

Please sign in to comment.