Why does a fill value lose its fractional part in an integer template?

full_like fills an array using the template's shape and, by default, its data type. The target type determines which values it can represent.

numpy.full_like in practice

Filling three integer slots with 0.75 yields zero in all three. Request a floating dtype when those slots represent probabilities.

import numpy as np
np.full_like([1,2,3], 0.75)

Result [0, 0, 0]

中文

为什么整数模板会截去填充值的小数?

full_like 根据模板的形状和默认数据类型创建常量数组,填充值要能由目标类型表达。

numpy.full_like 算例

给三个整数样本填入 0.75 会得到三个零。若这些值代表概率,显式指定浮点 dtype 才能保留小数。

import numpy as np
np.full_like([1,2,3], 0.75)

结果 [0, 0, 0]

References / 参考来源

Keep reading / 继续阅读