如何随机移动numpy数组的行

时间:2021-05-10 16:01:28

I am looking for a more pythonic way of randomly shifting rows of a numpy array. The idea is that I have an array of data, and I want to left-shift each row of the array by a random amount. My solution, which works, but I feel is a bit un-pythonic:

我正在寻找一种更加pythonic的方式来随机移动numpy数组的行。我的想法是我有一个数据数组,我想左移数组的每一行。我的解决方案有效,但我感觉有点不像pythonic:

def shift_rows(data, max_shift):
    """Left-shifts each row in `data` by a random amount up to `max_shift`."""
    return np.array([np.roll(row, -np.random.randint(0, max_shift)) for row in data])

And to test:

并测试:

data = np.array([np.arange(0, 5) for _ in range(10)])  # toy data to illustrate
shifted = shift_rows(data, max_shift=5)
shifted
# array([1, 2, 3, 4, 0],
#       [1, 2, 3, 4, 0],
#       [0, 1, 2, 3, 4],
#       ...
#       [4, 0, 1, 2, 3]])

This is really more of a thought experiment. Can anybody come up with a more efficient or more pythonic way of doing this? I suppose list comprehensions are pythonic, but if I need to do this over a huge array is this efficient?

这实际上更像是一个思想实验。任何人都可以提出更高效或更pythonic的方式吗?我认为列表推导是pythonic,但如果我需要在一个巨大的数组上做这个有效吗?

Edit: I marked the excellent reply by Divakar as the answer, but I would still love to hear it if anybody has any other ideas.

编辑:我将Divakar的优秀回复标记为答案,但如果有人有任何其他想法,我仍然会喜欢听到它。

1 个解决方案

#1


3  

Generate all the column indices for all rows in one go and then simply use integer-indexing for a vectorized solution, like so -

一次性生成所有行的所有列索引,然后简单地使用整数索引作为矢量化解决方案,如下所示 -

# Store shape of input array
m,n = data.shape

# Get random column start indices for each row in one go
col_start = np.random.randint(0, max_shift, data.shape[0])

# Get the rolled indices for every row again in a vectorized manner.
# We are extending col_start to 2D and then adding a range array to get 
# all column indices for every row by leveraging NumPy's braodcasting.
# Because of the additions, we might go off-limits. So, to simulate the 
# rolled over version, mod it.
idx = np.mod(col_start[:,None] + np.arange(n), n)

# Finall with integer indexing get the values off data array
shifted_out = data[np.arange(m)[:,None], idx]

Step-by-step run -

一步一步的运行 -

1] Inputs :

1]输入:

In [548]: data
Out[548]: 
array([[44, 23, 38, 32, 30],
       [69, 15, 32, 41, 63],
       [69, 41, 75, 50, 87],
       [23, 28, 38, 79, 91]])

In [549]: max_shift = 5

2] Proposed solution :

2]提出的解决方案:

2A] Get column starts :

2A]获取专栏开始:

In [550]: m,n = data.shape

In [551]: col_start = np.random.randint(0, max_shift, data.shape[0])

In [552]: col_start
Out[552]: array([1, 2, 3, 3])

2B] Get all indices :

2B]获取所有指数:

In [553]: idx = np.mod(col_start[:,None] + np.arange(n), n)

In [554]: col_start[:,None]
Out[554]: 
array([[1],
       [2],
       [3],
       [3]])

In [555]: col_start[:,None] + np.arange(n)
Out[555]: 
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [3, 4, 5, 6, 7]])

In [556]: np.mod(col_start[:,None] + np.arange(n), n)
Out[556]: 
array([[1, 2, 3, 4, 0],
       [2, 3, 4, 0, 1],
       [3, 4, 0, 1, 2],
       [3, 4, 0, 1, 2]])

2C] Finally index into data :

2C]最后索引数据:

In [557]: data[np.arange(m)[:,None], idx]
Out[557]: 
array([[23, 38, 32, 30, 44],
       [32, 41, 63, 69, 15],
       [50, 87, 69, 41, 75],
       [79, 91, 23, 28, 38]])

Verification -

1] Original approach :

1]原创方法:

