当前位置:主页 > python教程 > Python数据集打乱方法

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

发布:2023-04-21 09:00:01 59


为找教程的网友们整理了相关的编程文章,网友宰俊喆根据主题投稿了本篇教程内容,涉及到Python数据集打乱、Python数据集、Python数据集打乱方法、Python数据集打乱方法相关内容,已被161网友关注,如果对知识点想更进一步了解可以在下方电子资料中获取。

Python数据集打乱方法

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]

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持码农之家。


参考资料

相关文章

网友讨论