Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalize / Translate ndarray - Numpy / Python

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.

like image 812
pceccon Avatar asked Mar 20 '23 05:03

pceccon


1 Answers

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.

like image 95
mgilson Avatar answered Apr 02 '23 14:04

mgilson