Scaling continuous data to discrete range Edit: the context is as follows: I've trained some ML model that predicts some feature vector. Thats a. But I know that a can take some values from discrete range, but the model outputs continuous values. After applying predict I want to enforce the outputs to take the discrete values they are allowed to be in. I've also edited my example to be more informative.
I have this continuous data:
a = [0.003 0.994 1.334 3.2 1.464 2.454 2.941 999.999] (outputs)
How can I easily convert it to the following discrete array:
b = [0 1 2 3 3] (allowed values)
so that every element of a will be mapped to its closest counterpart in b:
a_scaled = [0 1 1 3 1 2 3 3] (what I need)
I know how to implement this, but I want to stick with &quot;don't invent the wheel&quot;. So is there any nice function from some library that does this?
Update Accordingly: Your question was not clear before, therefore, sorry for the irrelevant solution.
To achieve that, I don't think there is a public library function, however, you can build your solution using some beautiful &quot;ready&quot; functions.
Two solutions come into my mind:

First one's time complexity is O(N*M)

N is your prediction (list a in your case) size, and M is your dictionary (list b in your case) size.
import numpy as np

def findClosest(dictionary, value):
    idx = (np.abs(dictionary - value)).argmin()
    return dictionary[idx]

#[findClosest(b, elem) for elem in a] 

print([findClosest(b, elem) for elem in a])

This just subtracts your prediction value from the values in your dictionary and takes the absolute value of them. Then in the resulting array, it looks for the location of value that is smallest.

Second one's time complexity is O(N*log(M))

N and M denote the same thing as the first solution.
from bisect import bisect_left
def findClosestBinary(myList, myNumber):
    pos = bisect_left(myList, myNumber)
    if pos == 0:
        return myList[0]
    if pos == len(myList):
        return myList[-1]
    before = myList[pos - 1]
    after = myList[pos]
    if after - myNumber &lt; myNumber - before:
        return after
    else:
        return before

#[findClosestBinary(b, elem) for elem in a] 

print([findClosestBinary(b, elem) for elem in a])

Note: To save time I didn't implement myself but took the findClosestBinary() function from here.
This one is a better algorithmic approach in terms of time complexity. This does the same thing but uses a binary search to efficiently find the closest value in the dictionary.  However, it assumes your dictionary (list b) is sorted. Since your dictionary is a predefined list, you can improve the performance by providing a sorted array.
However, if your dictionary that you will map predictions to is not a very big one, then you can just use the first one. In the case of the dictionary being small, these two functions will behave the same in terms of time.