I have this numpy matrix:
x = np.random.randn(700,2)
What I wanna do is scale the values of the first column between that range: 1.5 and 11 and the values of the second column between `-0.5 and 5.0. Does anyone have an idea how I could achieve this? Thanks in advance
2 Answers
We use a new array y to hold the result:
def scale(col, min, max):
range = col.max() - col.min()
a = (col - col.min()) / range
return a * (max - min) + min
x = np.random.randn(700,2)
y = np.empty(x.shape)
y[:, 0] = scale(x[:, 0], 1.5, 11)
y[:, 1] = scale(x[:, 1], -0.5, 5)
print(y.min(axis=0), y.max(axis=0))
[ 1.5 -0.5] [11. 5.]
Comments
- subtract each column's minimum from itself
- for each column of the result divide by its maximum
- for column 0 of that result multiply by 11-1.5
- for column 1 of that result multiply by 5--0.5
- add 1.5 to column zero of that result
- add -0.5 to column one of that result
You could probably combine some of those steps.