When can a scratch array skip zero initialization?
empty_like allocates a matching buffer. Initial values of ordinary numeric arrays are unspecified.
numpy.empty_like in practice
A transform that overwrites every pixel can write doubled values straight into its buffer. This example fills every position before reading. Initialize positions that a conditional write might skip.
import numpy as np
np.multiply([2,3], 2, out=np.empty_like([2,3]))Result [4, 6]
中文
何时可以跳过数组的初始填零?
empty_like 为与模板相匹配的数组分配空间,普通数值类型的初值没有保证。
numpy.empty_like 算例
图像变换若会覆盖每个输出像素,可以直接把乘二的结果写入缓冲区。本例先写满再读取;有条件跳过的像素应显式初始化。
import numpy as np
np.multiply([2,3], 2, out=np.empty_like([2,3]))结果 [4, 6]