学无先后,达者为师

网站首页 编程语言 正文

Python中的常见数据集打乱方法_python

作者:starky0729   更新时间: 2023-04-02 编程语言

python常见的数据集打乱方法

第一种方法

通过index 

x_train, y_train=train_load()

index = [i for i in range(len(x_train))]

np.random.shuffle(index)

x_train= x_train[index]

y_train = y_train[index]

第二种方法

zip()+shuffle()方法

x_train, y_train=train_load()
result = list(zip(x_train, y_train))  # 打乱的索引序列
np.random.shuffle(result)
x_train,y_train = zip(*result)

第三种方法

seed()+shuffle

x_batch, y_batch = train_load()
#加载我所有的数据,这里想x_batch,Y_batch是list的格式,要注意

seed=100
random.seed(seed)
random.shuffle(x_batch)
random.seed(seed)#一定得重复在写一遍,和上面的seed要相同,不然y_batch和x_batch打乱顺序会不一样
random.shuffle(y_batch)

PS:numpy中函数shuffle与permutation都是对原来的数组随机打乱原来的顺序,shuffle中文含义为洗牌,permutation中文含义为排列,区别在于shuffle直接在原来的数组上进行操作,改变原来数组的顺序,无返回值。

而permutation不直接在原来的数组上进行操作,而是返回一个新的打乱顺序的数组,并不改变原来的数组。

python手动打乱数据集

x_train, y_train = np.array(x_train),np.array(y_train)
index = [i for i in range(len(y_train))]
np.random.shuffle(index)
x_train = x_train[index]
y_train = y_train[index]

总结

原文链接:https://blog.csdn.net/weixin_40964777/article/details/100050263

栏目分类
最近更新