从np.empty初始化numpy数组 [英] Initializing numpy array from np.empty
问题描述
从空内存初始化ndarray时如何确定符号位?
How are the sign bits determined when initializing an ndarray from empty memory?
>>> np.random.randn(3,3)
array([[-0.35557367, -0.0561576 , -1.84722985],
[ 0.89342124, -0.50871646, 1.31368413],
[ 0.0062188 , 1.62968789, 0.72367089]])
>>> np.empty((3,3))
array([[0.35557367, 0.0561576 , 1.84722985],
[0.89342124, 0.50871646, 1.31368413],
[0.0062188 , 1.62968789, 0.72367089]])
这些从空内存初始化的浮点值丢失了其符号†.为什么会这样?
These float values initialized from empty memory have lost their signs†. Why is that?
† 注意:此结果取决于内存重用的实现细节.该问题询问实施方案在做什么.
推荐答案
numpy.empty
并未手动清除符号位或任何东西.符号位就是malloc
返回值的那些位中可能留下的任何垃圾.您看到的效果是由于在其他地方调用numpy.absolute
造成的.
numpy.empty
isn't clearing the sign bits manually or anything. The sign bits are just whatever garbage happens to be left in those bits of the malloc
return value. The effect you're seeing is due to a numpy.absolute
call somewhere else.
问题是,numpy.empty
不会重用randn
返回值的缓冲区.毕竟,由于_
变量,当empty
创建其数组时,randn
返回值仍然有效.
The thing is, numpy.empty
isn't reusing the randn
return value's buffer. After all, the randn
return value is still alive when empty
creates its array, due to the _
variable.
numpy.empty
正在重用在字符串化第一个数组的过程中创建的数组的缓冲区.我相信它是这一个:
numpy.empty
is reusing the buffer of an array created in the process of stringifying the first array. I believe it's this one:
def fillFormat(self, data):
# only the finite values are used to compute the number of digits
finite_vals = data[isfinite(data)]
# choose exponential mode based on the non-zero finite values:
abs_non_zero = absolute(finite_vals[finite_vals != 0])
...
看到那个absolute
电话吗?就是那个.
See that absolute
call? That's the one.
以下是支持该结论的其他测试:
Here's some additional testing that supports that conclusion:
>>> a = numpy.random.randn(3, 3)
>>> b = numpy.arange(-5, 4, dtype=float)
>>> c = numpy.arange(-5, 13, 2, dtype=float)
>>> a
array([[-0.96810932, 0.86091026, -0.32675013],
[-1.23458136, 0.56151178, -0.37409982],
[-1.71348979, 0.64170792, -0.20679512]])
>>> numpy.empty((3, 3))
array([[ 0.96810932, 0.86091026, 0.32675013],
[ 1.23458136, 0.56151178, 0.37409982],
[ 1.71348979, 0.64170792, 0.20679512]])
>>> b
array([-5., -4., -3., -2., -1., 0., 1., 2., 3.])
>>> numpy.empty((3, 3))
array([[ 0.96810932, 0.86091026, 0.32675013],
[ 1.23458136, 0.56151178, 0.37409982],
[ 1.71348979, 0.64170792, 0.20679512]])
>>> c
array([ -5., -3., -1., 1., 3., 5., 7., 9., 11.])
>>> numpy.empty((3, 3))
array([[ 5., 3., 1.],
[ 1., 3., 5.],
[ 7., 9., 11.]])
>>> numpy.array([1.0, 0, 2, 3, 4, 5, 6, 7, 8, 9])
array([ 1., 0., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> numpy.empty((3, 3))
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
numpy.empty
结果受打印a
和c
的影响,而不是受那些数组创建过程的影响. b
没有任何作用,因为它有8个非零元素.最后的array([1.0, 0, 2, ...])
之所以起作用,是因为即使它有10个元素,它们中恰好有9个也不为零.
The numpy.empty
results are affected by printing a
and c
, rather than by the process of creating those arrays. b
has no effect, because it has 8 nonzero elements. The final array([1.0, 0, 2, ...])
has an effect, because even though it has 10 elements, exactly 9 of them are nonzero.
这篇关于从np.empty初始化numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!