Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of numpy.random

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shares with you the content of the sample analysis of numpy.random. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

From numpy import randomnumpy.random.uniform (low=0.0, high=1.0, size=None)

Generate size floating point numbers that are uniformly distributed. The range of values is [low, high), and the default range is [0,1.0).

> random.uniform () 0.3999807403689315 > random.uniform (size=1) array ([0.55950578]) > > random.uniform (5,6) 5.293682668235986 > random.uniform (5,6, size= (2)) array ([5.82416021, 5.68916836, 5.89708586], [5.63843125, 5.22963754, 5.4319899]) numpy.random.rand (d0, D1,..., dn)

Generate an array of dimensions (d0, D1,..., dn). The elements of the array are taken from the uniform distribution on [0,1). If there is no parameter input, a number is generated.

> random.rand () 0.4378166124207712 > random.rand (1) array ([0.69845956]) > > random.rand (3prime2) array ([[0.15725424, 0.45786148], [0.63133098, 0.81789056], [0.40032941, 0.19108526]]) > random.rand (3recover2) array ([[0.00404447], [0.3837963], [0.32518355], [0.82482599]) [[0.79603205], [0.19087375]) numpy.random.randint (low, high=None, size=None, dtype='I')

Generate size integers with a value interval of [low, high). If there is no input parameter high, the value interval is [0, low).

> > random.randint (8) 5 > random.randint (8, size=1) array ([1]) > random.randint (8, size= (2pint 2)) array ([4,7,0], [1,4,1]], [[2,6,4]]) > random.randint (8, size= (2pyr3), dtype='int64') array ([5,5,6], [2,7,2]]) [[2,7,6], [4,7,7], dtype=int64) numpy.random.random_integers (low, high=None, size=None)

Generate size integers with a value interval of [low, high]. If there is no input parameter high, the value interval is [1, low]. Note that there are closed intervals around here.

> random.random_integers (5) 1 > random.random_integers (5, size=1) array ([2]) > random.random_integers (4,5, size= (2)) array ([[5,4], [4,4]]) numpy.random.random (size=None)

Generate floating point numbers between [0.0,1.0)

> random.random (5) array ([0.94128141, 0.98725499, 0.48435957, 0.90948135, 0.40570882]) > random.random () 0.49761416226728084

The same usage:

Numpy.random.random_sample

Numpy.random.ranf

Numpy.random.sample (extraction does not repeat)

Numpy.random.bytes (length)

Generate random bytes

> random.bytes (1) baked%'> random.bytes (2) b'\ xd0\ xc3'numpy.random.choice (a, size=None, replace=True, p=None)

Select a random number of size (dimension) size from a (array). Replace=True means repeatable decimation, and p is the probability of each number in a.

If an is an integer, then the array represented by an is arange (a)

> random.choice (5) 3 > random.choice ([0.2,0.4]) 0.2 > random.choice ([0.2,0.4], p = [1,0]) 0.2 > random.choice ([0.2,0.4], p = [0,1]) 0.4 > random.choice (5,5) array ([1,2,4,4]) > random.choice (5,5, False) array ([2,0,1,4,3]) > > random.choice 3, 5), False) array ([[43, 81, 48, 2, 8], [33, 79, 30, 24, 83], [3, 82, 97, 49, 98]], [[32, 12, 15, 0, 96], [19, 61, 6, 42, 60], [7, 93, 20, 18, 58]]) numpy.random.permutation (x)

Randomly disturb the elements in x. If x is an integer, disrupt arange (x), if x is an array, scramble the first index of copy (x), which means to copy x first, disrupt the copy, and disrupt only the first dimension of the array.

> > random.permutation (5) array ([1min2 array 3, 0, 4]) > > random.permutation (5) array ([1, 4, 3, 2]) > random.permutation ([[1 Mie 2 Meng 3], [4 Meng 5 Jo 6]]) array ([1 Meng 2 Meng 3], [4 Meng 5 Jing 6]) > random.permutation ([1 Meng 2 Jing 3], [4 Meng 5 Meng 6]) array ([[4 Jing 5 Jing 6], [1 min 2]) 3]]) numpy.random.shuffle (x)

Similar to permutation, it randomly disrupts the elements in x. If x is an integer, then disrupt arange (x). But shuffle will modify x

> a = arange (5) > aarray ([0,1,2,3,4]) > > random.permutation (a) array ([1,4,3,0]) > aarray ([0,1,2,3,4]) > > random.shuffle (a) > > aarray ([4,1,3,0]) numpy.random.seed (seed=None)

Set the initial value of the random generation algorithm

Other random number functions that accord with function distribution

Numpy.random.beta

Numpy.random.binomial

Numpy.random.chisquare

Numpy.random.dirichlet

Numpy.random.exponential

Numpy.random.f

Numpy.random.gamma

Numpy.random.geometric

Numpy.random.gumbel

Numpy.random.hypergeometric

Numpy.random.laplace

Numpy.random.logistic

Numpy.random.lognormal

Numpy.random.logseries

Numpy.random.multinomial

Numpy.random.multivariate_normal

Numpy.random.negative_binomial

Numpy.random.noncentral_chisquare

Numpy.random.noncentral_f

Numpy.random.normal

Numpy.random.pareto

Numpy.random.poisson

Numpy.random.power

Numpy.random.randn

Numpy.random.rayleigh

Numpy.random.standard_cauchy

Numpy.random.standard_exponential

Numpy.random.standard_gamma

Numpy.random.standard_normal

Numpy.random.standard_t

Numpy.random.triangular

Numpy.random.vonmises

Numpy.random.wald

Numpy.random.weibull

Numpy.random.zipf

Thank you for reading! This is the end of this article on "sample Analysis of numpy.random". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report