Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...