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.
Added examples for statistical summarization:
* Scala: StatisticalSummary.scala ** Tests: correlation, MultivariateOnlineSummarizer * python: statistical_summary.py ** Tests: correlation (since MultivariateOnlineSummarizer has no Python API) Added sc.stop() to all examples. CorrelationSuite.scala * Added 1 test for RDDs with only 1 value Python SparseVector (pyspark/mllib/linalg.py) * Added toDense() function python/run-tests script * Added stat.py (doc test)
- Loading branch information
Showing
22 changed files
with
240 additions
and
8 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 |
---|---|---|
|
@@ -77,3 +77,5 @@ | |
output = cass_rdd.collect() | ||
for (k, v) in output: | ||
print (k, v) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,3 +71,5 @@ | |
output = hbase_rdd.collect() | ||
for (k, v) in output: | ||
print (k, v) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,3 +77,5 @@ def closestPoint(p, centers): | |
kPoints[x] = y | ||
|
||
print "Final centers: " + str(kPoints) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,5 @@ def add(x, y): | |
w -= points.map(lambda m: gradient(m, w)).reduce(add) | ||
|
||
print "Final w: " + str(w) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
""" | ||
Statistical summarization using MLlib. | ||
""" | ||
|
||
import sys | ||
|
||
from pyspark import SparkContext | ||
from pyspark.mllib.regression import LabeledPoint | ||
from pyspark.mllib.stat import Statistics | ||
from pyspark.mllib.util import MLUtils | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) not in [1,2]: | ||
print >> sys.stderr, "Usage: statistical_summary (<file>)" | ||
exit(-1) | ||
sc = SparkContext(appName="PythonStatisticalSummary") | ||
if len(sys.argv) == 2: | ||
filepath = sys.argv[1] | ||
else: | ||
filepath = 'data/mllib/sample_linear_regression_data.txt' | ||
corrType = 'pearson' | ||
|
||
points = MLUtils.loadLibSVMFile(sc, filepath)\ | ||
.map(lambda lp: LabeledPoint(lp.label, lp.features.toDense())) | ||
|
||
print '' | ||
print 'Summary of data file: ' + filepath | ||
print '%d data points' % points.count() | ||
|
||
# Statistics (correlations) | ||
print '' | ||
print 'Correlation (%s) between label and each feature' % corrType | ||
print 'Feature\tCorrelation' | ||
numFeatures = points.take(1)[0].features.size | ||
labelRDD = points.map(lambda lp: lp.label) | ||
for i in range(numFeatures): | ||
featureRDD = points.map(lambda lp: lp.features[i]) | ||
corr = Statistics.corr(labelRDD, featureRDD, corrType) | ||
print '%d\t%g' % (i, corr) | ||
print '' | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,5 @@ | |
output = sortedCount.collect() | ||
for (num, unitcount) in output: | ||
print num | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,5 @@ def generateGraph(): | |
break | ||
|
||
print "TC has %i edges" % tc.count() | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ | |
output = counts.collect() | ||
for (word, count) in output: | ||
print "%s: %i" % (word, count) | ||
|
||
sc.stop() |
121 changes: 121 additions & 0 deletions
121
examples/src/main/scala/org/apache/spark/examples/mllib/StatisticalSummary.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,121 @@ | ||
/* | ||
* 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.mllib.regression.LabeledPoint | ||
import org.apache.spark.rdd.RDD | ||
import scopt.OptionParser | ||
|
||
import org.apache.spark.mllib.linalg.Vectors | ||
import org.apache.spark.mllib.stat.{MultivariateOnlineSummarizer, 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.Statistics | ||
* }}} | ||
* 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 StatisticalSummary extends App { | ||
|
||
case class Params(input: String = "data/mllib/sample_linear_regression_data.txt") | ||
|
||
val defaultParams = Params() | ||
|
||
val parser = new OptionParser[Params]("StatisticalSummary") { | ||
head("StatisticalSummary: an example app for MultivariateOnlineSummarizer and Statistics (correlation)") | ||
opt[String]("input") | ||
.text(s"Input paths 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.StatisticalSummary \ | ||
| examples/target/scala-*/spark-examples-*.jar \ | ||
| data/mllib/sample_linear_regression_data.txt | ||
""".stripMargin) | ||
} | ||
|
||
parser.parse(args, defaultParams).map { params => | ||
run(params) | ||
} getOrElse { | ||
sys.exit(1) | ||
} | ||
|
||
def runStatisticalSummary(examples: RDD[LabeledPoint], params: Params) { | ||
// Summarize labels | ||
val labelSummary = examples.aggregate(new MultivariateOnlineSummarizer())( | ||
(summary, lp) => summary.add(Vectors.dense(lp.label)), | ||
(sum1, sum2) => sum1.merge(sum2)) | ||
|
||
// Summarize features | ||
val featureSummary = examples.aggregate(new MultivariateOnlineSummarizer())( | ||
(summary, lp) => summary.add(lp.features), | ||
(sum1, sum2) => sum1.merge(sum2)) | ||
|
||
println() | ||
println(s"Summary statistics") | ||
println(s"\tLabel\tFeatures") | ||
println(s"mean\t${labelSummary.mean(0)}\t${featureSummary.mean.toArray.mkString("\t")}") | ||
println(s"var\t${labelSummary.variance(0)}\t${featureSummary.variance.toArray.mkString("\t")}") | ||
println( | ||
s"nnz\t${labelSummary.numNonzeros(0)}\t${featureSummary.numNonzeros.toArray.mkString("\t")}") | ||
println(s"max\t${labelSummary.max(0)}\t${featureSummary.max.toArray.mkString("\t")}") | ||
println(s"min\t${labelSummary.min(0)}\t${featureSummary.min.toArray.mkString("\t")}") | ||
println() | ||
} | ||
|
||
def runCorrelations(examples: RDD[LabeledPoint], params: Params) { | ||
// 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() | ||
} | ||
|
||
def run(params: Params) { | ||
val conf = new SparkConf().setAppName(s"StatisticalSummary 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") | ||
|
||
runStatisticalSummary(examples, params) | ||
|
||
runCorrelations(examples, params) | ||
|
||
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