标签:
【说明】
翻译自官网的文档。
随机抽样 (numpy.random)
简单的随机数据
| rand(d0, d1, …, dn) |
随机值 >>> np.random.rand(3,2)
array([[ 0.14022471, 0.96360618], #random
[ 0.37601032, 0.25528411], #random
[ 0.49313049, 0.94909878]]) #random
|
| randn(d0, d1, …, dn) |
返回一个样本,具有标准正态分布。 Notes For random samples from sigma * np.random.randn(...) + mu Examples >>> np.random.randn() 2.1923875335537315 #random Two-by-four array of samples from N(3, 6.25): >>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random
|
| randint(low[, high, size]) |
返回随机的整数,位于半开区间 [low, high)。 >>> np.random.randint(2, size=10) array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) >>> np.random.randint(1, size=10) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) Generate a 2 x 4 array of ints between 0 and 4, inclusive: >>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
|
| random_integers(low[, high, size]) |
返回随机的整数,位于闭区间 [low, high]。 Notes To sample from N evenly spaced floating-point numbers between a and b, use: a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.) Examples >>> np.random.random_integers(5)
4
>>> type(np.random.random_integers(5))
<type ‘int‘>
>>> np.random.random_integers(5, size=(3.,2.))
array([[5, 4],
[3, 3],
[4, 5]])
Choose five random numbers from the set of five evenly-spaced numbers between 0 and 2.5, inclusive (i.e., from the set >>> 2.5 * (np.random.random_integers(5, size=(5,)) - 1) / 4. array([ 0.625, 1.25 , 0.625, 0.625, 2.5 ]) Roll two six sided dice 1000 times and sum the results: >>> d1 = np.random.random_integers(1, 6, 1000) >>> d2 = np.random.random_integers(1, 6, 1000) >>> dsums = d1 + d2 Display results as a histogram: >>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(dsums, 11, normed=True) >>> plt.show()
|
| random_sample([size]) |
返回随机的浮点数,在半开区间 [0.0, 1.0)。 To sample (b - a) * random_sample() + a Examples >>> np.random.random_sample() 0.47108547995356098 >>> type(np.random.random_sample()) <type ‘float‘> >>> np |

本文翻译自官方文档,详细介绍了numpy.random模块,包括简单的随机数据生成、数组的随机排列、各种概率分布的随机数以及随机数生成器的使用,如RandomState的seed、get_state和set_state等方法。
, use:
):
multiply the output of
最低0.47元/天 解锁文章
9152

被折叠的 条评论
为什么被折叠?



