计算 Python 中多维数组中达到或超过阈值的次数
Counting the number of times a threshold is met or exceeded in a multidimensional array in Python
我有一个从 netCDF 文件中引入的 numpy 数组,其形状为 (930, 360, 720),它被组织为(时间、纬度、经度)。
在 930 个时间戳的每个 lat/lon 对中,我需要计算该值达到或超过阈值 "x"(例如 0.2 或 0.5 等)的次数。 ) 并最终计算每个点超过阈值的百分比,然后输出结果以便稍后绘制。
我尝试了很多方法,但这是我最近的方法:
lat_length = len(lats)
#where lats has been defined earlier when unpacked from the netCDF dataset
lon_length = len(lons)
#just as lats; also these were defined before using np.meshgrid(lons, lats)
for i in range(0, lat_length):
for j in range(0, lon_length):
if ice[:,i,j] >= x:
#code to count number of occurrences here
#code to calculate percentage here
percent_ice[i,j] += count / len(time) #calculation
#then go on to plot percent_ice
我希望这是有道理的!我将不胜感激任何帮助。我在 Python 自学,所以我可能会遗漏一些简单的东西。
现在是使用 any() 函数的时候吗?计算超过阈值的次数然后计算百分比的最有效方法是什么?
啊,你看,又一个气象学家!
可能有多种方法可以做到这一点,我的解决方案不太可能是最快的,因为它使用了 numpy 的 MaskedArray
,众所周知它很慢,但这应该可行:
Numpy 有一种称为 MaskedArray
的数据类型,它实际上包含两个普通的 numpy
数组。它包含一个数据数组和一个布尔掩码。我会首先屏蔽所有大于或等于我的阈值的数据(使用 np.ma.masked_greater()
刚好大于):
ice = np.ma.masked_greater_equal(ice)
然后,您可以使用 ice.count()
通过指定要沿特定轴计数来确定每个 lat/lon 点低于阈值的值的数量:
n_good = ice.count(axis=0)
这应该return 一个包含好点数的二维数组。然后,您可以通过从 ice.shape[0]
:
中减去 n_good
来计算坏的数量
n_bad = ice.shape[0] - n_good
并使用以下方法计算不良百分比:
perc_bad = n_bad/float(ice.shape[0])
有很多方法可以不使用 MaskedArray
来做到这一点。这只是我想到的简单方法。
您可以将输入的 3D 数组与阈值 x
进行比较,然后沿第一个轴与 ndarray.sum(axis=0)
求和以获得计数,从而获得百分比,就像这样 -
# Calculate count after thresholding with x and summing along first axis
count = (ice > x).sum(axis=0)
# Get percentages (ratios) by dividing with first axis length
percent_ice = np.true_divide(count,ice.shape[0])
我有一个从 netCDF 文件中引入的 numpy 数组,其形状为 (930, 360, 720),它被组织为(时间、纬度、经度)。
在 930 个时间戳的每个 lat/lon 对中,我需要计算该值达到或超过阈值 "x"(例如 0.2 或 0.5 等)的次数。 ) 并最终计算每个点超过阈值的百分比,然后输出结果以便稍后绘制。
我尝试了很多方法,但这是我最近的方法:
lat_length = len(lats)
#where lats has been defined earlier when unpacked from the netCDF dataset
lon_length = len(lons)
#just as lats; also these were defined before using np.meshgrid(lons, lats)
for i in range(0, lat_length):
for j in range(0, lon_length):
if ice[:,i,j] >= x:
#code to count number of occurrences here
#code to calculate percentage here
percent_ice[i,j] += count / len(time) #calculation
#then go on to plot percent_ice
我希望这是有道理的!我将不胜感激任何帮助。我在 Python 自学,所以我可能会遗漏一些简单的东西。
现在是使用 any() 函数的时候吗?计算超过阈值的次数然后计算百分比的最有效方法是什么?
啊,你看,又一个气象学家!
可能有多种方法可以做到这一点,我的解决方案不太可能是最快的,因为它使用了 numpy 的 MaskedArray
,众所周知它很慢,但这应该可行:
Numpy 有一种称为 MaskedArray
的数据类型,它实际上包含两个普通的 numpy
数组。它包含一个数据数组和一个布尔掩码。我会首先屏蔽所有大于或等于我的阈值的数据(使用 np.ma.masked_greater()
刚好大于):
ice = np.ma.masked_greater_equal(ice)
然后,您可以使用 ice.count()
通过指定要沿特定轴计数来确定每个 lat/lon 点低于阈值的值的数量:
n_good = ice.count(axis=0)
这应该return 一个包含好点数的二维数组。然后,您可以通过从 ice.shape[0]
:
n_good
来计算坏的数量
n_bad = ice.shape[0] - n_good
并使用以下方法计算不良百分比:
perc_bad = n_bad/float(ice.shape[0])
有很多方法可以不使用 MaskedArray
来做到这一点。这只是我想到的简单方法。
您可以将输入的 3D 数组与阈值 x
进行比较,然后沿第一个轴与 ndarray.sum(axis=0)
求和以获得计数,从而获得百分比,就像这样 -
# Calculate count after thresholding with x and summing along first axis
count = (ice > x).sum(axis=0)
# Get percentages (ratios) by dividing with first axis length
percent_ice = np.true_divide(count,ice.shape[0])