tda
tda

Reputation: 2133

Scale/Transform/Normalise NumPy Array between Two Values

I have the following scenario:

value_range = [250.0, 350.0]
precision = 0.01
unique_values = len(np.arange(min(values_range), 
                              max(values_range) + precision, 
                              precision))

This means all values range between 250.0 and 350.0 with a precision of 0.01, giving a potential total of 10001 unique values that the data set can have.

# This is the data I'd like to scale
values_to_scale = np.arange(min(value_range), 
                            max(value_range) + precision, 
                            precision) 

# These are the bins I want to assign to
unique_bins = np.arange(1, unique_values + 1)

You can see in the above example, each value in values_to_scale will map exactly to its corresponding item in the unique_bins array. I.e. a value of 250.0 (values_to_scale[0]) will equal 1.0 (unique_bins[0]) etc.

However, if my values_to_scale array looks like:

values_to_scale = np.array((250.66, 342.02)) 

How can I do the scaling/transformation to get the unique bin value? I.e. 250.66 should equal a value of 66 but how do I obtain this?

NOTE The value_range could equally be between -1 and 1, I'm just looking for a generic way to scale/normalise data between two values.

Upvotes: 1

Views: 834

Answers (1)

Nils Werner
Nils Werner

Reputation: 36719

You're basically looking for a linear interpolation between min and max:

minv = min(value_range)
maxv = max(value_range)
unique_values = int(((maxv - minv) / precision) + 1)
((values_to_scale - minv) / (maxv + precision - minv) * unique_values).astype(int)
# array([  65, 9202])

Upvotes: 1

Related Questions