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.
Split RandomAndSampledRDDs into RandomRDDGeneration and SampledRDDs. (The name RandomRDDGeneration is to avoid a naming conflict with RandomRDDs.) RandomRDDGeneration prints first 5 samples Did same split for Python: random_rdd_generation.py and sampled_rdds.py Other small updates based on code review.
- Loading branch information
Showing
7 changed files
with
291 additions
and
116 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
""" | ||
Randomly generated RDDs. | ||
""" | ||
|
||
import sys | ||
|
||
from pyspark import SparkContext | ||
from pyspark.mllib.random import RandomRDDs | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) not in [1, 2]: | ||
print >> sys.stderr, "Usage: random_rdd_generation" | ||
exit(-1) | ||
|
||
sc = SparkContext(appName="PythonRandomRDDGeneration") | ||
|
||
numExamples = 10000 # number of examples to generate | ||
fraction = 0.1 # fraction of data to sample | ||
|
||
# Example: RandomRDDs.normalRDD | ||
normalRDD = RandomRDDs.normalRDD(sc, numExamples) | ||
print 'Generated RDD of %d examples sampled from the standard normal distribution'\ | ||
% normalRDD.count() | ||
print ' First 5 samples:' | ||
for sample in normalRDD.take(5): | ||
print ' ' + str(sample) | ||
|
||
# Example: RandomRDDs.normalVectorRDD | ||
normalVectorRDD = RandomRDDs.normalVectorRDD(sc, numRows = numExamples, numCols = 2) | ||
print 'Generated RDD of %d examples of length-2 vectors.' % normalVectorRDD.count() | ||
print ' First 5 samples:' | ||
for sample in normalVectorRDD.take(5): | ||
print ' ' + str(sample) | ||
|
||
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
92 changes: 92 additions & 0 deletions
92
examples/src/main/scala/org/apache/spark/examples/mllib/Correlations.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,92 @@ | ||
/* | ||
* 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.mllib.stat.Statistics | ||
import org.apache.spark.mllib.util.MLUtils | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
|
||
|
||
/** | ||
* An example app for summarizing multivariate data from a file. Run with | ||
* {{{ | ||
* bin/run-example org.apache.spark.examples.mllib.Correlations | ||
* }}} | ||
* By default, this loads a synthetic dataset from `data/mllib/sample_linear_regression_data.txt`. | ||
* If you use it as a template to create your own app, please use `spark-submit` to submit your app. | ||
*/ | ||
object Correlations { | ||
|
||
case class Params(input: String = "data/mllib/sample_linear_regression_data.txt") | ||
|
||
def main(args: Array[String]) { | ||
|
||
val defaultParams = Params() | ||
|
||
val parser = new OptionParser[Params]("Correlations") { | ||
head("Correlations: an example app for computing correlations") | ||
opt[String]("input") | ||
.text(s"Input path to labeled examples in LIBSVM format, default: ${defaultParams.input}") | ||
.action((x, c) => c.copy(input = x)) | ||
note( | ||
""" | ||
|For example, the following command runs this app on a synthetic dataset: | ||
| | ||
| bin/spark-submit --class org.apache.spark.examples.mllib.Correlations \ | ||
| examples/target/scala-*/spark-examples-*.jar \ | ||
| --input data/mllib/sample_linear_regression_data.txt | ||
""".stripMargin) | ||
} | ||
|
||
parser.parse(args, defaultParams).map { params => | ||
run(params) | ||
} getOrElse { | ||
sys.exit(1) | ||
} | ||
} | ||
|
||
def run(params: Params) { | ||
val conf = new SparkConf().setAppName(s"Correlations with $params") | ||
val sc = new SparkContext(conf) | ||
|
||
val examples = MLUtils.loadLibSVMFile(sc, params.input).cache() | ||
|
||
println(s"Summary of data file: ${params.input}") | ||
println(s"${examples.count()} data points") | ||
|
||
// Calculate label -- feature correlations | ||
val labelRDD = examples.map(_.label) | ||
val numFeatures = examples.take(1)(0).features.size | ||
val corrType = "pearson" | ||
println() | ||
println(s"Correlation ($corrType) between label and each feature") | ||
println(s"Feature\tCorrelation") | ||
var feature = 0 | ||
while (feature < numFeatures) { | ||
val featureRDD = examples.map(_.features(feature)) | ||
val corr = Statistics.corr(labelRDD, featureRDD) | ||
println(s"$feature\t$corr") | ||
feature += 1 | ||
} | ||
println() | ||
|
||
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
Oops, something went wrong.