0

I found this problem when calculating array from rasters:

with rasterio.open(file) as ds:   
    arr3d=ds.read()
    arr3d=np.ma.masked_where(arr3d==-32768,arr3d,False)
    list=[]
    for i in range(0,24):
        tmean=arr3d[i,:,:].mean()  
        list.append(tmean)

I just wanted to get the list containing 24 mean values, but this code returned the list including each layer of arr3d, its mask layer and mean values.

 len(list)=72       

But when I tried arr3d[i,:,:].mean(), just retruned a mean value without any array. What is the differce between arr.mean() and np.mean(arr)?

4

1 回答 1

1

np.mean()返回:(1) 单个值,如果平均值是沿展平的数组或数组是一维的,或者 (2) 沿每个轴具有平均值的值数组。因为这很令人困惑,我建议始终将axis参数显式传递给np.mean()函数。如果您不传递轴,则采用展平数组的平均值。函数也是如此.mean()——它们实际上是相同的函数。

我建议明确传递要计算平均值的轴:

with rio.open(file) as ds:   
    arr3d=ds.read()
    arr3d=np.ma.masked_where(arr3d==-32768,arr3d,False)
    means = np.mean(arr3d, axis=0)

然后means将始终具有与 的第一个轴相同数量的元素arr3d。您目前通过手动迭代 24 个元素来完成此操作,但您可以删除此步骤。

于 2017-03-27T14:51:57.823 回答