forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename SparkPCA/SVD to TallSkinnyPCA/SVD
- Loading branch information
Showing
4 changed files
with
128 additions
and
105 deletions.
There are no files selected for viewing
48 changes: 0 additions & 48 deletions
48
examples/src/main/scala/org/apache/spark/examples/mllib/SparkPCA.scala
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
examples/src/main/scala/org/apache/spark/examples/mllib/SparkSVD.scala
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
examples/src/main/scala/org/apache/spark/examples/mllib/TallSkinnyPCA.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
examples/src/main/scala/org/apache/spark/examples/mllib/TallSkinnySVD.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |