湖上湖
您可以随机化它们的索引。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 indexesn = 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()
慕尼黑5688855
使用随机生成器seed在每次操作之前应用相同的方法shuffle。import numpy as npa = 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 = 123456789rng = 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]))