In [536]: data = np.random.randint(11,99,(4,5))
     ...: max_shift = 5
     ...: col_start = -np.random.randint(0, max_shift, data.shape[0])
     ...: for i,row in enumerate(data):
     ...:     print np.array([np.roll(row, col_start[i])])
     ...:     
[[83 93 17 53 61]]
[[55 88 84 94 89]]
[[59 63 29 72 85]]
[[57 95 13 21 14]]

2] Proposed approach re-using col_start, so that we could do a value verification :

2]提议的方法重用col_start,以便我们可以进行值验证:

In [537]: m,n = data.shape

In [538]: idx = np.mod(-col_start[:,None] + np.arange(n), n)

In [539]: data[np.arange(m)[:,None], idx]
Out[539]: 
array([[83, 93, 17, 53, 61],
       [55, 88, 84, 94, 89],
       [59, 63, 29, 72, 85],
       [57, 95, 13, 21, 14]])

#1


3  

Generate all the column indices for all rows in one go and then simply use integer-indexing for a vectorized solution, like so -

一次性生成所有行的所有列索引,然后简单地使用整数索引作为矢量化解决方案,如下所示 -

# Store shape of input array
m,n = data.shape

# Get random column start indices for each row in one go
col_start = np.random.randint(0, max_shift, data.shape[0])

# Get the rolled indices for every row again in a vectorized manner.
# We are extending col_start to 2D and then adding a range array to get 
# all column indices for every row by leveraging NumPy's braodcasting.
# Because of the additions, we might go off-limits. So, to simulate the 
# rolled over version, mod it.
idx = np.mod(col_start[:,None] + np.arange(n), n)

# Finall with integer indexing get the values off data array
shifted_out = data[np.arange(m)[:,None], idx]

Step-by-step run -

一步一步的运行 -

1] Inputs :

1]输入:

In [548]: data
Out[548]: 
array([[44, 23, 38, 32, 30],
       [69, 15, 32, 41, 63],
       [69, 41, 75, 50, 87],
       [23, 28, 38, 79, 91]])

In [549]: max_shift = 5

2] Proposed solution :

2]提出的解决方案:

2A] Get column starts :

2A]获取专栏开始:

In [550]: m,n = data.shape

In [551]: col_start = np.random.randint(0, max_shift, data.shape[0])

In [552]: col_start
Out[552]: array([1, 2, 3, 3])

2B] Get all indices :

2B]获取所有指数:

In [553]: idx = np.mod(col_start[:,None] + np.arange(n), n)

In [554]: col_start[:,None]
Out[554]: 
array([[1],
       [2],
       [3],
       [3]])

In [555]: col_start[:,None] + np.arange(n)
Out[555]: 
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [3, 4, 5, 6, 7]])

In [556]: np.mod(col_start[:,None] + np.arange(n), n)
Out[556]: 
array([[1, 2, 3, 4, 0],
       [2, 3, 4, 0, 1],
       [3, 4, 0, 1, 2],
       [3, 4, 0, 1, 2]])

2C] Finally index into data :

2C]最后索引数据:

In [557]: data[np.arange(m)[:,None], idx]
Out[557]: 
array([[23, 38, 32, 30, 44],
       [32, 41, 63, 69, 15],
       [50, 87, 69, 41, 75],
       [79, 91, 23, 28, 38]])

Verification -

1] Original approach :

1]原创方法:

In [536]: data = np.random.randint(11,99,(4,5))
     ...: max_shift = 5
     ...: col_start = -np.random.randint(0, max_shift, data.shape[0])
     ...: for i,row in enumerate(data):
     ...:     print np.array([np.roll(row, col_start[i])])
     ...:     
[[83 93 17 53 61]]
[[55 88 84 94 89]]
[[59 63 29 72 85]]
[[57 95 13 21 14]]

2] Proposed approach re-using col_start, so that we could do a value verification :

2]提议的方法重用col_start,以便我们可以进行值验证:

In [537]: m,n = data.shape

In [538]: idx = np.mod(-col_start[:,None] + np.arange(n), n)

In [539]: data[np.arange(m)[:,None], idx]
Out[539]: 
array([[83, 93, 17, 53, 61],
       [55, 88, 84, 94, 89],
       [59, 63, 29, 72, 85],
       [57, 95, 13, 21, 14]])