astrofrog / astrofrog/fast-histogram

messing around with alternatives

Open
#41 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
281
Forks
28
PR merge metrics
No merged PRs in 30d

Description

Hey.
I was just playing around with this and was trying to see if there's a way to implement this efficiently with standard libs.

My usual way to do things like this is using the ``scipy.sparse.coo_matrix`` construct.

```python
import scipy.sparse as sp

def bincount2d(x, y, bins):
return sp.coo_matrix((np.ones(x.shape[0]), (x, y)), shape=(bins, bins), dtype=np.int)
```

If the data was scaled so that making it ints would put it in the right bins, this would work.

```python

import numpy as np
x = np.random.random(10_000_000)
y = np.random.random(10_000_000)

from fast_histogram import histogram2d
%timeit _ = histogram2d(x, y, range=[[0, 1], [0, 1]], bins=30)
```
> 36.8 ms ± 4.14 ms per loop

```python
xx = (x * 30)
yy = (y * 30)

%timeit bincount2d(xx, yy, bins=30)
```
153 ms ± 4.04 ms per loop

So your code would "only" be 5x faster, so it's about a 4x speedup over numpy.
Unfortunately I cheated and didn't include shifting ``xx` so that the data aligns with the bins. I don't think it's possible to make this work without copying the data at least once, which is why I'm giving up on this route.

Thought it might be of interest, though ...

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the `fast_histogram.histogram2d` and `scipy.sparse.coo_matrix` benchmarks shown in the issue. The issue does not identify files, tests, an accepted implementation, or a definition of done, so further direction from the maintainers would be needed before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.