方法一:(推荐)
此方法直接传入两个数据一起打乱。
import numpy as np
from sklearn.utils import shuffle
x = np.array([[1., 0.],
[2., 1.],
[0., 0.]])
y = np.array([0, 1, 2])
x, y = shuffle(x, y, random_state=0)
print(x)
print(y)
# 输出
[[0. 0.]
[2. 1.]
[1. 0.]]
[2 1 0]
方法二:
该方法复杂些。
import numpy as np
x = np.array([[1., 0.],
[2., 1.],
[0., 0.]])
y = np.array([0, 1, 2])
state = np.random.get_state()
np.random.shuffle(x)
np.random.set_state(state)
np.random.shuffle(y)
print(x)
print(y)
# 输出
[[1. 0.]
[0. 0.]
[2. 1.]]
[0 2 1]
方法三:
该方法复杂些。
import numpy as np
x = np.array([[1., 0.],
[2., 1.],
[0., 0.]])
y = np.array([0, 1, 2])
shuffle_ix = np.random.permutation(np.arange(len(x)))
x = x[shuffle_ix]
y = y[shuffle_ix]
print(x)
print(y)
# 输出
[[0. 0.]
[2. 1.]
[1. 0.]]
[2 1 0]