Python: assigning # values ​​to a list for silos, rounding

I want a function that can take a series and a set of silos and basically round to the nearest silo. For instance:

my_series = [ 1, 1.5, 2, 2.3,  2.6,  3]
def my_function(my_series, bins):
    ...

my_function(my_series, bins=[1,2,3])
> [1,2,2,3,3,3]

It looks like Numpy Digitize is meant to be executed, but it generates the wrong values ​​(asterisks for the wrong values):

np.digitize(my_series, bins= [1,2,3], right=False)
> [1, 1*, 2, 2*, 2*, 3]

The reason this is incorrect is clear from the documentation:

Each returned index i is such that bins [i-1] <= x <[i] if the boxes are monotonically increasing or bins [i-1]> x> = bins [i] if the bin is monotonically decreasing. If the values ​​in x go beyond the boxes, 0 or len (baskets) are returned, respectively. If the right is true, then the right bit is closed so that the index i is such that the bins [i-1] x <= bins [i] or bins [i-1]> = x> bins [i] '', if the cells are monotonous increasing or decreasing accordingly.

I can get closer to what I want if I enter in values ​​decreasing and setting "rights" to True ...

np.digitize(my_series, bins= [3,2,1], right=True)
> [3, 2, 2, 1, 1, 1]

(1) (3). , 3 , , . .

+4
3

np.digitize right, True, , bins, np.take, :

np.take(bins,np.digitize(a,bins,right=True))
+1

np.searchsorted , :

a , v , a .

In [1]: my_series = [1, 1.5, 2, 2.3, 2.6, 3]

In [2]: bins = [1,2,3]

In [3]: import numpy as np

In [4]: [bins[k] for k in np.searchsorted(bins, my_series)]
Out[4]: [1, 2, 2, 3, 3, 3]

( numpy 1.10.0, digitize searchsorted.)

+1

Another way:

In [25]: def find_nearest(array,value):
    ...:     idx = (np.abs(array-np.ceil(value))).argmin()
    ...:     return array[idx]
    ...: 

In [26]: my_series = np.array([ 1, 1.5, 2, 2.3,  2.6,  3])

In [27]: bins = [1, 2, 3]

In [28]: [find_nearest(bins, x) for x in my_series]
Out[28]: [1, 2, 2, 3, 3, 3]
+1
source

Source: https://habr.com/ru/post/1653967/


All Articles