从np.empty初始化numpy数组 [英] Initializing numpy array from np.empty

查看:137
本文介绍了从np.empty初始化numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从空内存初始化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结果受打印ac的影响,而不是受那些数组创建过程的影响. 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屋!

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