Github user ScrapCodes commented on a diff in the pull request:
https://github.com/apache/spark/pull/1783#discussion_r15801518
--- Diff: python/pyspark/rdd.py ---
@@ -901,6 +902,97 @@ def sampleVariance(self):
1.0
"""
return self.stats().sampleVariance()
+
+ def histogram(self, buckets=None, evenBuckets=False, bucketCount=None):
+ """
+ Compute a histogram using the provided buckets or bucketCount. The
+ buckets are all open to the left except for the last which is
closed
+ e.g. for the array [1, 10, 20, 50], the buckets are [1, 10), [10,
20),
+ [20, 50] i.e. 1<=x<10, 10<=x<20, 20<=x<=50. And on the input of 1
and 50
+ we would have a histogram of 1, 0, 1.
+
+ If bucketCount is supplied, evenly-spaced buckets are
automatically
+ constructed using the minimum and maximum of the RDD. For example
if the
+ min value is 0 and the max is 100 and there are two buckets the
resulting
+ buckets will be [0, 50) [50, 100]. bucketCount must be at least 1.
+ Exactly one of buckets and bucketCount must be provided.
+
+ Note: if your histogram is evenly spaced (e.g. [0, 10, 20, 30])
this can
+ be switched from an O(log n) computation to O(1) per element
(where n is
+ the number of buckets) if you set evenBuckets to true.
+ buckets must be sorted and not contain any duplicates.
+ buckets array must be at least two elements
+
+ >>> a = sc.parallelize(range(100))
+ >>> a.histogram(bucketCount=2)
+ ([0.0, 49.5, 99.0], [50, 50])
+ >>> a.histogram(3)
+ ([0.0, 33.0, 66.0, 99.0], [33, 33, 34])
+ >>> a.histogram([0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
+ ([0, 10, 20, 30, 40, 50, 60, 70, 80, 90], [10, 10, 10, 10, 10, 10,
10, 10, 11])
+ >>> a.histogram([0, 10, 20, 30, 40, 50, 60, 70, 80, 90],
evenBuckets=True)
+ ([0, 10, 20, 30, 40, 50, 60, 70, 80, 90], [10, 10, 10, 10, 10, 10,
10, 10, 11])
+ >>> a.histogram([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 99])
+ ([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 99], [10, 10, 10, 10, 10,
10, 10, 10, 10, 10])
+
+ """
+
+ if (buckets and bucketCount) or (not buckets and not bucketCount):
+ raise ValueError("Pass either buckets or bucketCount but not
both")
+
+ if bucketCount and bucketCount <= 0:
+ raise ValueError("bucketCount must be positive")
+
+ if type(buckets) == int: #Treat int argument as bucketCount, not
buckets
+ bucketCount = buckets
+
+ def getBuckets():
+ mm_stats = self.stats()
+ min = mm_stats.min()
+ max = mm_stats.max()
+ increment = (max - min) * 1.0 / bucketCount
+ if increment != 0:
+ buckets = [round(min+x*increment, 2) for x in
range(bucketCount+1)]
--- End diff --
And please add a test case for this too.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]