python - 使用每个切片的百分位数过滤多维 numpy 数组

标签 python numpy

我有一个形状为 x,y,z 的 numpy 数组,它表示 x x y 的 z 矩阵。我可以对每个矩阵进行切片,然后使用带有百分位数的剪辑来过滤掉异常值:

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

有没有一些有效的方法可以做到同样的事情而不需要一次只做一个切片?

最佳答案

您可以将数组传递给 np.clip,因此在 mxz 维度上可能有不同的限制:

import numpy as np

# Create random mx
x, y, z = 10, 11, 12
mx = np.random.random((x, y, z))

# Calculate the percentiles across the x and y dimension
perc01 = np.percentile(mx, 1, axis=(0, 1), keepdims=True)
perc99 = np.percentile(mx, 99, axis=(0, 1), keepdims=True)

# Clip array with different limits across the z dimension
filtered_mx = np.clip(mx, a_min=perc01, a_max=perc99)

关于python - 使用每个切片的百分位数过滤多维 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59921404/

相关文章:

python - CSV 读取错误 : new-line character seen in unquoted field

python - 使用 BeautifulSoup 提取标签内的文本

python - 用字典值替换大型 numpy 张量中的条目

Python:将 3D 数组中的值存储到 csv

python - 根据列值从数据框中插入值

python - 比较数据框中的列以获得所需的输出

python - Flask 迁移忽略外键

python : how to speed up this file loading

python - 类型错误 : unsupported operand type(s) for/: 'list' and 'long'

python - Numpy:如何按整数值缩放整数数组?