Turn---python--numpy random class

Source: Internet
Author: User
Tags rand random shuffle shuffle

The random class is used in NumPy to get the stochastic number. Numpy.random.random () generates a random floating-point number by default to generate a random floating-point number, the range is between 0.0~1.0, or the size of the returned data can be set by the parameter size; generate a random floating-point number: Import Numpyn = Numpy.random.random () Print n123 output: 0.4294894864211 set parameter Size:import Numpyn = Numpy.random.random (size= (3, 2)) print  n123 output: [[0.32018625 0.22410508] [0.57830333 0.74477335] [0.08333105 0.48533304]]123numpy.random.randint () generates a random integer API: Randint (Low, High=none, Size=none, dtype= ' l ') numpy.random.randint () randomly give an integer int type that can specify the range of this integer import NumPy as Npprint Np.random.randint (8) Print Np.random.randint (5, size=3) print np.random.randint (6, size= (3,2)) 1234 output: 4[1 1 3][[2 4] [5 4] [3 0]]  12345 specified range: Import numpy as Npprint np.random.randint (low=5, high=10, size=3) 12 output: [7 5 5]1numpy.random.normal () Gaussian distribution random number Api:normal (loc=0.0, scale=1.0, Size=none) loc: mean, scale: standard deviation, size: sizeimport Numpyn = numpy.random.normal for Sample extraction ( loc=0.0, Scale=1, Size= (2, 3)) print n123 output: [[ -0.15040995-0.43780718-0.22292445] [ -0.89388124-0.39465164 0.24113838]] 12NUMPY.RANDOM.RANDN () standard normal distribution random number Numpy.random.raNDN (D0, D1, ..., dn) function: Returns a sample value (d0*d1* ... * dn) from a standard normal distribution 1:import numpy as Npprint np.random.randn (4, 2) 12 output: [[ -1.88753851-2]. 54412195] [0.51856343-1.07733711] [1.05820592-0.23889217] [0.73309062 0.42152066]]1234 example 2:import numpy as Npprint np. RANDOM.RANDN (4, 2, 3) 12 output: [[[-1.00477835 1.16919912-1.28299362] [0.0645336 0.19143397-0.16957401]] [[ -1.45250491-0 .51844037-0.01241654] [0.41427599 0.19469926-0.92450654]] [[-1.90133606 1.23554382-1.37775698] [-0.98110245 0.356 2373-0.27816068] [[1.0380202 0.24293181 0.5341542] [ -0.62945999 1.62233629-0.07299065]]]1234567891011numpy.rand Om.rand () generates random numbers between [0, 1] numpy.random.rand (D0, D1, ..., DN) functions: Generates a random sample (d0*d1* ... * dn) in [0, 1): Import numpy as Npprint Np.ran Dom.rand (2,3) 12 output: [[0.06112299 0.02476706 0.04235452] [0.47891264 0.68831817 0.31309659]]12numpy.random.shuffle () random Shuffle sequence numpy.random.shuffle () randomly sort all elements of a sequence < incoming parameters can be a sequence or tuple >import numpy as NPX = Range (0, 8, 1) print Xnp.random.shuffle (x) print x12345 output: [0, 1, 2, 3, 4, 5, 6, 7][2, 3, 5, 4, 1, 7, 0, 6]123numpy.random.choice () an element of a randomly selected sequence Numpy.random.choice () can be randomly selected from a sequence (string, list, tuple, etc.) , returns a list of the random items of a tuple or string. Import NumPy as Npprint np.random.choice ([' A ', ' B ', ' C ', ' d ', ' e ']) 12 output: C1print Np.random.choice (5, 6) 1 output (6 elements less than 5): [2 3 3 3 1 2]1p: The probability of each entry appearing. If not, assume that all the entries in a sample are evenly distributed. Import NumPy as Npprint Np.random.choice (5, 3, p=[0.1, 0, 0.3, 0.6, 0]) (p=[0.1, 0, 0.3, 0.6, 0) The probability of 0 1 2 3 4 respectively is [0.1, 0 , 0.3, 0.6, 0]) output: [0 3 2]1import numpy as NPSs = [' Pooh ', ' Rabbit ', ' piglet ', ' Christopher ']print np.random.choice (SS, 5, p=[0.5, 0.1, 0.1, 0.3]) 123 output: [' Christopher ' Piglet ' Pooh ' piglet ' Christopher ']1numpy.random.binomial () Two-Item distribution sampling numpy.random.RandomState.binomial (n, p, Size=none) indicates that a two-item distribution is sampled, S is the number of successes P (n) =csnps (1? P) N?ssize: The number of samples, n p in the formula n p; The return value of the function represents the number of occurrences/successes in N. such as: When n=5,p=0.2,size=1000, that is, 5 events each occurrence probability is 0.2, then 5 simultaneous probability, sampling size=1000 times: P (p=0.2) =C55P5 (1?p) 0=0.32import NumPy as Npprint Sum (np.random.binomial (5, 0.2, size=10000) ==0)/10000.12 available: 0.32461 very close to manual calculation results. Numpy.random.RanDomstate () specifies that the seed value numpy.random.RandomState () specifies the seed value (which specifies that the seed value is the same as the random number that is generated for the same condition, avoids problems that are caused by random numbers when the program is debugged), if the seed value is not set Np.random.randint (8) may produce any integer within 0-7, and the number produced each time may be any one. When the seed value is set, Np.random.RandomState (0). Randint (8) can produce any integer within 0-7, but the seed value does not change the same number that is generated each time the program is run. Generate random integers: import numpy as Npprint np.random.RandomState (0). Randint (8) 12 output: 41 Generate random floating point number: import numpyn1 = Numpy.random.RandomState (0). Random_sample () n2 = numpy.random.RandomState (0). Random_sample (size= (2,3)) print N1, n21234 output: 0.548813503927 [[0.5488135 0.71518937 0.60276338] [0.54488318 0.4236548 0.64589411]]

Go---python--numpy random class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.