0
假设我有一个2d numpy数组,并且我想过滤每个行通过特定条件的元素。例如,我只想要其特定行以上的第90百分位以上的元素。我想出了这个解决方案:具有行特定条件的过滤器numpy数组
import numpy as np
a = np.random.random((6,5))
thresholds = np.percentile(a, 90, axis=1)
threshold_2d = np.vstack([thresholds]*a.shape[1]).T
mask = a > threshold_2d
final = np.where(mask, a, np.nan)
它的工作原理和它的矢量,但感觉有点尴尬,尤其是在我创建threshold_2d一部分。有没有更优雅的方式?我能以某种方式自动广播np.where的条件而不必创建匹配的2d掩码?