• 技术文章 >常见问题 >Python常见问题

    在python中如何打乱数据?

    宋雪维宋雪维2020-12-03 17:43:58原创5166

    在玩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学习网。














    专题推荐:python常见问题
    品易云
    上一篇:python中的shuffle怎么用? 下一篇:如何用python发送邮件给多人?

    相关文章推荐

    • python入门:int()• 如何使用python3代码输出嵌套式对象?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网