根据一列的值从二维矩阵中提取值

Extract values from a 2D matrix based on the values of one column

我有一个二维 numpy 数组 "X",其中包含 m 行和 n 列。当列 r 的值落在某个范围内时,我正在尝试提取一个子数组。现在我已经通过遍历每一行来实现这个,正如预期的那样真的很慢。 python 中更简单的方法是什么?

    for j in range(m):
        if ((X[j,r]>=lower1) & (X[j,r]<=upper1)):
            count=count+1
            if count==1:
                X_subset=X[j,:]
            else:
                X_subset=np.vstack([X_subset,X[j,:]])

例如:

X=np.array([[10,3,20],
            [1,1,25],
            [15,4,30]])

如果第二列的值在 3 到 4 的范围内(r=1,lower1=3,upper1=4),我想得到这个二维数组的子集。结果应该是:

[[ 10  3  20]
 [ 15  4  30]]

您可以使用 boolean indexing:

>>> def select(X, r, lower1, upper1):
...     m = X.shape[0]
...     count = 0
...     for j in range(m):
...         if ((X[j,r]>lower1) & (X[j,r]<upper1)):
...             count=count+1
...             if count==1:
...                 X_subset=X[j,:]
...             else:
...                 X_subset=np.vstack([X_subset,X[j,:]])
...     return X_subset
... 
# an example
>>> X = np.random.random((5, 5))
>>> r = 2
>>> l, u = 0.4, 0.8
# your method:
>>> select(X, r, l, u)
array([[0.35279849, 0.80630909, 0.67111171, 0.59768928, 0.71130907],
       [0.3013973 , 0.15820738, 0.69827899, 0.69536766, 0.70500236],
       [0.07456726, 0.51917318, 0.58905997, 0.93859414, 0.47375552],
       [0.27942043, 0.62996422, 0.78499397, 0.52212271, 0.51194071]])
# boolean indexing:
>>> X[(X[:, r] > l) & (X[:, r] < u)]
array([[0.35279849, 0.80630909, 0.67111171, 0.59768928, 0.71130907],
       [0.3013973 , 0.15820738, 0.69827899, 0.69536766, 0.70500236],
       [0.07456726, 0.51917318, 0.58905997, 0.93859414, 0.47375552],
       [0.27942043, 0.62996422, 0.78499397, 0.52212271, 0.51194071]])