当where=False时,numpy函数的默认值是什么?

2024-06-15 02:46:50 发布

您现在位置:Python中文网/ 问答频道 /正文

ufunc documentation表示:

where

New in version 1.7. Accepts a boolean array which is broadcast together with the operands. Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.

out未给定时,默认行为是什么?你知道吗

我观察到一些行为,这对我来说真的没有意义:

import numpy as np
a,b = np.ones((2,2))
np.add(a,b,where = False) #returns 0
np.exp(a, where = False)  #returns 1
np.sin(a, where = False)  #returns 1
np.sign(a, where = False) #returns 0
np.reciprocal(a, where = False) #returns 0

有人知道潜在的原因/行为吗? 尤其是np.reciprocal实际上没有意义,因为倒数永远不能是0

编辑:行为更为复杂:

a,b = np.ones(2)
np.add(a,b,where = False) #returns 6.0775647498958414e-316
a,b = 1,1
np.add(a,b, where = False) #returns 12301129, 
#running this line several times doesn't give the same result every time...

我使用的是Numpy版本1.11.1


Tags: ofthetoinaddfalsedocumentationnp
1条回答
网友
1楼 · 发布于 2024-06-15 02:46:50

它看起来像垃圾,因为它就是被垃圾收集的内存。你知道吗

无论调用什么函数,都会留出一块内存来放入结果,但决不会将任何结果放入其中,因为where=False。您得到的值与从np.empty得到的值相同,也就是说,在函数赋值之前,内存块中有任何垃圾。你知道吗

相关问题 更多 >