4

There is a simple way to normalize a ndarray (every values between 0.0, 1.0)?

For example, I have a matrix like:

a = [[1., 2., 3.],
     [4., 5., 6.],
     [7., 8., 9.]]

Until now I'm getting the max value with

max(max(p[1:]) for p in a)
a / p

Besides I think numpy may have a method to this in one line, this doesn't work if my data is something like this:

b = [[-1., -2., -3.],
     [-4., -5., -6.],
     [-7., -8., 0.]]

Which gives an error caused by zero division.

What I'm trying to do is that the max value became 1. So, I would like to do a translation such that 9 becomes 1 (in positive case just dividing the values by it max value), and 0 (when it is the max value) becomes 1 (with translation method, e.g), which I know hot to do, but I guess numpy may have a solution for do this thing in its package.

How can I perform this nicely with numpy?

Thank you in advance.

6
  • well how would you normalize the array if the maximum value is a 0? The error isn't because of the way you're doing it, it is legitimately a domain error in the way you've defined the problem. Commented Mar 17, 2014 at 16:08
  • What I'm trying to do is that the max value became 1 and the minimum 0. So, I would like to do a translation such that 0 becomes 1, which I know hot to do, but I guess numpy may have a solution inside its package. Commented Mar 17, 2014 at 16:15
  • Ok, so you want to normalize by the largest magnitude (i.e., absolute value). Commented Mar 17, 2014 at 16:16
  • The minimum zero doesn't make sense, sorry. Commented Mar 17, 2014 at 16:18
  • 1
    @sh1ftst0rm -- Not exactly. Normalizing by the largest magnitude would put the values in the range from -1, 1 in the general case. There needs to be a shift and normalization by the peak to peak value. Commented Mar 17, 2014 at 16:20

1 Answer 1

7

You could use np.ptp1 (peak to peak) in conjunction with np.min to do this in the general case:

new_arr = (a - a.min())/np.ptp(a)

example:

>>> a = np.array([[-1., 0, 1], [0, 2, 1]])
>>> np.ptp(a)
3.0
>>> a
array([[-1.,  0.,  1.],
       [ 0.,  2.,  1.]])
>>> (a - a.min())/np.ptp(a)
array([[ 0.        ,  0.33333333,  0.66666667],
       [ 0.33333333,  1.        ,  0.66666667]])

Of course, this still would give an error if a consists of entirely zeros -- But the problem isn't well posed in that case.

1IIRC, np.ptp calls np.max and np.min. If performance is really critical, you might what to create your own ptp and save np.min to a temporary variable so you don't calculate it twice.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that the OP has a problem when the max value is 0; I don't think this helps in that case.
@CodyPiersall -- Yeah, I realized that. Addressed.

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.