【发布时间】:2018-03-18 13:15:32
【问题描述】:
我有一些卫星数据,如下所示(散点图):
我现在想将这些数据按时间和纬度划分为规则网格,并让每个 bin 等于其中所有数据点的平均值。我一直在尝试scipy.stats.binned_statistic_2d,对我得到的结果感到困惑。
首先,如果我将“count”统计信息传递给 scipy binning 函数,它似乎可以正常工作(最少的代码和下面的图)。
id1 = np.ma.masked_where(id1==0, id1) #id1 is the actual data and I have tried using this masking argument and without to the same effect
x_range = np.arange(0,24.25,.25) #setting grid spacing for x and y
y_range = np.arange(-13,14,1)
xbins, ybins = len(x_range), len(y_range) #number of bins in each dimension
H, xedges, yedges, binnumber = stats.binned_statistic_2d(idtime, idlat, values = id1, statistic='count' , bins = [xbins, ybins]) #idtime and idlat are the locations of each id1 value in time and latitude
H = np.ma.masked_where(H==0, H) #masking where there was no data
XX, YY = np.meshgrid(xedges, yedges)
fig = plt.figure(figsize = (13,7))
ax1=plt.subplot(111)
plot1 = ax1.pcolormesh(XX,YY,H.T)
结果图
现在,如果我将统计数据更改为均值、np.mean、np.ma.mean 等...这是我得到的图,它似乎可以挑选出有数据的地方和没有数据的地方:
即使此数据的最小值和最大值分别为 612 和 2237026。我已经编写了一些手动执行此操作的代码,但它并不漂亮并且需要很长时间(而且我还没有完全考虑到边缘效应,所以运行到错误然后修复它需要很长时间)。
我希望得到一些建议以使其发挥作用。谢谢!
编辑:我刚刚注意到在运行脚本后我收到了运行时警告,我无法找到任何关于在线的信息。谷歌搜索警告返回零结果。除计数之外的每个统计选项都会出现警告。
AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\colors.py:494: RuntimeWarning:在 less cbook._putmask(xa, xa
Edit2:我在下面附加了一些重复我的问题的代码。此代码适用于统计计数,但不适用于平均值或任何其他统计数据。此代码以相同的方式产生与之前相同的运行时警告。
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
x = np.random.rand(1000)
y = np.random.rand(1000)
z = np.arange(1000)
H, xedges, yedges, binnumber = stats.binned_statistic_2d(x, y, values = z, statistic='count' , bins = [20, 20])
H2, xedges2, yedges2, binnumber2 = stats.binned_statistic_2d(x, y, values = z, statistic='mean' , bins = [20, 20])
XX, YY = np.meshgrid(xedges, yedges)
XX2, YY2 = np.meshgrid(xedges2, yedges2)
fig = plt.figure(figsize = (13,7))
ax1=plt.subplot(111)
plot1 = ax1.pcolormesh(XX,YY,H.T)
cbar = plt.colorbar(plot1,ax=ax1, pad = .015, aspect=10)
plt.show()
fig2 = plt.figure(figsize = (13,7))
ax2=plt.subplot(111)
plot2 = ax2.pcolormesh(XX2,YY2,H2.T)
cbar = plt.colorbar(plot2,ax=ax2, pad = .015, aspect=10)
plt.show()
编辑 3:User8153 能够识别问题。解决方案是从出现 nans 的 scipy stats 中屏蔽数组。我使用np.ma.masked_invalid() 来做到这一点。下面是我的原始数据和测试数据的平均统计量图。
【问题讨论】:
-
当使用
'count'统计信息时,您会屏蔽H中计数为 0 的元素,即没有数据。根据binned_statistic_2d的文档,当将统计信息更改为'mean'或'median'时,空箱由NaN表示。您是否尝试更改掩码以过滤掉那些NaNs? -
您检查了 NA 值吗?您没有提供您的数据,因此无法重现。
-
也许你退后一点,通过给问题中的问题提供minimal reproducible example 重新开始。 (您的代码不完整或无法验证,因此很难通过猜测以外的方式解决此问题)
-
我添加了重现我的问题的工作代码。我没有机会检查其他 cmets 以查看他们今天是否解决了问题,但明天会解决。我也在使用 Enthought Canopy 的 Windows 机器上。
标签: python numpy matplotlib scipy statistics