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

按列洗牌多维数组,并相应地更新索引列表

  •  1
  • Gabriel  · 技术社区  · 9 年前

    给定一个 N M 列数组,我需要将其洗牌 柱 同时更新一个单独的(唯一的)列索引列表,以指向无序元素的新位置。

    例如,以以下内容为例 (3, 5)

    a = [[ 0.15337424  0.21176979  0.19846229  0.5245618   0.24452392]
         [ 0.17460481  0.45727362  0.26914808  0.81620202  0.8898504 ]
         [ 0.50104826  0.22457154  0.24044079  0.09524352  0.95904348]]
    

    以及列索引列表:

    idxs = [0 3 4]
    

    如果我按列洗牌数组,使其看起来像这样:

    a = [[ 0.24452392  0.19846229  0.5245618   0.21176979  0.15337424]
         [ 0.8898504   0.26914808  0.81620202  0.45727362  0.17460481]
         [ 0.95904348  0.24044079  0.09524352  0.22457154  0.50104826]]
    

    索引数组应修改为如下所示:

    idxs = [4 2 0]
    

    我可以通过在洗牌前后转置数组来按列洗牌数组(请参阅下面的代码),但我不确定如何更新索引列表。整个过程需要尽可能快,因为新阵列将执行数百万次。

    import numpy as np
    
    def getData():
        # Array of (N, M) dimensions
        N, M = 10, 500
        a = np.random.random((N, M))
    
        # List of unique column indexes in a.
        # This list could be empty, or it could have a length of 'M'
        # (ie: contain all the indexes in the range of 'a').
        P = int(M * np.random.uniform())
        idxs = np.arange(0, M)
        np.random.shuffle(idxs)
        idxs = idxs[:P]
    
        return a, idxs
    
    
    a, idxs = getData()
    
    # Shuffle a by columns
    b = a.T
    np.random.shuffle(b)
    a = b.T
    
    # Update the 'idxs' list?
    
    3 回复  |  直到 9 年前
        1
  •  1
  •   Divakar    9 年前

    得到列索引的随机置换,其中 np.random.permutation

    col_idx = np.random.permutation(a.shape[1])
    

    shuffled_a = a[:,col_idx]
    

    然后,只需索引到排序后的索引中 col_idx 具有 idxs 对于追溯版本-

    shuffled_idxs = col_idx.argsort()[idxs]
    

    In [236]: a # input array
    Out[236]: 
    array([[ 0.1534,  0.2118,  0.1985,  0.5246,  0.2445],
           [ 0.1746,  0.4573,  0.2691,  0.8162,  0.8899],
           [ 0.501 ,  0.2246,  0.2404,  0.0952,  0.959 ]])
    
    In [237]: col_idx = np.random.permutation(a.shape[1])
    
    # Let's use the sample permuted column indices to verify desired o/p
    In [238]: col_idx = np.array([4,2,3,1,0])
    
    In [239]: shuffled_a = a[:,col_idx]
    
    In [240]: shuffled_a
    Out[240]: 
    array([[ 0.2445,  0.1985,  0.5246,  0.2118,  0.1534],
           [ 0.8899,  0.2691,  0.8162,  0.4573,  0.1746],
           [ 0.959 ,  0.2404,  0.0952,  0.2246,  0.501 ]])
    
    In [241]: col_idx.argsort()[idxs]
    Out[241]: array([4, 2, 0])
    
        2
  •  0
  •   Acccumulation    9 年前
    original_index = range(a.shape[1])
    permutation_series = pd.Series(original_index)
    permutation_series.index = np.random.permutation(original_index)
    new_idx = permutation_series[old_idx]
    a = a[:,permutation_series.index]
    
        3
  •  0
  •   hemraj    9 年前

    数据数组必须使用索引数组进行洗牌,因此首先洗牌索引数组并使用它洗牌数据数组。

    import numpy as np
    
    def getData():
        # Array of (N, M) dimensions
        a = np.arange(15).reshape(3, 5)
        # [[ 0  1  2  3  4]
        # [ 5  6  7  8  9]
        # [10 11 12 13 14]]
        idxs = np.arange(a.shape[0]) #  [0 1 2]
        return a, idxs
    
    a, idxs = getData()
    
    # Shuffle a by columns
    b = a.T
    # [[ 0  5 10]
    # [ 1  6 11]
    # [ 2  7 12]
    # [ 3  8 13]
    # [ 4  9 14]]
    
    np.random.shuffle(idxs)  #  [2 0 1]
    a = b[:, idxs]
    
    # [[10  0  5]
    # [11  1  6]
    # [12  2  7]
    # [13  3  8]
    # [14  4  9]]
    

    所以,如果你想洗牌任何其他数组,比如说x,以匹配数组a的洗牌,IDX将非常有用