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

SciPy中label与sum函数正确导入方式及代码报错求助

解决SciPy ndimage.measurements弃用警告问题

你的代码出现弃用警告是因为SciPy官方计划在2.0.0版本移除scipy.ndimage.measurements命名空间,要求直接从scipy.ndimage导入label和sum函数。按照以下方式修改代码即可解决:

修改步骤

  • 替换导入语句:把from scipy.ndimage import measurements改为直接导入需要的函数,为避免和Python内置sum冲突,给sum起别名nd_sum
  • 替换代码中的函数调用:把measurements.label()换成label(),measurements.sum()换成nd_sum()

修改后的完整代码

from pylab import *
from scipy.ndimage import label, sum as nd_sum


L = 100
pv = [0.2,0.3,0.4,0.5,0.6,0.7]
z = rand(L,L)
for i in range(len(pv)):
    p = pv[i]
    m = z < p
    lw, num = label(m)
    area = nd_sum(m, lw, index=arange(lw.max() + 1))
    areaImg = area[lw]
    subplot(2,3,i+1)
    tit = 'p='+str(p)
    imshow(areaImg, origin='lower')
    title(tit)
    axis()

show()  # 加上这句确保图像正常显示,部分环境需要手动触发

补充说明

  • 新增的show()函数可以保证图像在多数运行环境下正常弹出显示
  • 修改后的代码完全保留原功能,同时消除弃用警告,兼容未来SciPy版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 22:22:25