如何在python中随机打乱数据和目标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35076223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to randomly shuffle data and target in python?
提问by Demonedge
I have a 4D array training images, whose dimensions correspond to (image_number,channels,width,height). I also have a 2D target labels,whose dimensions correspond to (image_number,class_number). When training, I want to randomly shuffle the data by using random.shuffle, but how can I keep the labels shuffled by the same order of my images? Thx!
我有一个 4D 数组训练图像,其尺寸对应于 (image_number,channels,width,height)。我还有一个 2D 目标标签,其尺寸对应于 (image_number,class_number)。训练时,我想使用 random.shuffle 随机打乱数据,但是如何使标签按图像的相同顺序打乱?谢谢!
采纳答案by Randy
If you want a numpy-only solution, you can just reindex the second array on the first, assuming you've got the same image numbers in both:
如果你想要一个 numpy-only 解决方案,你可以在第一个数组上重新索引第二个数组,假设你在两个数组中都有相同的图像编号:
In [67]: train = np.arange(20).reshape(4,5).T
In [68]: target = np.hstack([np.arange(5).reshape(5,1), np.arange(100, 105).reshape(5,1)])
In [69]: train
Out[69]:
array([[ 0, 5, 10, 15],
[ 1, 6, 11, 16],
[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19]])
In [70]: target
Out[70]:
array([[ 0, 100],
[ 1, 101],
[ 2, 102],
[ 3, 103],
[ 4, 104]])
In [71]: np.random.shuffle(train)
In [72]: target[train[:,0]]
Out[72]:
array([[ 2, 102],
[ 3, 103],
[ 1, 101],
[ 4, 104],
[ 0, 100]])
In [73]: train
Out[73]:
array([[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 1, 6, 11, 16],
[ 4, 9, 14, 19],
[ 0, 5, 10, 15]])
回答by Pear666
Depending on what you want to do, you could also randomly generate a number for each dimension of your array with
根据您想要做什么,您还可以为数组的每个维度随机生成一个数字
random.randint(a, b) #a and b are the extremes of your array
which would select randomly amongst your objects.
这将在您的对象中随机选择。
回答by sv_jan5
There is another easy way to do that. Let us suppose that there are total N
images. Then we can do the following:
还有另一种简单的方法可以做到这一点。让我们假设有总N
图像。然后我们可以执行以下操作:
from random import shuffle
ind_list = [i for i in range(N)]
shuffle(ind_list)
train_new = train[ind_list, :,:,:]
target_new = target[ind_list,]
回答by Foreever
from sklearn.utils import shuffle
import numpy as np
X = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])
y = np.array([0, 1, 2, 3, 4])
X, y = shuffle(X, y)
print(X)
print(y)
[[1 1 1]
[3 3 3]
[0 0 0]
[2 2 2]
[4 4 4]]
[1 3 0 2 4]