From e9bd6cb51dce9222a5a284cd171b299b0169852b Mon Sep 17 00:00:00 2001 From: Reza Zadeh Date: Sat, 4 Jan 2014 12:33:22 -0800 Subject: new example file --- .../scala/org/apache/spark/examples/SparkSVD.scala | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/src/main/scala/org/apache/spark/examples/SparkSVD.scala (limited to 'examples') 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 ") + 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) + } +} -- cgit v1.2.3