为什么numpy.apply_along_axis给出相同的结果,而不管轴是什么?

时间:2020-10-19 14:02:29

标签: python-3.x numpy-ndarray

我有一个包含分数的numpy数组的numpy数组:

import numpy as np
In [1]: scores                                                                 
Out[1]: 
array([[-1.354 ,  0.    ,  0.6921, ..., -0.1972, -0.0454, -0.1233],
       [-1.6837,  0.    ,  0.7019, ..., -0.1534,  0.0536, -0.0269],
       [-1.4549, -0.346 ,  0.7698, ...,  0.385 ,  0.3527,  0.0277],
       ...,
       [-0.7322,  1.7791,  1.5935, ...,  0.515 ,  1.0949,  0.3007],
       [-0.3222,  1.2375,  1.6012, ...,  0.4675,  0.5924,  0.1081],
       [-1.4317,  0.    ,  0.7675, ..., -0.3711, -0.2111, -0.2084]])

In [2]: scores.shape                                                           
Out[2]: (7, 1324)

我应用了一个函数来沿轴0标准化-1和1之间的数据:

normlized_scores = np.apply_along_axis(lambda x, mini=np.amin(scores), maxi=np.amax(scores): 2*((x-mini)/(maxi-mini))-1, 0, scores)

我得到了预期的结果:

In [3]: normlized_scores                                                                                                                                                                 
Out[3]: 
array([[-0.13278178,  0.26165992,  0.46327963, ...,  0.20421243,
         0.24843418,  0.22574067],
       [-0.22882862,  0.26165992,  0.46613453, ...,  0.21697206,
         0.27727445,  0.25382352],
       [-0.16217555,  0.16086463,  0.48591488, ...,  0.37381653,
         0.36440703,  0.26972937],
       ...,
       [ 0.04835844,  0.77993999,  0.72587176, ...,  0.41168759,
         0.58062167,  0.3492586 ],
       [ 0.16779794,  0.62216331,  0.72811489, ...,  0.39785009,
         0.43423544,  0.29315116],
       [-0.15541702,  0.26165992,  0.48524485, ...,  0.1535526 ,
         0.20016314,  0.20094969]])

我应用相同的函数沿轴1标准化-1和1之间的数据:

normlized_scores = np.apply_along_axis(lambda x, mini=np.amin(scores), maxi=np.amax(scores): 2*((x-mini)/(maxi-mini))-1, 1, scores)

我也得到了预期的结果:

In [4]: normlized_scores                                                                                                                                                                
Out[4]: 
array([[-0.13278178,  0.26165992,  0.46327963, ...,  0.20421243,
         0.24843418,  0.22574067],
       [-0.22882862,  0.26165992,  0.46613453, ...,  0.21697206,
         0.27727445,  0.25382352],
       [-0.16217555,  0.16086463,  0.48591488, ...,  0.37381653,
         0.36440703,  0.26972937],
       ...,
       [ 0.04835844,  0.77993999,  0.72587176, ...,  0.41168759,
         0.58062167,  0.3492586 ],
       [ 0.16779794,  0.62216331,  0.72811489, ...,  0.39785009,
         0.43423544,  0.29315116],
       [-0.15541702,  0.26165992,  0.48524485, ...,  0.1535526 ,
         0.20016314,  0.20094969]])

我不知道这是怎么可能的...我想像这样的轴工作方式:

enter image description here

因此,例如np.sum函数的工作方式如图像中提到的那样:

In [5]: np.sum(scores) # axis=None                                                                                                                                                                 
Out[5]: -6527.8252

In [6]: np.apply_along_axis(np.sum, 0, scores) # axis=0                                                                                                                                                          
Out[6]: array([-7.7028,  4.2714,  7.6119, ...,  0.8379,  2.1619,  0.1256]) # shape: (1324,)

In [7]: np.apply_along_axis(np.sum, 1, scores) # axis=1                                                                                                                                                          
Out[7]: 
array([ -855.0827, -1011.2521,  -954.4777,  -948.0853,  -938.6737,
        -948.9815,  -871.2722]) # shape: (7,)

为什么我的函数不受axis参数的影响?我会错过什么吗?

编辑(请参见注释):根据@Divyessh,归一化主要不取决于轴。但是为什么它返回具有相同形状的数组?

0 个答案:

没有答案