numpy.random的常用接口

2020年7月10日 / 6次阅读 / Last Modified 2020年7月10日
NumPy

本文给自己总结一下numpy.random模块的常用接口,记录的过程就是学习,记录也是为了以后随时查阅。

rand

Create an array of the given shape and populate it with
random samples from a uniform distribution
over [0, 1).

>>> np.random.rand(2,3)
array([[0.13604767, 0.54706518, 0.94340186],
       [0.10566913, 0.90031032, 0.23057024]])
>>> np.random.rand(2,3,4)
array([[[0.34165656, 0.7698457 , 0.6392361 , 0.9513174 ],
        [0.78345945, 0.91428761, 0.68014409, 0.80150022],
        [0.83923091, 0.60756419, 0.89181402, 0.77862488]],

       [[0.01192324, 0.169277  , 0.48357421, 0.01000297],
        [0.25484512, 0.47667183, 0.26410589, 0.10054143],
        [0.04791362, 0.69730833, 0.13431676, 0.99415972]]])

>>> np.random.rand()
0.8424794862687751
>>> np.random.rand()
0.7097437358148481
>>> np.random.rand()
0.1610060488696904

0到1之间的uniform分布的随机数。

randn

If positive int_like arguments are provided, randn generates an array
of shape (d0, d1, ..., dn), filled
with random floats sampled from a univariate "normal" (Gaussian)
distribution of mean 0 and variance 1. A single float randomly sampled
from the distribution is returned if no argument is provided.

>>> np.random.randn()
0.5764392759886351
>>> np.random.randn()
-0.33175506587689274
>>> np.random.randn()
1.7097877445150522
>>>
>>> np.random.randn(2,3)
array([[-0.55428707,  1.14566589,  1.87706945],
       [-1.02006852, -0.88680206,  0.80531686]])
>>> np.random.randn(2,3,4)
array([[[ 0.72899078,  1.69401091, -0.84856813, -0.23470369],
        [ 0.4531826 , -1.1331611 ,  0.36746101,  1.27162636],
        [ 0.44054895,  0.47382325,  0.19433531,  0.31796577]],

       [[ 0.46967856, -0.51194763,  1.57272016,  0.56488933],
        [-1.09155081,  0.06674095,  0.33853473,  0.0415777 ],
        [-0.34024221,  0.39184704,  0.5675626 , -0.11597493]]])

n就是normal distribution的意思,符合标准正态分布的随机数。

randint

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

Return random integers from the "discrete uniform" distribution of
the specified dtype in the "half-open" interval [`low`, `high`). If
`high` is None (the default), then results are from [0, `low`).
>>> np.random.randint(2)
1
>>> np.random.randint(2)
0
>>> np.random.randint(2)
1
>>> np.random.randint(2)
1
>>> np.random.randint(2)
1
>>>
>>> np.random.randint(2,3)
2
>>> np.random.randint(2,3,size=10)
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
>>> np.random.randint(2,8,size=10)
array([4, 7, 2, 3, 4, 3, 4, 6, 2, 7])

这个也是uniform分布的。

random

Return random floats in the half-open interval [0.0, 1.0). Alias for
random_sample to ease forward-porting to the new random API.

>>> np.random.random()
0.8703315150164825
>>> np.random.random()
0.781580990742874
>>> np.random.random()
0.18435181621480878
>>> np.random.random(size=10)
array([0.39093171, 0.2394987 , 0.18581069, 0.44418598, 0.88687475,
       0.48460031, 0.09613176, 0.20120989, 0.11030902, 0.16480227])

seed

其实都是伪随机数,设置一个seed值,可以使随机序列可预测

>>> np.random.seed(123)
>>> np.random.rand(5)
array([0.69646919, 0.28613933, 0.22685145, 0.55131477, 0.71946897])
>>> np.random.seed(123)
>>> np.random.rand(5)
array([0.69646919, 0.28613933, 0.22685145, 0.55131477, 0.71946897])

-- EOF --

本文链接:https://www.pynote.net/archives/2234

留言区

电子邮件地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top