【numpy】np.random

【numpy】np.random_第1张图片

【numpy】np.random_第2张图片


1)np.random.uniform(low, high, size)

生成 [low,high) 之间的随机浮点数,随机的概率是均匀的

参数:

  • low:最小值(能取到)
  • high:最大值(取不到)
  • size:默认为None,表示生成随机数的形状,不传参数时,只返回一个随机数
import numpy as np

np.random.seed(100)

a = np.random.uniform(1, 6)
print(a)    # 3.717024708954827

b = np.random.uniform(1, 6, [3, 2])
print(b)
# [[2.39184693 3.12258795]
#  [5.22388066 1.02359428]
#  [1.6078456  4.35374542]]

2)np.random.randn(d0, d1, …, dn)

生成标准正态分布的随机数,等于 np.random.normal(mean=0, stddev=1, size)

import numpy as np

np.random.seed(100)

a = np.random.randn(2, 3)
print(a)
# [[-1.74976547  0.3426804   1.1530358 ]
#  [-0.25243604  0.98132079  0.51421884]]

b = np.random.randn(3, 2)
print(b)
# [[ 0.22117967 -1.07004333]
#  [-0.18949583  0.25500144]
#  [-0.45802699  0.43516349]]

3)np.random.normal(loc=mean, scale=stddev, size)

生成 均值为mean,标准差为stddev的正态分布 的随机数

import numpy as np

np.random.seed(100)

a = np.random.normal(5, 1)
print(a)

b = np.random.normal(loc=5, scale=1, size=[3, 2])
print(b)

4)np.random.randint(low, high, size)

import numpy as np

np.random.seed(100)

a = np.random.randint(1, 6)
print(a)  
# 1

b = np.random.randint(1, 7, [3, 2])
print(b)
# [[1 4]
#  [1 3]
#  [5 3]]

5)np.random.shuffle(ndarray)

import numpy as np

np.random.seed(100)

a = np.random.normal(5, 1, [3, 2])
print(a)
# [[3.25023453 5.3426804 ]
#  [6.1530358  4.74756396]
#  [5.98132079 5.51421884]]

np.random.shuffle(a)
print(a)

你可能感兴趣的:(#,python,#,pytorch,numpy,python,机器学习)