根据一列的值从 2D 矩阵中提取值

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

提问人:user9549524 提问时间:3/26/2018 最后编辑:user9549524 更新时间:3/26/2018 访问量:1311

问:

我有一个 2D 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),我想获取此 2D 数组的子集。结果应为:

[[ 10  3  20]
 [ 15  4  30]]
python 数组 numpy 编码样式 逻辑

评论

0赞 Aran-Fey 3/26/2018
您能否发布一个输入矩阵和预期结果的示例?仅通过阅读代码来理解您的程序应该做什么有点困难。

答:

1赞 Paul Panzer 3/26/2018 #1

您可以使用布尔索引

>>> 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]])

评论

1赞 holdenweb 3/26/2018
好东西。刚到同一个地方,所以停了下来。