randint(随机整数)

Return random integers from low (inclusive) to high (exclusive).

从区间 [low, high) 中返回随机的整数数组

  • 参数
    1. low:最小值
    2. high:最大值,默认为 None
    3. size:个数,默认为None
    4. dtype:数据类型,默认为’l’
  • 返回值
    • out:整型或者整型数组

一些简单的🌰

  1. 0 到 50 中随机一个数
1
2
np.random.randint(50)
# 26
  1. 10 到 50 中随机一个数
1
2
np.random.randint(10, 50)
# 36
  1. 0 到 50 中随机 10 个数
1
2
np.random.randint(50, size=10)
# array([10, 25, 37, 34, 13, 15, 16, 12, 44, 4])
  1. 10 到 50 中随机 10 个数
1
2
np.random.randint(10, 50, 10)
# array([40, 46, 27, 44, 30, 46, 15, 41, 18, 23])

random/random_sample(随机 0-1 的浮点数)

Return random floats in the half-open interval [0.0, 1.0).

返回一个在 0-1 之间的随机的浮点数的数组

  • 参数
    1. size:大小,默认为 None
  • 返回值
    • out:浮点数或浮点数数组

一些简单的🌰

  1. 随机返回一个浮点数
1
np.random.random()     # 0.939393746883071
  1. 随机返回有一个浮点数的数组
1
np.random.random(1)    # array([0.70345759])
  1. 随机返回 10 个浮点数的数组
1
2
3
np.random.random(10)
# array([0.84469124, 0.16361142, 0.59353651, 0.80872106, 0.15086249,
# 0.4595056 , 0.5550403 , 0.59146936, 0.07822706, 0.99842643])

rand(随机 0-1 给定形状的浮点数)

Random values in a given shape.

按照给定的形状返回数据(值为[0, 1) )

  • 参数
    • d0, d1, …, dn:形状信息,默认为 None,即可以不传数据
  • 返回值
    • out:ndarray

一些简单的🌰

  1. 不传数据
1
2
np.random.rand()
# 0.4506933261729883
  1. 返回一个 2 x 2的随机数组
1
2
3
np.random.rand(2, 2)
# array([[0.67949434, 0.73498025],
# [0.21896692, 0.54682848]])

randn(标准正态分布中随机样本)

Return a sample (or samples) from the “standard normal” distribution.

从标准正态分布返回一个或多个样本

  • 参数:
    1. d0, d1, …, dn:形状信息
  • 返回值:
    • Z:浮点型或者浮点型数组

一些简单的🌰

  1. 不传数据
1
np.random.randn() # 1.5567818373271456
  1. 返回 2 x 5 的标准正态分布样本
1
2
3
np.random.randn(2, 5)
# array([[ 1.15218971, -0.19590126, 0.10391749, -0.46857231, 0.58370011],
# [-0.29639999, -0.73765977, -0.03441631, 0.26545265, 0.25255167]])

normal(正态分布中随机样本)

Draw random samples from a normal (Gaussian) distribution.

从正态分布中抽取随机样本,默认返回标准正态分布

  • 参数
    1. loc:期望值,默认为 0
    2. scale:标准差,默认为 1
    3. size:大小,默认为 None
  • 返回值
    • out:浮点型或者浮点型数组

一些简单的🌰

  1. 不传参数,默认为标准正态分布
1
2
np.random.normal()    # 0.39605193328763477

  1. 期望值为 1,标准差为 2 的正态分布
1
2
np.random.normal(1, 2)    # 0.9208239350120327

  1. 获取 10 个期望值为 5,标准差为 2 的正态分布的值
1
2
3
4
np.random.normal(5, 2, 10)
# array([5.66208783, 3.63723912, 4.4776973 , 5.20380349, 6.0451376 ,
# 4.53452163, 2.34042043, 4.43470636, 6.75530746, 1.95167798])

choice(从给定数据中随机样本)

Generates a random sample from a given 1-D array

从给定的一维数组中,生成随机样本

  • 参数:
    1. a:随机的参数
      • 如果是 ndarray,则从中随机一个数字
      • 如果是 int,则从 np.arange(a) 中随机一个数组
    2. size:大小,默认为 None
    3. replace:是否要放回取样,默认为 True
    4. p:随机选取对应索引的概率
  • 返回值:
    • samples:single item or ndarray

一些简单的🌰

  1. 从 0-9 中随机选择一个元素
1
np.random.choice(10) # 2
  1. 从 0-9 中不放回取样 10 个数据
1
2
np.random.choice(10, 10, False)
# array([8, 9, 5, 0, 1, 4, 3, 6, 7, 2])
  1. 从0 - 1中取样 10 个数据,0 的取样概率为 0.8,1 的取样概率为 0.2
1
2
np.random.choice(2, 10, p=[0.8, 0.2])
# array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1])

uniform(均匀分布抽取)

Draw samples from a uniform distribution.

从均匀分布中抽取样本

  • 参数
    1. low:下界,默认为 0
    2. high:上界,默认为 1
    3. size:大小,默认为 None
  • 返回值
    • out:ndarray or scalar

一些简单的🌰

  1. 不传参数,默认 [0, 1) 的均匀分布
1
np.random.uniform() # 0.237545651484531
  1. 获取[2, 10)中均匀分布的一个样本
1
np.random.uniform(2, 10) # 3.751506848826293

数据打散

permutation(不修改原数据)

Randomly permute a sequence, or return a permuted range.

随机排序一个序列,返回排序的结果

  • 参数
    1. x:同 choice 中的 a
  • 返回值
    • out:ndarray
1
2
np.random.permutation(10)
# array([7, 1, 0, 6, 9, 5, 3, 8, 4, 2])

shuffle(修改原数据)

Modify a sequence in-place by shuffling its contents.

原地修改一个数组的顺序

  • 参数
    • x
1
2
3
a = np.arange(10)
np.random.shuffle(a)
a # array([4, 3, 9, 5, 6, 1, 8, 0, 2, 7])

随机种子

seed

seed一旦定下来之后,如果是固定的 seed,则每次随机的结果都是一样的。

注意:seed 的有效次数仅为一次

1
2
3
np.random.seed(2)
np.random.randint(100)
# 40

评论