aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorReza Zadeh <rizlar@gmail.com>2014-01-04 12:33:22 -0800
committerReza Zadeh <rizlar@gmail.com>2014-01-04 12:33:22 -0800
commite9bd6cb51dce9222a5a284cd171b299b0169852b (patch)
tree309af9f159271e6e04f979f8007f90b3c4ff6450 /examples
parent8bfcce1ad81348a5eac3e3d332ddc293380c041a (diff)
downloadspark-e9bd6cb51dce9222a5a284cd171b299b0169852b.tar.gz
spark-e9bd6cb51dce9222a5a284cd171b299b0169852b.tar.bz2
spark-e9bd6cb51dce9222a5a284cd171b299b0169852b.zip
new example file
Diffstat (limited to 'examples')
-rw-r--r--examples/src/main/scala/org/apache/spark/examples/SparkSVD.scala58
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/src/main/scala/org/apache/spark/examples/SparkSVD.scala b/examples/src/main/scala/org/apache/spark/examples/SparkSVD.scala
new file mode 100644
index 0000000000..5590ee728a
--- /dev/null
+++ b/examples/src/main/scala/org/apache/spark/examples/SparkSVD.scala
@@ -0,0 +1,58 @@
+/*
+ * 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
+
+import org.apache.spark.SparkContext
+import org.apache.spark.mllib.linalg.SVD
+import org.apache.spark.mllib.linalg.MatrixEntry
+
+/**
+ * Compute SVD of an example matrix
+ * Input file should be comma separated, 1 indexed of the form
+ * i,j,value
+ * Where i is the column, j the row, and value is the matrix entry
+ *
+ * For example input file, see:
+ * mllib/data/als/test.data
+ */
+object SparkSVD {
+ def main(args: Array[String]) {
+ if (args.length < 3) {
+ System.err.println("Usage: SVD <master> <file>")
+ System.exit(1)
+ }
+ val sc = new SparkContext(args(0), "SVD",
+ System.getenv("SPARK_HOME"), Seq(System.getenv("SPARK_EXAMPLES_JAR")))
+
+ // Load and parse the data file
+ val data = sc.textFile(args(1)).map { line =>
+ val parts = line.split(',')
+ MatrixEntry(parts(0).toInt, parts(1).toInt, parts(2).toDouble)
+ }
+ val m = 4
+ val n = 4
+
+ // recover largest singular vector
+ val decomposed = SVD.sparseSVD(data, m, n, 1)
+ val u = decomposed.U
+ val s = decomposed.S
+ val v = decomposed.V
+
+ println("singular values = " + s.toArray.mkString)
+ }
+}