python - apply numpy.histogram to multidimensional array -


i want apply numpy.histogram() multi-dimensional array along axis.

say, example have 2d array , want apply histogram() along axis=1.

code:

import numpy  array = numpy.array([[0.6, 0.7, -0.3, 1.0, -0.8], [0.2, -1.0, -0.5, 0.5, 0.8],                      [0.25, 0.3, -0.1, -0.8, 1.0]]) bins = [-1.0, -0.5, 0, 0.5, 1.0, 1.0] hist, bin_edges = numpy.histogram(array, bins) print(hist) 

output:

[3 3 3 4 2] 

expected output:

[[1 1 0 2 1],  [1 1 1 2 0],  [1 1 2 0 1]] 

how can expected output?

i tried use solution suggested in this post, doesn't me expected output.

for n-d cases, can np.histogram2d making dummy x-axis (i):

def vec_hist(a, bins):     = np.repeat(np.arange(np.product(a.shape[:-1]), a.shape[-1]))     return np.histogram2d(i, a.flatten(), (a.shape[0], bins)).reshape(a.shape[:-1], -1) 

output

vec_hist(array, bins) out[453]:  (array([[ 1.,  1.,  0.,  2.,  1.],         [ 1.,  1.,  1.,  2.,  0.],         [ 1.,  1.,  2.,  0.,  1.]]),  array([ 0.        ,  0.66666667,  1.33333333,  2.        ]),  array([-1.       , -0.5      ,  0.       ,  0.5      ,  0.9999999,  1.       ])) 

for histograms on arbitrary axis, you'll need create i using np.meshgrid , np.ravel_multi_axis , use reshape resulting histogram.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -