Created
January 4, 2013 20:20
-
-
Save nkeim/4455635 to your computer and use it in GitHub Desktop.
Fast numpy histograms in 1 and 2 dimensions, extensible to n.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def fast_hist(data, bin_edges): | |
"""Fast 1-dimensional histogram. Comparable to numpy.histogram(), but careless. | |
'bin_edges' should encompass all values in 'data'; the first and last elements | |
in 'bin_edges' are ignored, and are effectively (-infinity, infinity). | |
Returns the histogram array only. | |
""" | |
# Yes, I've tested this against histogram(). | |
return np.bincount(np.digitize(data, bin_edges[1:-1]), minlength=len(bin_edges) - 1) | |
def fast_hist_2d(data, bin_edges): | |
"""Fast 2-dimensional histogram. Comparable to numpy.histogramdd(), but careless. | |
'data' is an Nx2 array. 'bin_edges' is used for both dimensions and should | |
encompass all values in 'data'; the first and last elements in 'bin_edges' | |
are ignored, and are effectively (-infinity, infinity). | |
Returns the histogram array only. | |
""" | |
# Yes, I've tested this against histogramdd(). | |
xassign = np.digitize(data[:,0], bin_edges[1:-1]) | |
yassign = np.digitize(data[:,1], bin_edges[1:-1]) | |
nbins = len(bin_edges) - 1 | |
flatcount = np.bincount(xassign + yassign * nbins, minlength=nbins*nbins) | |
return flatcount.reshape((nbins, nbins)).T |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment