numpy中的随机打乱数据方法np.random.shuffle解读

作者:是康康啊 时间:2023-05-19 08:26:53 

numpy随机打乱数据方法np.random.shuffle

import numpy as np
#实验可得每次shuffle后数据都被打乱,这个方法可以在机器学习训练
#的时候在每个epoch结束后将数据重新洗牌进入下一个epoch的学习
num = np.arange(20)
print(num)
np.random.shuffle(num)
print(num)
num1 = np.arange(20)
print(num1)
np.random.shuffle(num1)
print(num1)
np.random.shuffle(num1)
print(num1)

#打印输出:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[ 1  5 19  9 14  2 12  3  6 18  4  8 16  0 10 17 13  7 15 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[ 2  4 13 14 11 17  9 19  5 12 15  7 18 16  3 10  1  8  0  6]
[ 8 11 13  6 19  7  9 12  4  3 10 14 15  2  1  0 17 18 16  5]

numpy随机生成数据问题

用numpy.random模块来生成随机数组

1.np.random.rand 用于生成[0.0, 1.0)之间的随机浮点数, 当没有参数时,返回一个随机浮点数,当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数建议是整数型。

import numpy as np
np.random.rand(8)

output [ 0.55958421 0.97358761 0.77753246 0.28072869 0.18467794 0.85755336
0.03976048 0.08161713]

2、np.random.randn这个函数返回一个样本,具有标准正态分布。

np.random.randn(8)

output [ 0.5512808 1.32780895 -0.95738756 0.93710414 -2.0854875 -0.5100787 n 0.40982079 -1.235186 ]

3、np.random.randint(low[, high, size]) 返回随机的整数,位于半开区间 [low, high)。

np.random.randint(10,size=10)

output [3 3 8 7 3 2 6 2 3 6]

4、random_integers(low[, high, size]) 返回随机的整数,位于闭区间 [low, high]。

np.random.random_integers(5)

output = 4

5、 np.random.shuffle(x) 类似洗牌,打乱顺序;np.random.permutation(x)返回一个随机排列.

arr = np.arange(10)
np.random.shuffle(arr)
print(arr)
np.random.permutation(10)

output[1 7 5 2 9 4 3 6 0 8 ]
array([1,7,4,3,0,9,2,5,8,6])

用random模块自己构造

1、random.randint(low, hight) -> 返回一个位于[low,hight]之间的整数。

该函数接受两个参数,这两个参数必须是整数(或者小数位是0的浮点数),并且第一个参数必须不大于第二个参数

import random
random.randint(1,10)
random.randint(1.0,10.0)

2、random.random() -> 不接受参数,返回一个[0.0, 1.0)之间的浮点数

random.random()

3、random.randrange(start, stop, step) -> 返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),若start大于stop时 ,setp必须为负数.step不能是0

random.randrange(1,100,2)  #返回[1,100]之间的奇数
random.randrange(100,1,-2)  #返回[100,1]之间的偶数

来源:https://blog.csdn.net/weixin_43896259/article/details/106116955

标签:numpy,打乱数据,np.random.shuffle
0
投稿

猜你喜欢

  • 新Orcas语言特性:扩展方法

    2007-09-23 12:49:00
  • PL/SQL数据类型及操作符

    2009-02-26 11:17:00
  • 超级简单实现框架滚动控制

    2008-07-01 12:14:00
  • 关于shopex同步ucenter的redirect问题,导致script不运行

    2023-07-13 05:20:53
  • 实用PHP会员权限控制实现原理分析

    2023-11-23 11:32:39
  • 将mysql转换到oracle必须了解的50件事

    2010-07-05 12:15:00
  • Python快速实现一键抠图功能的全过程

    2021-03-03 14:58:39
  • python数据分析之公交IC卡刷卡分析

    2022-02-10 02:23:56
  • scrapy处理python爬虫调度详解

    2021-09-10 11:30:00
  • linux安装Python3.4.2的操作方法

    2022-06-17 19:19:15
  • 详解Golang语言HTTP客户端实践

    2023-09-17 13:52:07
  • JS 中触发 A 标签的点击事件

    2009-01-11 17:30:00
  • 很酷的JQuery Solar System

    2007-12-15 08:09:00
  • HTML5 的五个激动人心的特性

    2009-01-02 17:36:00
  • Js实现仿msn的右下角popup提示窗口

    2007-12-27 20:30:00
  • 通过作业调度建立SQL Server的自动备份

    2008-12-09 14:58:00
  • JavaScript Table行定位效果

    2009-05-25 10:47:00
  • Python图像处理之图像的灰度线性变换

    2021-12-16 22:30:58
  • 使用Python处理json字符串中的非法双引号问题

    2021-01-19 19:26:13
  • PHP实现批量生成App各种尺寸Logo

    2023-07-23 03:59:59
  • asp之家 网络编程 m.aspxhome.com