GithubHelp home page GithubHelp logo

easonchan1213 / dsq Goto Github PK

View Code? Open in Web Editor NEW

This project forked from laserson/dsq

0.0 1.0 0.0 188 KB

Distributed Streaming Quantiles (for PySpark)

License: Apache License 2.0

Python 100.00%

dsq's Introduction

dsq

Distributed Streaming Quantiles

This module implements a count-min sketch-based streaming quantile computation, as described by Cormode and Muthukrishnan.

It is designed with PySpark in mind, but should be suitable with other distributed computing frameworks (e.g., MapReduce).

Installation

From the repo:

git clone https://github.com/laserson/dsq.git
cd dsq
python setup.py install

How it works

The count-min sketch is a probabilistic data structure, analogous to a Bloom filter that stores counts. The domain of possible values is split into dyadic intervals (powers of two) up to a specified number of levels. (This also means the domain must be specified in advance.) Each level has its own count-min sketch. When computing a quantile, we estimate the number of observations to the left of the specified quantile (along with the total count of observations). The estimate is built by taking the largest possible intervals from the highest levels of the tree; higher levels are more accurate.

Example usage

The quantile accumulator requires the specification of a few parameters:

  • lower_bound, upper_bound -- the domain of possible values in the stream

  • num_levels -- the depth of the binary tree that partitions the domain

  • epsilon, delta -- precision parameters: "the error should be within a factor of epsilon with probability (1-delta)". Note that the higher the demanded precision, the more memory is needed to store the sketch.

Finally, the smallest interval in the dyadic partitioning represents the smallest precision that is attainable.

# Example PySpark usage

# create a SparkContext
# NOTE: you need to ship the dsq.py file around the cluster
sc = SparkContext("spark://your.spark.master", "YourSparkApp",
        pyFiles=["/path/to/dsq.py"])

# create an RDD containing a bunch of random numbers
import random
values = sc.parallelize([random.uniform(0, 1) for i in xrange(10000)], 5)

# set parameters for QuantileAccumulator
lower_bound = 0
upper_bound = 1
num_levels = 12
epsilon = 0.001
delta = 0.01
seed = 1729 # optional

# create the accumulator function that will process a partition
from dsq import QuantileAccumulator
accum_fn = QuantileAccumulator(lower_bound, upper_bound, num_levels,
        epsilon, delta, seed)

# stream the data through the accumulators
accumulators = values.mapPartitions(accum_fn)
# and merge them all together
quantile_accum = accumulators.reduce(lambda x, y: x.merge(y))

# compute the 95th percentile
quantile_accum.ppf(0.95)

dsq's People

Contributors

laserson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.