You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Numpy数组十进制数求和异常:0.5被识别为0的问题求助

问题原因

你用np.full_like(red_band, 0.5)创建L数组时,full_like会完全继承red_band的数据类型——从输出能看到是uint16(无符号整数类型),小数0.5会被直接截断为0,所以L数组全是0。

解决方法

方法一:创建L时指定浮点数据类型

直接在full_like里指定浮点型dtype,确保0.5能被正确存储:

red_band = dataset.read(4)
nir_band = dataset.read(5)
# 指定dtype为float32或float64,按需选择
L = np.full_like(red_band, 0.5, dtype=np.float32)

# 提前转换波段为浮点型,避免重复类型转换
red_float = red_band.astype(np.float32)
nir_float = nir_band.astype(np.float32)
savi = ((nir_float - red_float) / (red_float + nir_float + L)) * (1 + L)

方法二:提前转换原波段为浮点型

先把red_band和nir_band转成浮点型,再用full_like创建L,此时L会自动继承浮点类型:

red_band = dataset.read(4).astype(np.float32)
nir_band = dataset.read(5).astype(np.float32)
L = np.full_like(red_band, 0.5)

savi = ((nir_band - red_band) / (red_band + nir_band + L)) * (1 + L)

方法三:计算时临时转换L的类型

如果不想修改L的创建逻辑,在计算SAVI时将L转成浮点型再参与运算:

red_band = dataset.read(4)
nir_band = dataset.read(5)
L = np.full_like(red_band, 0.5)

red_float = red_band.astype(float)
nir_float = nir_band.astype(float)
savi = ((nir_float - red_float) / (red_float + nir_float + L.astype(float))) * (1 + L.astype(float))

内容的提问来源于stack exchange,提问作者Franco Barrionuevo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:40:18