numpy函数的默认值是什么,其中where = False? [英] What is the default of numpy functions, with where=False?

查看:187
本文介绍了numpy函数的默认值是什么,其中where = False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ufunc文档指出:

The ufunc documentation states:

其中

1.7版中的新功能. 接受与操作数一起广播的布尔数组.值True表示要在该位置计算ufunc,值False表示将值保留在输出中.

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时的默认行为是什么?

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

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

Does anyone know the underlying reason/behavior? Especially np.reciprocal doesn't really make sense, as the reciprocal value can never be 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

I'm using Numpy version 1.11.1

推荐答案

它看起来就像是垃圾,因为它是确切的-被垃圾回收的内存.

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

无论您要调用什么函数,都将预留一块内存来存放结果,但由于where=False,因此永远不会在其中存放任何结果.您将获得与np.empty相同的值-即在函数分配给该内存块之前该内存块中存在的任何垃圾.

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.

这篇关于numpy函数的默认值是什么,其中where = False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