aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/python/mllib/stratified_sampling_example.py
diff options
context:
space:
mode:
authorXin Ren <iamshrek@126.com>2016-03-21 16:09:34 -0700
committerXiangrui Meng <meng@databricks.com>2016-03-21 16:09:34 -0700
commit1af8de200c4d3357bcb09e7bbc6deece00e885f2 (patch)
tree7201a72a2110684f77760fa601b07c20cc8ef961 /examples/src/main/python/mllib/stratified_sampling_example.py
parentf3717fc7c97ea402c9ddf9020405070421eeb4a4 (diff)
downloadspark-1af8de200c4d3357bcb09e7bbc6deece00e885f2.tar.gz
spark-1af8de200c4d3357bcb09e7bbc6deece00e885f2.tar.bz2
spark-1af8de200c4d3357bcb09e7bbc6deece00e885f2.zip
[SPARK-13019][DOCS] Replace example code in mllib-statistics.md using include_example
https://issues.apache.org/jira/browse/SPARK-13019 The example code in the user guide is embedded in the markdown and hence it is not easy to test. It would be nice to automatically test them. This JIRA is to discuss options to automate example code testing and see what we can do in Spark 1.6. Goal is to move actual example code to spark/examples and test compilation in Jenkins builds. Then in the markdown, we can reference part of the code to show in the user guide. This requires adding a Jekyll tag that is similar to https://github.com/jekyll/jekyll/blob/master/lib/jekyll/tags/include.rb, e.g., called include_example. `{% include_example scala/org/apache/spark/examples/mllib/SummaryStatisticsExample.scala %}` Jekyll will find `examples/src/main/scala/org/apache/spark/examples/mllib/SummaryStatisticsExample.scala` and pick code blocks marked "example" and replace code block in `{% highlight %}` in the markdown. See more sub-tasks in parent ticket: https://issues.apache.org/jira/browse/SPARK-11337 Author: Xin Ren <iamshrek@126.com> Closes #11108 from keypointt/SPARK-13019.
Diffstat (limited to 'examples/src/main/python/mllib/stratified_sampling_example.py')
-rw-r--r--examples/src/main/python/mllib/stratified_sampling_example.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/src/main/python/mllib/stratified_sampling_example.py b/examples/src/main/python/mllib/stratified_sampling_example.py
new file mode 100644
index 0000000000..a13f8f08dd
--- /dev/null
+++ b/examples/src/main/python/mllib/stratified_sampling_example.py
@@ -0,0 +1,38 @@
+#
+# 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.
+#
+
+from __future__ import print_function
+
+from pyspark import SparkContext
+
+if __name__ == "__main__":
+ sc = SparkContext(appName="StratifiedSamplingExample") # SparkContext
+
+ # $example on$
+ # an RDD of any key value pairs
+ data = sc.parallelize([(1, 'a'), (1, 'b'), (2, 'c'), (2, 'd'), (2, 'e'), (3, 'f')])
+
+ # specify the exact fraction desired from each key as a dictionary
+ fractions = {1: 0.1, 2: 0.6, 3: 0.3}
+
+ approxSample = data.sampleByKey(False, fractions)
+ # $example off$
+
+ for each in approxSample.collect():
+ print(each)
+
+ sc.stop()