3 回答
TA贡献2003条经验 获得超2个赞
您可以随机化它们的索引。
import numpy as np
# before randomizing:
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([1,2,3])
# randomize indexes
n = np.size(a,axis=0)
new_index = np.random.choice(n, size=n, replace=False)
# after randomizing (you may want a copy of them instead of a view):
a2 = a[new_index].copy()
b2 = b[new_index].copy()
TA贡献1848条经验 获得超2个赞
使用随机生成器seed
在每次操作之前应用相同的方法shuffle
。
import numpy as np
a = np.array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]])
b = np.array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]])
c = np.array([1, 2, 3])
SEED = 123456789
rng = np.random.default_rng(SEED)
rng.shuffle(a,axis=0)
rng = np.random.default_rng(SEED)
rng.shuffle(b,axis=0)
rng = np.random.default_rng(SEED)
rng.shuffle(c,axis=0)
a、b 和 c 的输出
(array([[2, 3, 4, 5],
[3, 4, 5, 6],
[1, 2, 3, 4]]),
array([[2, 3, 4, 5],
[3, 4, 5, 6],
[1, 2, 3, 4]]),
array([2, 3, 1]))
TA贡献1812条经验 获得超5个赞
一种方法是创建一个索引数组,对其进行打乱,然后按它排列其他数组:
size = len(a)
indexes = np.arange(size)
new_a = np.empty(size)
new_b = np.empty(size)
new_b = np.empty(size)
for i in range(size):
new_a[i] = a[indexes[i]]
new_b[i] = b[indexes[i]]
new_c[i] = c[indexes[i]]
添加回答
举报