numpy的random模块详解

标签: python numpy random
20人阅读 评论(0) 收藏 举报
分类:

参考:
https://docs.scipy.org/doc/numpy/reference/routines.random.html
https://blog.csdn.net/akadiao/article/details/78252840?locationNum=9&fps=1
https://blog.csdn.net/kancy110/article/details/69665164

1、Simple random data

函数 解释
rand(d0, d1, …, dn) Random values in a given shape.
randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal” distribution.
randint(low[, high, size, dtype]) Return random integers from low (inclusive) to high (exclusive).
random_integers(low[, high, size]) Random integers of type np.int between low and high, inclusive.
random_sample([size]) Return random floats in the half-open interval [0.0, 1.0).
random([size]) Return random floats in the half-open interval [0.0, 1.0).
ranf([size]) Return random floats in the half-open interval [0.0, 1.0).
sample([size]) Return random floats in the half-open interval [0.0, 1.0).
choice(a[, size, replace, p]) Generates a random sample from a given 1-D array
bytes(length) Return random bytes.
1.1、numpy.random.random

参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html#numpy.random.random

功能:返回范围在半开区间[0.0, 1.0) 上的浮点数

numpy.random.random(size=None):返回范围在半开区间[0.0, 1.0) 上的浮点数

random_sample、random、ranf、sample用法相同:

random_sample_type = type(np.random.random_sample())
print(random_sample_type)

random_sample = np.random.random_sample((5))#与random_sample(5,)相同
print(random_sample)

random_sample = np.random.random_sample((5,))
print(random_sample)

random_sample = np.random.random_sample((3,2))
print(random_sample)

# Three-by-two array of random numbers from [-5, 0):
random_sample_result = 5 * np.random.random_sample((3, 2)) - 5
print(random_sample_result)

打印:

<class 'float'>

[ 0.58452144  0.17618506  0.95080302  0.66095854  0.34928887]

[ 0.39012758  0.27384807  0.30607608  0.46398196  0.88590116]

[[ 0.12028886  0.57902902]
 [ 0.87015091  0.1462187 ]
 [ 0.43734193  0.09571964]]

[[-4.94102089 -1.79261502]
 [-4.34365906 -3.16519113]
 [-1.75801587 -1.88706362]]
1.2、numpy.random.rand 生成随机浮点数

参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand

功能:返回范围在半开区间[0.0, 1.0) 上的浮点数

numpy.random.rand(d0, d1, ..., dn)

默认为生成一个随机的浮点数,范围是[0, 1),也可以通过参数size设置返回数据的size。

与numpy.random.random不同的是:numpy.random.random接收的元组,numpy.random.rand不是

random_rand_type = type(np.random.rand())
print(random_rand_type)

random_rand = np.random.rand(5)#与random_rand(5,)相同
print(random_rand)

random_rand = np.random.rand(5,)
print(random_rand)

random_rand = np.random.rand(3,2)
print(random_rand)

# Three-by-two array of random numbers from [-5, 0):
random_rand_result = 5 * np.random.rand(3, 2) - 5
print(random_rand_result)

打印:

<class 'float'>

[ 0.26487271  0.0281932   0.8042671   0.2643821   0.33199909]

[ 0.91690691  0.3412498   0.11569359  0.53687716  0.19945599]

[[ 0.09106275  0.64573293]
 [ 0.04541494  0.04964684]
 [ 0.24620085  0.81908924]]

[[-1.70500552 -1.19790261]
 [-2.56920771 -1.32168807]
 [-4.77324942 -2.59249556]]

1.3、numpy.random.randint 产生随机整数

参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html#numpy.random.randint

numpy.random.randint(low, high=None, size=None, dtype='l')

1、随机生成[low,high)范围内的整数

random_randint = np.random.randint(3, 6, size=10)
print(random_randint)

打印:

[3 4 4 3 4 3 4 5 5 3]#说明3在范围内,而6不在范围内

2、指定size
size : 是int型 或者 是int型元组 , 默认是None返回单个整数

random_randint = np.random.randint(3, 10)
print(random_randint)
random_randint = np.random.randint(3, 10, size=10)
print(random_randint)
random_randint = np.random.randint(3, 10, size=(2, 5))
print(random_randint)
random_randint = np.random.randint(3, 10, size=(2, 2, 5))
print(random_randint)

打印:

9

[4 3 3 4 5 5 5 6 6 7]

[[3 3 4 3 8]
 [7 6 4 6 9]]

[[[6 3 6 3 5]
  [5 5 5 5 7]]
 [[9 5 8 8 4]
  [7 8 3 9 6]]]

