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

    https://github.com/apache/spark/pull/2142#discussion_r16915511
  
    --- Diff: python/pyspark/rdd.py ---
    @@ -1993,11 +1993,46 @@ def meanApprox(self, timeout, confidence=0.95):
             >>> (rdd.meanApprox(1000) - r) / r < 0.05
             True
             """
    -        jrdd = self.map(float)._to_jrdd()
    +        jrdd = self.map(float)._to_java_object_rdd()
             jdrdd = self.ctx._jvm.JavaDoubleRDD.fromRDD(jrdd.rdd())
             r = jdrdd.meanApprox(timeout, confidence).getFinalValue()
             return BoundedFloat(r.mean(), r.confidence(), r.low(), r.high())
     
    +    def countApproxDistinct(self, relativeSD=0.05):
    +        """
    +        :: Experimental ::
    +        Return approximate number of distinct elements in the RDD.
    +
    +        The algorithm used is based on streamlib's implementation of
    +        "HyperLogLog in Practice: Algorithmic Engineering of a State
    +        of The Art Cardinality Estimation Algorithm", available
    +        <a href="http://dx.doi.org/10.1145/2452376.2452456";>here</a>.
    +
    +        @param relativeSD Relative accuracy. Smaller values create
    +                           counters that require more space.
    +                           It must be greater than 0.000017.
    +
    +        >>> n = sc.parallelize(range(1000)).map(str).countApproxDistinct()
    +        >>> 950 < n < 1050
    +        True
    +        >>> n = sc.parallelize([i % 20 for i in 
range(1000)]).countApproxDistinct()
    +        >>> 18 < n < 22
    +        True
    +        """
    +        if relativeSD < 0.000017:
    +            raise ValueError("relativeSD should be greater than 0.000017")
    +        if relativeSD > 0.37:
    +            raise ValueError("relativeSD should be smaller than 0.37")
    +        hashRDD = self.map(lambda x: portable_hash(x) % sys.maxint)
    +        c = hashRDD._to_java_object_rdd().countApproxDistinct(relativeSD)
    +        # range of hash is [0, sys.maxint]
    +        if c > sys.maxint / 30:
    --- End diff --
    
    Just a magic number :-) it could be 10 or 20 or 50.
    
    After some experiments, this correction still have biased errors from -10% 
to 1%.


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