Skip to main content

Build Status

sparse_linear_binning: linear binning (optimized for sparsity)

Performs a linear binning technique described in Wand and Jones on a regularly-spaced grid in an arbitrary number of dimensions. The asymptotic behavior of this binning technique performs better than so-called simple binning (i.e. as in histograms). Each data point in d-dimensional space must have an associated weight (for equally weighted points just use a weight of 1.0 for each point).

For example, within a (segment of a) 2D grid with corners A, B, C, and D and a 2D data point P with weight wP:

A-----------------------------------B
|        |                          |
|                                   |
|        |                          |
|- - - - P- - - - - - - - - - - - - |
|        |                          |
D-----------------------------------C
  • Assign a weight to corner A of the proportion of area between P and C (times wP)

  • Assign a weight to corner B of the proportion of area between P and D (times wP)

  • Assign a weight to corner C of the proportion of area between P and A (times wP)

  • Assign a weight to corner D of the proportion of area between P and B (times wP)

Note that the under- and overflow bins need to be accounted for when specifying the numbers of grid points in each dimension (grid points act as bin centers). For instance, if you want grid points in steps of 0.1 in a range of [0,1] (i.e. (0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1)), specify the number of grid points to be 11. Internally, the grid points are stored in a high performance, C++-based hash table (sparsepp). This allows for finer binning in some circumstances because the hash table doesn’t allocates memory for grid points with near-zero weight. To accommodate arbitrary numbers of bins along each dimension, an arbitrary precision numeric library (boost multiprecision) may be used internally and will negatively impact performance. If this degradation in performance is unacceptable, consider reducing the number of grid points in such a way that the product of grid points in all dimensions is less than the numeric maximum of “unsigned long long” on your system. For instance, in 20 dimenisons with each dimension having 51 grid points gives a total of 14171098670753043575626125424226001 potential grid points at which point the arbitrary precision library must take care of all arithmetic related to determining grid points.

Quickstart

  • pip install sparse_linear_binning

or

Example

This constructs one million random 2D points in the unit square with random weights and constructs a grid of 51 by 51 (can be different along different dimensions) linearly binned “bin centers.” The boundaries of the grid of bin centers are specified by extents and can be thought of as the under- and overflow bins (i.e. these are the coordinates of the first and last bin centers).

from sparse_linear_binning import sparse_linear_binning
import numpy as np

# generate one million random 2D points and weights
# (should take less than a second to bin)
n_samples=1000000
D=2

# coordinates, weights, and extents must be of type "double"
sample_coords = np.random.random(size=(n_samples, D))
sample_weights = np.random.random(size=n_samples)
extents = np.tile([0., 1.], D).reshape((D, 2))
n_bins = np.full(D, 51)

coords, weights = sparse_linear_binning(sample_coords, sample_weights,
                                        extents, n_bins)

# check that weights on grid match original weights
print(np.allclose(weights.sum(), sample_weights.sum()))

Dependencies

  • numpy

# 1.1.0

  • Added support for automatically converting Numpy arrays to proper d-types.

  • Added more explicit tests.

Metadata

Release files for sparse_linear_binning 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparse_linear_binning 1.1.0
File Size Uploaded Provenance Actions
sparse_linear_binning-1.1.0.tar.gz 1.3 MB Without provenance Details for sparse_linear_binning-1.1.0.tar.gz

Release files / sparse_linear_binning-1.1.0.tar.gz

Download URL sparse_linear_binning-1.1.0.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
bdf33e1b799b66c53b057647f3ddd09dcd898be79b52f516247843f499615f46
BLAKE2b-256 checksum
How to use checksums
2788c9e113c4428b0c105c3e94f947180d1f2a09a0a7a662902a0eebba8a905d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for sparse_linear_binning 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page