1

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

0

2 Answers 2

3

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.]
Sign up to request clarification or add additional context in comments.

Comments

2
  • 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.