【问题标题】:Numpy 2D array shuffle elements between rows and not columnsNumpy 2D 数组在行而不是列之间随机播放元素
【发布时间】:2020-05-15 18:59:45
【问题描述】:

我有以下二维数组:

seq_length = 5
x = np.array([[0, 2, 0, 4], [5,6,7,8]])
x_repeated = np.repeat(x, seq_length, axis=1)


[[0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]
 [5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]]

我想根据seq_length shuffle x_repeated seq 的所有项目将一起洗牌。

例如,可能的洗牌:

[[0 0 0 0 0 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
 [5 5 5 5 5 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]

谢谢

【问题讨论】:

    标签: python arrays numpy shuffle


    【解决方案1】:

    你可以这样做:

    import numpy as np
    
    seq_length = 5
    x = np.array([[0, 2, 0, 4], [5, 6, 7, 8]])
    
    swaps = np.random.choice([False, True], size=4)
    
    for swap_index, swap in enumerate(swaps):
        if swap:
            x[[0, 1], swap_index] = x[[1, 0], swap_index]
    
    x_repeated = np.repeat(x, seq_length, axis=1)
    

    您还可以依赖True 非零这一事实,并将for 替换为:

    for swap_index in swaps.nonzero()[0]:
        x[[0, 1], swap_index] = x[[1, 0], swap_index]
    

    关键是我在np.repeat 调用之前进行了洗牌/交换,与之后进行相比效率更高(同时满足您对需要交换的值序列的要求)。每对具有相同值的序列有 50% 的机会被交换。

    【讨论】:

      【解决方案2】:

      设法通过以下方式解决它:

      items_count = x.shape[-1]    
      swap_flags = np.repeat(np.random.choice([0, 1], size=items_count), single_item_length)
      

      给予:

      [1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]
      
      for idx, flag in enumerate(swap_flags):
          if flag:
              x_repeated[0,idx], x_repeated[1,idx] = x_repeated[1,idx], x_repeated[0,idx]
      

      结果:

      [[5 5 5 5 5 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
       [0 0 0 0 0 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]
      

      还是不那么优雅numpy 方式。

      【讨论】:

        【解决方案3】:

        这是我的尝试:

        def randomize_blocks(arr):
          """ Shuffles an n-dimensional array given consecutive blocks of numbers.
          """
          groups = (np.diff(arr.ravel(), prepend=0) != 0).cumsum().reshape(arr.shape)
          u, c = np.unique(groups, return_counts=True)
          np.random.shuffle(u)
          o = np.argsort(u)
          return arr.ravel()[np.argsort(np.repeat(u, c[o]))].reshape(arr.shape)
        

        Breakdown

        首先我们得到组

        groups = (np.diff(arr.ravel(), prepend=0) != 0).cumsum().reshape(arr.shape)
        

        array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
               [4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7]])
        

        然后,我们得到每个组的 unique 和 counts。

        u, c = np.unique(groups, return_counts=True)
        

        >>> print(u, c)
        (array([6, 0, 3, 5, 2, 4, 7, 1]),
         array([5, 5, 5, 5, 5, 5, 5, 5]))
        

        最后,我们shuffle 我们的唯一组,重构数组并使用argsort 重新排序打乱的唯一组。

        o = np.argsort(u)
        arr.ravel()[np.argsort(np.repeat(u, c[o]))].reshape(arr.shape)
        

        示例用法:

        >>> randomize_blocks(arr)
        array([[0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5],
               [7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6]])
        

        >>> randomize_blocks(arr)
        array([[6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 2, 2, 2, 2, 2]])
        

        【讨论】:

        • 感谢您的回答,但您没有回答。您确实在行和列之间进行了改组。我只问了行之间的随机播放。
        【解决方案4】:
        import random
        import numpy as np
        
        seq_length = 5
        x = np.array([[0, 2, 0, 4], [5,6,7,8]])
        x_repeated = np.repeat(x, seq_length, axis=1)
        
        cnt = 0
        frst_row = True
        
        ref_lst_1 = [(lambda x: x_repeated[0][seq_length*x])(x) for x in range(0,4)] # [0, 2, 0, 4]
        ref_lst_2 = [(lambda x: x_repeated[1][seq_length*x])(x) for x in range(0,4)] # [5, 6, 7, 8]
        
        nlist=[]
        
        while cnt < 8:
            frst_row = int(not frst_row)
            if frst_row == 0:
                index = random.randint(0, len(ref_lst_1)-1)
                num = ref_lst_1[index]
                # print("ref_lst_1 : ", ref_lst_1)
                ref_lst_1.pop(index)
            if frst_row == 1:
                index = random.randint(0, len(ref_lst_2)-1)
                num = ref_lst_2[index]
                # print("ref_lst_2 : ", ref_lst_2)
                ref_lst_2.pop(index)
            nlist = nlist+[num]*seq_length
            cnt+=1
        
        print(nlist)
        """[0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 
           0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 2, 2, 2, 2, 2, 7, 7, 7, 7, 7]
        """
        
        narray = np.array([nlist[0:20], nlist[20:40]])
        print(narray)
        
        """[[0 0 0 0 0 5 5 5 5 5 4 4 4 4 4 8 8 8 8 8]
         [0 0 0 0 0 6 6 6 6 6 2 2 2 2 2 7 7 7 7 7]]"""
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-04-17
          • 1970-01-01
          • 1970-01-01
          • 2014-04-21
          • 1970-01-01
          • 2023-03-10
          • 2018-12-03
          相关资源
          最近更新 更多