如何洗牌两个 numpy 数组,以便记录索引在洗牌后保持对齐?
How to shuffle two numpy arrays, so that record indices are stay aligned in both after shuffling?
我有一个 4 维数组和一个一维数组:
import numpy as np
data = np.random.randn(10, 1, 5, 5) # num_records, depth, height, width
labels = np.array([1,1,1,1,1,0,0,0,0,0])
我想按 num_records
打乱数据和标签,以获得 labels
的随机顺序。
我知道可以使用 shuffle
函数:np.random.shuffle(data)
。但是我不知道如何在洗牌后保持 data
和 labels
之间的关系。
这会将两个数组一起洗牌:
import numpy as np
data = np.random.randn(10, 1, 5, 5) # num_records, depth, height, width
labels = np.array([1,1,1,1,1,0,0,0,0,0])
# shuffle indices
idx = np.random.permutation(range(len(labels)))
# shuffle together
data, labels = data[idx,:,:,:], labels[idx]
import sklearn.utils as sku
array1_shuffled, array2_shuffled = sku.shuffle(array1, array2)
https://www.kite.com/python/answers/how-to-shuffle-two-numpy-arrays-in-unision-in-python
我有一个 4 维数组和一个一维数组:
import numpy as np
data = np.random.randn(10, 1, 5, 5) # num_records, depth, height, width
labels = np.array([1,1,1,1,1,0,0,0,0,0])
我想按 num_records
打乱数据和标签,以获得 labels
的随机顺序。
我知道可以使用 shuffle
函数:np.random.shuffle(data)
。但是我不知道如何在洗牌后保持 data
和 labels
之间的关系。
这会将两个数组一起洗牌:
import numpy as np
data = np.random.randn(10, 1, 5, 5) # num_records, depth, height, width
labels = np.array([1,1,1,1,1,0,0,0,0,0])
# shuffle indices
idx = np.random.permutation(range(len(labels)))
# shuffle together
data, labels = data[idx,:,:,:], labels[idx]
import sklearn.utils as sku
array1_shuffled, array2_shuffled = sku.shuffle(array1, array2)
https://www.kite.com/python/answers/how-to-shuffle-two-numpy-arrays-in-unision-in-python