简单的问题,但我似乎无法让它工作。 我有一个形状为 (2000, 1) 的数组。从这个数组中,我想计算每 40 行的第 99 个百分位,并返回一个相同长度的 bool 数组,其中第 99 个百分位以上的所有内容都为真,其余为假。 对于前 40 个,我可以:
a = np.random.rand(2000,1)
maxper40 = a[0:40] > np.percentile(a[0:40], 99)
我可以做这样的事情来获得一个移动的窗口:
windowSize = 40
for i in range(0,len(a)-windowSize+1):
print max(a[i:i+windowSize])
我如何以 40 的间隔遍历数组,计算每个间隔的 99 个百分位数并返回与输入长度相同的 bool 数组?
非常感谢!
请您参考如下方法:
您可以按 50x40 reshape 数组并获取每个数组的百分位数:
a.reshape(50, 40) > np.percentile(a.reshape(50, 40), 99, axis=0)
这将返回一个大小为 (50, 40) 的数组。如果你想要一个一维数组,你可以在之后调用 flatten:
(a.reshape(50, 40) > np.percentile(a.reshape(50, 40), 99, axis=0)).flatten()
