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.
You could use np.ptp
1 (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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With