'How to spread Numpy values on a different interval

I have the following array:

a = np.array([20, 10, 5, 15])

Given a new interval (for instance B = [10, 40]) I need to spread the values of the original array an the new interval, so the result would be:

b = np.array([40., 20, 10., 30])

A possible solution would be applying the following formula (in pseudocode) to each value:

new_value = min(B) + (current_value - min(a)) * (max(a)-min(a)) / (max(B)-min(B))

Any quicker approach ?



Solution 1:[1]

As mentioned in the comments, you can use numpy.interp like so:

np.interp(a, (a.min(),a.max()), (10,40))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rikyeah