Github user nrchandan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1791#discussion_r15858417
  
    --- Diff: python/pyspark/rdd.py ---
    @@ -854,6 +884,97 @@ def redFunc(left_counter, right_counter):
     
             return self.mapPartitions(lambda i: 
[StatCounter(i)]).reduce(redFunc)
     
    +    def histogram(self, buckets, even=False):
    +        """
    +        Compute a histogram using the provided buckets. The buckets
    +        are all open to the right except for the last which is closed.
    +        e.g. [1,10,20,50] means the buckets are [1,10) [10,20) [20,50],
    +        which means 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 your histogram is evenly spaced (e.g. [0, 10, 20, 30]),
    +        this can be switched from an O(log n) inseration to O(1) per
    +        element(where n = # buckets), if you set `even` to True.
    +
    +        Buckets must be sorted and not contain any duplicates, must be
    +        at least two elements.
    +
    +        If `buckets` is a number, it will generates buckets which is
    +        evenly spaced between the minimum and maximum of the RDD. For
    +        example, if the min value is 0 and the max is 100, given buckets
    +        as 2, the resulting buckets will be [0,50) [50,100]. buckets must
    +        be at least 1 If the RDD contains infinity, NaN throws an exception
    +        If the elements in RDD do not vary (max == min) always returns
    +        a single bucket.
    +
    +        It will return an tuple of buckets and histogram.
    +
    +        >>> rdd = sc.parallelize(range(51))
    +        >>> rdd.histogram(2)
    +        ([0, 25, 50], [25, 26])
    +        >>> rdd.histogram([0, 5, 25, 50])
    +        ([0, 5, 25, 50], [5, 20, 26])
    +        >>> rdd.histogram([0, 15, 30, 45, 60], True)
    +        ([0, 15, 30, 45, 60], [15, 15, 15, 6])
    +        """
    +
    +        if isinstance(buckets, (int, long)):
    +            if buckets < 1:
    +                raise ValueError("buckets should not less than 1")
    +
    +            # faster than stats()
    +            def minmax(it):
    +                minv, maxv = float("inf"), float("-inf")
    +                for v in it:
    +                    minv = min(minv, v)
    +                    maxv = max(maxv, v)
    +                return [(minv, maxv)]
    +
    +            def _merge(a, b):
    +                return (min(a[0], b[0]), max(a[1], b[1]))
    +
    +            minv, maxv = self.mapPartitions(minmax).reduce(_merge)
    +
    +            if minv == maxv or buckets == 1:
    +                return [minv, maxv], [self.count()]
    +
    +            inc = (maxv - minv) / buckets
    +            # keep them as integer if possible
    +            if inc * buckets != maxv - minv:
    --- End diff --
    
    This was smart!


---
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]

Reply via email to