代码之家  ›  专栏  ›  技术社区  ›  user9549524

基于一列的值从二维矩阵中提取值

  •  1
  • user9549524  · 技术社区  · 6 年前

    我有一个二维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]]
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Paul Panzer    6 年前

    您可以使用 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]])