Python 使用每个切片的百分位数筛选多维numpy数组

Python 使用每个切片的百分位数筛选多维numpy数组,python,numpy,Python,Numpy,我有一个形状为x,y,z的numpy数组,它表示x乘以y的z矩阵。我可以对每个矩阵进行切片,然后使用clip with percentiles过滤出异常值: mx = array[:, :, 0] # taking the first matrix filtered_mx = np.clip(mx, np.percentile(mx, 1), np.percentile(mx, 99)) 有没有什么有效的方法可以一次不在片上执行相同的操作?您可以将数组传递到np.clip,因此可以在mx的z

我有一个形状为x,y,z的numpy数组,它表示x乘以y的z矩阵。我可以对每个矩阵进行切片,然后使用clip with percentiles过滤出异常值:

mx = array[:, :, 0]  # taking the first matrix
filtered_mx = np.clip(mx, np.percentile(mx, 1), np.percentile(mx, 99))

有没有什么有效的方法可以一次不在片上执行相同的操作?

您可以将数组传递到
np.clip
,因此可以在
mx
z
维度上设置不同的限制:

将numpy导入为np
#创建随机mx
x、 y,z=10,11,12
mx=np.random.random((x,y,z))
#计算x和y维度上的百分位数
perc01=np.百分位(mx,1,轴=(0,1),keepdims=True)
perc99=np.百分位(mx,99,axis=(0,1),keepdims=True)
#在z维度上具有不同限制的剪辑数组
过滤的\u mx=np.clip(mx,a\u min=perc01,a\u max=perc99)