品易云推流 关闭
文章详情页
文章 > Python常见问题 > 在python中如何打乱数据?

在python中如何打乱数据?

python常见问题

头像

宋雪维

2020-12-03 17:43:585346浏览 · 0收藏 · 0评论

在玩python学习机器时,对于那种对随机性不太敏感的模型,理论上说可以不打乱。但敏感不敏感也跟数据量级,复杂度,算法内部计算机制都有关,目前并没有一个经纬分明的算法随机度敏感度列表。既然打乱数据并不会得到一个更差的结果,一般推荐的做法就是打乱全量数据。那怎么打乱呢?今天小编就教大家在python中打乱数据集和标签,来看看吧。

方法一、打乱索引Index

import numpy as np

index = [i for i in range(len(test_data))] # test_data为测试数据

np.random.shuffle(index) # 打乱索引

test_data = test_data[index]

test_label = test_label[index]

方法二:通过数组来shuffle来打乱

image_list=[]           # list of images label_list=[]           # list of labels temp = np.array([image_list, label_list]) temp = temp.transpose() np.random.shuffle(temp) images = temp[:, 0]     # array of images   (N,) labels = temp[:, 1]

方法三:通过随机数打乱

import numpy as np

np.random.seed(12)

np.random.shuffle(test_data)

np.random.seed(12)

np.random.shuffle(test_label)

以上就是小编整理的用python打乱数据的方法,如果你在玩python机器学习的话,可以采取以上方法打乱数据哦~更多学习推荐:python学习网。














关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图