尽管 "from __future__ import division",为什么 int numpy 数组的逆幂为 0?
Why is the inverse power of a int numpy array 0 despite "from __future__ import division"?
我总是使用from __future__ import division
来避免整数除法问题。我刚遇到一个情况,它显然仍然存在:
np.array([10])**(-1)
returns array([0])
,与 1/np.array([10])
相反,returns array([ 0.1])
符合预期。
我知道这可以解决,例如通过使用 np.array([10])**(-1.)
或使用 astype('float')
将数组转换为浮点数。我只想知道为什么它会这样,因为它对我来说似乎有点不一致,例如10**(-1)
给出 0.1。
numpy.array()
使用运算符重载来实现功能。例如,**
由 object.__pow__()
special method 处理。
/
运算符通常由 object.__div__
(and the __rdiv__
method for right-hand-side values), and the from __future__ import division
'switch' causes Python to use the object.__truediv__
方法处理。
然而,**
运算符不受开关的影响; Python 将继续调用 object.__pow__
而 numpy
不会知道您使用了 __future__
导入。
因此,当您给它整数操作数时,它会继续 return 整数结果;即使那样它也不会强迫float
。
请注意,Python 也以 //
operator and the __floordiv__
hook 的形式提供了 下限除法 运算符。没有等价于求幂的下限,因此 numpy
没有其他选项可以让您区分使用 float
和 int
操作数。
作为上面给出的答案的脚注,您可以通过检查相应通用函数的 types
属性来检查 NumPy 将 return 用于特定输入数组类型的内容。
对于 NumPy 数组,幂运算符 **
对应于 np.power
:
>>> np.power.types
['bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L', 'qq->q',
'QQ->Q', 'ee->e', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D', 'GG->G', 'OO->O']
Python兼容的整数类型由l
表示,Python兼容的浮点数由d
(see here)表示。
你可以看到对于两个整数类型,**
将 return 另一个整数:'ll->l'
对于 NumPy 数组,除法运算符 /
对应于 np.divide
:
>>> np.divide.types
['bb->d', 'BB->d', 'hh->d', 'HH->d', 'ii->d', 'II->d', 'll->d', 'LL->d', 'qq->d',
'QQ->d', 'ee->e', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D', 'GG->G', 'mq->m',
'md->m', 'mm->d', 'OO->O']
在这里,给定两个整数类型(以及大多数其他输入),一个浮点数将被 returned:'ll->d'
我总是使用from __future__ import division
来避免整数除法问题。我刚遇到一个情况,它显然仍然存在:
np.array([10])**(-1)
returns array([0])
,与 1/np.array([10])
相反,returns array([ 0.1])
符合预期。
我知道这可以解决,例如通过使用 np.array([10])**(-1.)
或使用 astype('float')
将数组转换为浮点数。我只想知道为什么它会这样,因为它对我来说似乎有点不一致,例如10**(-1)
给出 0.1。
numpy.array()
使用运算符重载来实现功能。例如,**
由 object.__pow__()
special method 处理。
/
运算符通常由 object.__div__
(and the __rdiv__
method for right-hand-side values), and the from __future__ import division
'switch' causes Python to use the object.__truediv__
方法处理。
然而,**
运算符不受开关的影响; Python 将继续调用 object.__pow__
而 numpy
不会知道您使用了 __future__
导入。
因此,当您给它整数操作数时,它会继续 return 整数结果;即使那样它也不会强迫float
。
请注意,Python 也以 //
operator and the __floordiv__
hook 的形式提供了 下限除法 运算符。没有等价于求幂的下限,因此 numpy
没有其他选项可以让您区分使用 float
和 int
操作数。
作为上面给出的答案的脚注,您可以通过检查相应通用函数的 types
属性来检查 NumPy 将 return 用于特定输入数组类型的内容。
对于 NumPy 数组,幂运算符 **
对应于 np.power
:
>>> np.power.types
['bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L', 'qq->q',
'QQ->Q', 'ee->e', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D', 'GG->G', 'OO->O']
Python兼容的整数类型由l
表示,Python兼容的浮点数由d
(see here)表示。
你可以看到对于两个整数类型,**
将 return 另一个整数:'ll->l'
对于 NumPy 数组,除法运算符 /
对应于 np.divide
:
>>> np.divide.types
['bb->d', 'BB->d', 'hh->d', 'HH->d', 'ii->d', 'II->d', 'll->d', 'LL->d', 'qq->d',
'QQ->d', 'ee->e', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D', 'GG->G', 'mq->m',
'md->m', 'mm->d', 'OO->O']
在这里,给定两个整数类型(以及大多数其他输入),一个浮点数将被 returned:'ll->d'