Skip to main content
 首页 » 编程设计

python之numpy.empty 给出非空数组

2025年05月04日15cloudgamer

当我使用 foo = np.empty(1) 创建一个空的 numpy 数组时,结果数组包含一个 float64:

>>> foo = np.empty(1) 
>>> foo 
array([ 0.]) 
>>> type(foo[0]) 
<type 'numpy.float64'> 

为什么它不直接返回 array([])?

请您参考如下方法:

您没有阅读 the documentation ,它说:

Return a new array of given shape and type, without initializing entries.

empty 与创建一个没有元素的“空”数组无关。这只是意味着数组没有初始化它的值(即,它们是不可预测的,取决于为数组分配的内存中发生的任何事情)。