What is the default of numpy functions, with where=False?

给你一囗甜甜゛ 提交于 2019-12-17 21:16:44

问题


The ufunc documentation states:

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.

What is the default behavior, when out is not given?

I observed some behavior, which doesn't really make sense to me:

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

Does anyone know the underlying reason/behavior? Especially np.reciprocal doesn't really make sense, as the reciprocal value can never be 0

EDIT: The behavior is even more complex:

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...

I'm using Numpy version 1.11.1


回答1:


It looks like garbage becasue that's exactly what it is - memory that's been garbage collected.

Whatever function you are calling sets aside a block of memory to put the results in, but never puts any results there because where=False. You're getting the same values you would from np.empty - i.e. whatever garbage was in that memory block before the function assigned it.



来源:https://stackoverflow.com/questions/45543505/what-is-the-default-of-numpy-functions-with-where-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!