在numpy中缩放(或规范化)这样的数组?

时间:2022-06-20 21:44:49

In numpy, the original array has the shape(2,2,2) like this

在numpy中,原始数组的形状(2,2,2)就像这样

[[[0.2,0.3],[0.1,0.5]],[[0.1,0.3],[0.1,0.4]]]

I'd like to scale the array so that the max value of the a dimension is 1 like this:

我想缩放数组,以便a维度的最大值为1,如下所示:

As max([0.2,0.1,0.1,0.1]) is 0.2, and 1/0.2 is 5, so for the first element of the int tuple, multiple it by 5.

因为max([0.2,0.1,0.1,0.1])是0.2,而1 / 0.2是5,所以对于int元组的第一个元素,将它乘以5。

As max([0.3,0.5,0.3,0.4]) is 0.5, and 1/0.5 is 2, so for the second element of the int tuple, multiple it by 2

因为max([0.3,0.5,0.3,0.4])是0.5,而1 / 0.5是2,所以对于int元组的第二个元素,将它乘以2

So the final array is like this:

所以最终的数组是这样的:

[[[1,0.6],[0.5,1]],[[0.5,0.6],[0.5,0.8]]]

I know how to multiple an array with an integer in numpy, but I'm not sure how to multiple the array with different factor. Does anyone have ideas about this?

我知道如何使用numpy中的整数来复用数组,但我不确定如何使用不同的因子对数组进行多重处理。有没有人有这个想法?

1 个解决方案

#1


4  

If your array = a:

如果你的数组= a:

>>> import numpy as np
>>> a = np.array([[[0.2,0.3],[0.1,0.5]],[[0.1,0.3],[0.1,0.4]]])

You can do this:

你可以这样做:

>>> a/np.amax(a.reshape(4,2),axis=0)
array([[[ 1. ,  0.6],
        [ 0.5,  1. ]],

       [[ 0.5,  0.6],
        [ 0.5,  0.8]]])

#1


4  

If your array = a:

如果你的数组= a:

>>> import numpy as np
>>> a = np.array([[[0.2,0.3],[0.1,0.5]],[[0.1,0.3],[0.1,0.4]]])

You can do this:

你可以这样做:

>>> a/np.amax(a.reshape(4,2),axis=0)
array([[[ 1. ,  0.6],
        [ 0.5,  1. ]],

       [[ 0.5,  0.6],
        [ 0.5,  0.8]]])