-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UDTs for Vectors in MLlib, plus DatasetExample using the UDTs
- Loading branch information
Showing
6 changed files
with
242 additions
and
12 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
examples/src/main/scala/org/apache/spark/examples/mllib/DatasetExample.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,101 @@ | ||
/* | ||
* 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 scopt.OptionParser | ||
|
||
import org.apache.spark.{SparkConf, SparkContext} | ||
import org.apache.spark.mllib.linalg.Vector | ||
import org.apache.spark.mllib.regression.LabeledPoint | ||
import org.apache.spark.mllib.stat.MultivariateOnlineSummarizer | ||
import org.apache.spark.mllib.util.MLUtils | ||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.sql.{Row, SQLContext, SchemaRDD} | ||
|
||
/** | ||
* An example of how to use [[org.apache.spark.sql.SchemaRDD]] as a Dataset for ML. Run with | ||
* {{{ | ||
* ./bin/run-example org.apache.spark.examples.mllib.Dataset [options] | ||
* }}} | ||
* If you use it as a template to create your own app, please use `spark-submit` to submit your app. | ||
*/ | ||
object DatasetExample { | ||
|
||
case class Params( | ||
input: String = "data/mllib/sample_libsvm_data.txt", | ||
dataFormat: String = "libsvm") extends AbstractParams[Params] | ||
|
||
def main(args: Array[String]) { | ||
val defaultParams = Params() | ||
|
||
val parser = new OptionParser[Params]("Dataset") { | ||
head("Dataset: an example app using SchemaRDD as a Dataset for ML.") | ||
opt[String]("input") | ||
.text(s"input path to dataset") | ||
.action((x, c) => c.copy(input = x)) | ||
opt[String]("dataFormat") | ||
.text("data format: libsvm (default), dense (deprecated in Spark v1.1)") | ||
.action((x, c) => c.copy(input = x)) | ||
checkConfig { params => | ||
success | ||
} | ||
} | ||
|
||
parser.parse(args, defaultParams).map { params => | ||
run(params) | ||
}.getOrElse { | ||
sys.exit(1) | ||
} | ||
} | ||
|
||
def run(params: Params) { | ||
|
||
val conf = new SparkConf().setAppName(s"Dataset with $params") | ||
val sc = new SparkContext(conf) | ||
val sqlContext = new SQLContext(sc) | ||
import sqlContext._ // for implicit conversions | ||
|
||
// Load input data | ||
val origData: RDD[LabeledPoint] = params.dataFormat match { | ||
case "dense" => MLUtils.loadLabeledPoints(sc, params.input) | ||
case "libsvm" => MLUtils.loadLibSVMFile(sc, params.input) | ||
} | ||
println(s"Loaded ${origData.count()} instances from file: ${params.input}") | ||
|
||
// Convert input data to SchemaRDD explicitly. | ||
val schemaRDD: SchemaRDD = origData | ||
println(s"Converted to SchemaRDD with ${schemaRDD.count()} records") | ||
|
||
// Select columns, using implicit conversion to SchemaRDD. | ||
val labelsSchemaRDD: SchemaRDD = origData.select('label) | ||
val labels: RDD[Double] = labelsSchemaRDD.map { case Row(v: Double) => v } | ||
val numLabels = labels.count() | ||
val meanLabel = labels.fold(0.0)(_ + _) / numLabels | ||
println(s"Selected label column with average value $meanLabel") | ||
|
||
val featuresSchemaRDD: SchemaRDD = origData.select('features) | ||
val features: RDD[Vector] = featuresSchemaRDD.map { case Row(v: Vector) => v } | ||
val featureSummary = features.aggregate(new MultivariateOnlineSummarizer())( | ||
(summary, feat) => summary.add(feat), | ||
(sum1, sum2) => sum1.merge(sum2)) | ||
println(s"Selected features column with average values:\n ${featureSummary.mean.toString}") | ||
|
||
sc.stop() | ||
} | ||
|
||
} |
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
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
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
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
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