Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Using np.maximum.reduceat to reduce an 2D array

$
0
0

I had a 2D array, which a pair of float data coupled with index (shaped (3, k) float array).I wanted to get the maximum value in data[0] for each indexes at data[2], while also getting the same index data from data[1]

See Example below:

data = np.array([[ 0.45114132,  0.31522008,  0.66176217,  0.45114132,  0.26872137],       [11.        ,  6.        , 10.        ,  4.        ,  8.        ],       [ 0.        ,  0.        ,  0.        ,  1.        ,  1.        ]])# Expected Output:array([[0.66176217, 0.45114132], [10., 4], [0, 1])

I could get the output from np.maximum.reduceat but only for the first row

indx = np.unique(data[2].astype(int),return_index=1)[1]np.maximum.reduceat(data[0], indx)   # not works for the whole `data`# Outputarray([0.66176217, 0.45114132])

Also, I've tried to create a mask, but this doesn't always works as the example

mask = np.maximum.reduceat(data[0], indx)data[:, np.isin(data[0], mask)]# Outputarray([[ 0.45114132,  0.66176217,  0.45114132],       [11.        , 10.        ,  4.        ],       [ 0.        ,  0.        ,  1.        ]])

Further note: I intended to prevent usage of any loops / list instantiations.
As handling a large amount of data would be too slow.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>