小编DJC*_*ley的帖子

当指数为负数时,为什么numpy.power不对数组进行元素处理?

对于负指数,numpy.power或**与/处理数组时有什么区别?以及为什么numpy.power没有按照文档中的描述进行元素操作。

例如,使用python 2.7.3:

>>> from __future__ import division
>>> import numpy as np   
>>> A = arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
Run Code Online (Sandbox Code Playgroud)

当指数为负时,看来**和numpy.power并非按元素进行操作

>>> A**-1
array([[-9223372036854775808,                    1,                    0],
       [                   0,                    0,                    0],
       [                   0,                    0,                    0]])
>>> np.power(A, -1)
array([[-9223372036854775808,                    1,                    0],
       [                   0,                    0,                    0],
       [                   0,                    0,                    0]])
Run Code Online (Sandbox Code Playgroud)

鉴于/是基于元素的

>>> 1/A
array([[        inf,  1.        ,  0.5       ],
       [ 0.33333333,  0.25      ,  0.2       ],
       [ 0.16666667,  0.14285714,  0.125     ]])
Run Code Online (Sandbox Code Playgroud)

当指数为正时,我没有此类问题。为什么它对负指数表现不同?

python arrays numpy

2
推荐指数
1
解决办法
3785
查看次数

标签 统计

arrays ×1

numpy ×1

python ×1