【发布时间】:2016-03-23 07:34:46
【问题描述】:
我有一个 numpy 数组,它是从 netCDF 文件中引入的,其形状为 (930, 360, 720),其组织形式为 (时间、纬度、经度)。
在每个 930 个时间戳的每个纬度/经度对上,我需要计算该值达到或超过阈值“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() 函数了吗?计算超出阈值的次数然后计算百分比的最有效方法是什么?
【问题讨论】:
-
您是要计算整个数组中超过阈值的总次数还是每个时间戳的总次数。即您是在寻找一个数字,例如 100 次还是 930 个数字,所以在时间戳 1 上超过 5 次,在时间戳 2 上超过 10 次,依此类推。
-
每个纬度/经度的总次数(因此循环中的 i 和 j 对),因此最终输出将产生一个形状数组 (720, 360),每个点将保持超过的次数。这是否有助于更好地澄清它?
标签: python arrays numpy multidimensional-array percentage