3、 high 为None 时
那么结果的范围为 [0, low).

random_randint = np.random.randint(3)
print(random_randint)

random_randint = np.random.randint(3,size=10)
print(random_randint)

打印:

2
[2 2 0 0 1 0 1 0 0 1]

1.4、numpy.random.random_integers

参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random_integers.html#numpy.random.random_integers

numpy.random.random_integers(low, high=None, size=None)

生成一个整数或一个N维整数数组,取值范围:若high不为None,则取[low,high]之间随机整数,否则取[1,low]之间随机整数。

用法与random.randint相似,不同的是取值区间为闭区间[low, high],并且high为None时取值范围为范围为 [1, low].

原文:similar to randint, only for the closed interval [low, high], and 1 is the lowest value if high is omitted. In particular, this other one is the one to use to generate uniformly distributed discrete non-integers.

random_integers = np.random.random_integers(3, 6, size=10)
print(random_integers)

random_integers = np.random.random_integers(3,size=10)
print(random_integers)

打印:

[4 4 6 3 6 6 3 3 6 4]
[2 1 2 2 2 3 3 2 1 2]
1.5、numpy.random.randn

numpy.random.randn(d0, d1, …, dn):生成一个浮点数或N维浮点数组,取数范围:正态分布的随机样本数。

randn = np.random.randn()
print(randn)

打印:

1.387157144507402

使用公式:sigmanp.random.randn(...)+muN(μ,σ2)获取样本

#Two-by-four array of samples from N(3, 6.25):
randn = 2.5 * np.random.randn(2, 4) + 3
print(randn)

打印:

[[ 6.03858058  4.47342334 -1.37679171  2.20495446]
 [ 5.70048472  1.28674501 -1.06387771  3.38788724]]
查看评论

numpy的random模块中的几个常用函数

详细的random模块中的其他函数:http://www.mamicode.com/info-detail-507676.html rand(d0, d1, …, dn),输入几个参数返回几维的数组,...
  • qq_30159351
  • qq_30159351
  • 2016-09-03 22:04:26
  • 1164

python中numpy的random模块

numpy的random模块里面有好几个生成随机数的函数,总是记不清,今天整理一下。 numpy.random 1. rand(d0,d1,.....,dn) 产生[0,1]的浮点随机数,括号里面...
  • liu450016702
  • liu450016702
  • 2017-03-22 11:07:52
  • 1171

python中的random模块中的randint模块和numpy中random模块的randint的区别

今天从网上down了一个代码下来看看,结果发现运行结果不一样,最后才发现是随机数的问题。在这里Mark一下、 在Python中,通过import random,然后调用random.randomin...
  • xiongchengluo1129
  • xiongchengluo1129
  • 2018-01-16 21:24:03
  • 300

numpy中的随机数模块

numpy.random模块中提供啦大量的随机数相关的函数。 1 numpy中产生随机数的方法   1)rand()   产生[0,1]的浮点随机数,括号里面的参数可以指定产生数组的形状 ...
  • IAlexanderI
  • IAlexanderI
  • 2017-10-26 19:23:13
  • 535

Python3.5——内置模块详解之random模块

1、random模块基础的方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import random ...
  • loveliuzz
  • loveliuzz
  • 2017-09-28 20:21:00
  • 770

numpy的random模块

转自http://www.mamicode.com/info-detail-507676.html 排列 shuffle(x) 现场修改序列,改变自身内容。(类似洗牌...
  • qtlyx
  • qtlyx
  • 2016-02-24 20:07:26
  • 4305

numpy: np.random模块 探究(源码)

官方api定义From Random sampling: Random sampling (numpy.random) Simple random data rand(d0, d1, …...
  • JNingWei
  • JNingWei
  • 2017-09-01 14:45:15
  • 542

python numpy.random详细解析

随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) ...
  • vicdd
  • vicdd
  • 2016-09-26 10:03:37
  • 14046

random VS numpy.random

给随机生成器设置seed的目的是每次运行程序得到的随机数的值相同,这样方便测试。numpy.random.seed()不是线程安全的,如果程序中有多个线程最好使用numpy.random.Random...
  • zm714981790
  • zm714981790
  • 2017-03-10 12:12:00
  • 1385

Python和numpy 中random 用法

Python random 通用格式                    np.random.randint(0,10,size=3)  #等价于 np.random.randint(0,10,3...
  • luoganttcc
  • luoganttcc
  • 2017-02-22 12:28:23
  • 3205
    个人资料
    持之以恒
    等级:
    访问量: 4万+
    积分: 1850
    排名: 2万+
    最新评论