'numpy.float64' 对于 scipy 函数质心不可迭代
'numpy.float64' is not iterable for scipy function centre of mass
我一直在尝试根据我的观测数据(.nc 格式)计算降水场的质心,但我不断收到错误消息:"TypeError: 'numpy.float64' object is not iterable"
我已经设法将 netcdf 文件从 .nc
转换为 xarray 数据集,然后提取值以提供 (1, 90, 180) 数组然后我将其转换为 (90, 180) 以获得其他功能。然后我尝试计算阵列的质心,但它一直给我一条错误消息。
from scipy import ndimage
ncobsdata = Dataset('/home/data/20180380293.nc', mode = 'r')
obsdata = xr.open_dataset(xr.backends.NetCDF4DataStore(ncobsdata))
obs = obsdata.rain_total #shape = (1, 90, 180)
obsv = np.squeeze(obs) #I had to do this step to make it (90, 180)
CoM_obsv = ndimage.measurements.center_of_mass(obsv)
我希望获得质心结果,但我一直收到错误消息:
File "_____.py", line 10, in <module>
CoM_obsv = ndimage.measurements.center_of_mass(obsv)
File "________/scipy/ndimage/measurements.py", line 1289, in center_of_mass
return [tuple(v) for v in numpy.array(results).T]
TypeError: 'numpy.float64' object is not iterable
所以这里发生的是 obs
和 obsv
变量都存储为 xarray.DataArrays - 这个 class 是常规 numpy 数组的包装器。要访问基础 np.ndarray,您需要调用对象中的值:
CoM_obsv = ndimage.measurements.center_of_mass(obsv.values)
请注意,您不需要为 obsv = np.squeeze(obs) #I had to do this step to make it (90, 180)
执行此操作,因为已经有一个可用于 xarray.DataArrays 的挤压方法。
我一直在尝试根据我的观测数据(.nc 格式)计算降水场的质心,但我不断收到错误消息:"TypeError: 'numpy.float64' object is not iterable"
我已经设法将 netcdf 文件从 .nc
转换为 xarray 数据集,然后提取值以提供 (1, 90, 180) 数组然后我将其转换为 (90, 180) 以获得其他功能。然后我尝试计算阵列的质心,但它一直给我一条错误消息。
from scipy import ndimage
ncobsdata = Dataset('/home/data/20180380293.nc', mode = 'r')
obsdata = xr.open_dataset(xr.backends.NetCDF4DataStore(ncobsdata))
obs = obsdata.rain_total #shape = (1, 90, 180)
obsv = np.squeeze(obs) #I had to do this step to make it (90, 180)
CoM_obsv = ndimage.measurements.center_of_mass(obsv)
我希望获得质心结果,但我一直收到错误消息:
File "_____.py", line 10, in <module>
CoM_obsv = ndimage.measurements.center_of_mass(obsv)
File "________/scipy/ndimage/measurements.py", line 1289, in center_of_mass
return [tuple(v) for v in numpy.array(results).T]
TypeError: 'numpy.float64' object is not iterable
所以这里发生的是 obs
和 obsv
变量都存储为 xarray.DataArrays - 这个 class 是常规 numpy 数组的包装器。要访问基础 np.ndarray,您需要调用对象中的值:
CoM_obsv = ndimage.measurements.center_of_mass(obsv.values)
请注意,您不需要为 obsv = np.squeeze(obs) #I had to do this step to make it (90, 180)
执行此操作,因为已经有一个可用于 xarray.DataArrays 的挤压方法。