Python numpy数组中某些行的无序排序
我只想洗牌numpy数组中某些行的顺序。这些行始终是连续的(例如,洗牌行23-80)。每行中的元素数可以从1(因此数组实际上是1D)到100不等 下面是示例代码,演示我如何看待方法Python numpy数组中某些行的无序排序,python,arrays,numpy,shuffle,Python,Arrays,Numpy,Shuffle,我只想洗牌numpy数组中某些行的顺序。这些行始终是连续的(例如,洗牌行23-80)。每行中的元素数可以从1(因此数组实际上是1D)到100不等 下面是示例代码,演示我如何看待方法shuffle\u rows()的工作原理。我将如何设计这样一种方法来有效地进行这种洗牌 import numpy as np >>> a = np.arange(20).reshape(4, 5) >>> a array([[ 0, 1, 2, 3, 4],
shuffle\u rows()
的工作原理。我将如何设计这样一种方法来有效地进行这种洗牌
import numpy as np
>>> a = np.arange(20).reshape(4, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
>>> shuffle_rows(a, [1, 3]) # including rows 1, 2 and 3 in the shuffling
array([[ 0, 1, 2, 3, 4],
[15, 16, 17, 18, 19],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
你可以用。这将洗牌行本身,而不是行中的元素
从:
此函数仅沿多维数组的第一个索引洗牌数组
例如:
import numpy as np
def shuffle_rows(arr,rows):
np.random.shuffle(arr[rows[0]:rows[1]+1])
a = np.arange(20).reshape(4, 5)
print(a)
# array([[ 0, 1, 2, 3, 4],
# [ 5, 6, 7, 8, 9],
# [10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19]])
shuffle_rows(a,[1,3])
print(a)
#array([[ 0, 1, 2, 3, 4],
# [10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19],
# [ 5, 6, 7, 8, 9]])
shuffle_rows(a,[1,3])
print(a)
#array([[ 0, 1, 2, 3, 4],
# [10, 11, 12, 13, 14],
# [ 5, 6, 7, 8, 9],
# [15, 16, 17, 18, 19]])
如果我在Python3中尝试打印(shuffle_rows(a[1,3]),我在输出中得到“None”?shuffle在适当的位置完成<代码>打印(a)您将看到无序的rowsAh,这很有意义。谢谢你的澄清!