3 回答

TA貢獻2003條經(jīng)驗 獲得超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條經(jīng)驗 獲得超2個贊
使用隨機生成器seed
在每次操作之前應(yīng)用相同的方法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條經(jīng)驗 獲得超5個贊
一種方法是創(chuàng)建一個索引數(shù)組,對其進行打亂,然后按它排列其他數(shù)組:
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]]
添加回答
舉報