python 利用numpy同时打乱列表的顺序,同时打乱数据和标签的顺序

tech2023-06-29  124

转载自: https://www.cnblogs.com/LiuXinyu12378/p/12390055.html

方法一: np.random.shuffle (无返回值,直接打乱原列表)

train = np.array([1,2,3,4,5]) label = np.array([0,1,2,3,4]) state = np.random.get_state() np.random.shuffle(train) np.random.set_state(state) np.random.shuffle(label) print(train) print(label) 结果: [5 4 1 2 3] [4 3 0 1 2]

方法二:返回一个打乱的序列,可将其用于以同一顺序打乱不同列表

shuffle_ix = np.random.permutation(np.arange(len(train))) train = train[shuffle_ix] label = label[shuffle_ix] print(train) print(label) 结果: [2 3 1 4 5] [1 2 0 3 4]
最新回复(0)