0

I have a 2-variable discrete function represented in the form of a tuple through the following line of code:

hist_values, hist_x, hist_y = np.histogram2d()

Where you can think of a non-smooth 3d surface with hist_values being the height of the surface at grids with edge coordinates of (hist_x, hist_y).

Now, I would like to collect those grids for which hist_values is above some threshold level.

Rebel
  • 472
  • 8
  • 25

1 Answers1

1

You could simply compare the hist_values with the threshold, this would give you a mask as an array of bool which can be used in slicing, e.g.:

import numpy as np

# prepare random input
arr1 = np.random.randint(0, 100, 1000)
arr2 = np.random.randint(0, 100, 1000)

# compute 2D histogram
hist_values, hist_x, hist_y = np.histogram2d(arr1, arr2)

mask = hist_values > threshold  # the array of `bool`
hist_values[mask]  # only the values above `threshold`

Of course, the values are then collected in a flattened array. Alternatively, you could also use mask to instantiate a masked-array object (using numpy.ma, see docs for more info on it).

If you are after the coordinates at which this is happening, you should use numpy.where().

# i0 and i1 contain the indices in the 0 and 1 dimensions respectively
i0, i1 = np.where(hist_values > threshold)

# e.g. this will give you the first value satisfying your condition
hist_values[i0[0], i1[0]]

For the corresponding values of hist_x and hist_y you should note that these are the boundaries of the bins, and not, for example, the mid-values, therefore you could resort to the lower or upper bound of it.

# lower edges of `hist_x` and `hist_y` respectively...
hist_x[i0]
hist_y[i1]

# ... and upper edges
hist_x[i0 + 1]
hist_y[i1 + 1]
norok2
  • 25,683
  • 4
  • 73
  • 99
  • Thanks Norok, This will give me hist_values above the limit. What I want are the *x*, and *y* arrays for which this holds.Could you please let me know how to extract these arrays? – Rebel Mar 27 '18 at 07:23
  • Thanks, this is amazing. To get mid-values, I just added half-width of the bins in each direction to all elements of the arrays. – Rebel Mar 27 '18 at 18:39