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

Why can't you use Numpy's random function well?

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

Share

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

In this issue, the editor will bring you about why you can not use the random function of Numpy. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In the process of learning and application of python data analysis, we often need to use the random function of numpy. Because the random function random has many functions, it is often confused or can not be remembered. Let's sum up and learn.

1. Import numpy as np

1 numpy.random.rand ()

Numpy.random.rand (d0.d0.d1.dyn)

The rand function generates data between [0Power1) based on a given dimension, including 0, but not 1.

Dn table each dimension

The return value is the array of the specified dimension

1. Np.random.rand (4jue 2)

1. Array ([0.02173903, 0.44376568]

2. [0.25309942, 0.85259262]

3. [0.56465709, 0.95135013]

4. [0.14145746, 0.55389458]])

1. Np.random.rand (4, 3, 2) # shape: 4, 3, 3, 2

1. Array ([[0.08256277, 0.11408276])

2. [0.11182496, 0.51452019]

3. [0.09731856, 0.18279204]]

4.

5. [[0.74637005, 0.76065562]

6. [0.32060311, 0.69410458]

7. [0.28890543, 0.68532579]]

8.

9. [[0.72110169, 0.52517524]

10. [0.32876607, 0.66632414]

11. [0.45762399, 0.49176764]]

twelve。

13. [[0.73886671, 0.81877121]

14. [0.03984658, 0.99454548]

15. [0.18205926, 0.99637823])

2 numpy.random.randn ()

Numpy.random.randn (d0.d0.d1.dyn)

The randn function returns a sample or set of samples with a standard normal distribution.

Dn table each dimension

The return value is the array of the specified dimension

1. Np.random.randn () # returns a single data when there are no parameters

1.-1.1241580894939212

1. Np.random.randn (2pr 4)

1. Array ([0.27795239,-2.57882503, 0.3817649, 1.42367345]

2. [- 1.16724625,-0.22408299, 0.63006614,-0.41714538])

1. Np.random.randn (4pm 3pm 2)

1. Array ([[1.27820764, 0.92479163])

2. [- 0.15151257, 1.3428253]

3. [- 1.30948998, 0.15493686]]

4.

5. [- 1.49645411,-0.27724089]

6. [0.71590275, 0.81377671]

7. [- 0.71833341, 1.61637676]]

8.

9. [0.52486563,-1.7345101]

10. [1.24456943,-0.10902915]

11. [1.27292735,-0.00926068]]

twelve。

13. [[0.88303, 0.46116413]

14. [0.13305507, 2.44968809]

15. [- 0.73132153,-0.88586716]])

Introduction to standard normal distribution

Standard normal distribution-standard normal distribution

The standard normal distribution, also known as u distribution, is a normal distribution with 0 as the mean and 1 as the standard deviation, which is marked as N (0).

3 numpy.random.randint ()

3.1 numpy.random.randint ()

Numpy.random.randint (low, high=None, size=None, dtype='l')

Returns a random integer with a range of [low,high), including low, but not high

Parameters: low is the minimum value, high is the maximum value, size is the array dimension size, dtype is the data type, and the default data type is np.int

When high is not filled in, the default range for generating random numbers is [0Centum low).

1. Np.random.randint (1) # returns an integer between [0, 1), so there is only 0.

1. Array ([0,0,0,0,0])

1. Np.random.randint (1p5) # returns a random integer of [1p5) time

1. 4

1. Np.random.randint (- 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 4, 5, 5, 5, 4, 5, 5, 5, 4, 5, 5, 5, 4, 5, 5, 5, 4, 5, 5, 5, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4

1. Array ([2,-1]

2. [2,0])

3.2 numpy.random.random_integers

Numpy.random.random_integers (low, high=None, size=None)

Returns a random integer with a range of [low,high], including low and high

Parameters: low is the minimum, high is the maximum, and size is the size of the array dimension.

When high is not filled in, the default range for generating random numbers is [1]

This function has been replaced in the latest version of numpy. It is recommended to use the randint function.

1. Np.random.random_integers (1)

1. Array ([1,1,1,1,1])

4 generate floating-point numbers between [0pc1)

Numpy.random.random_sample (size=None)

Numpy.random.random (size=None)

Numpy.random.ranf (size=None)

Numpy.random.sample (size=None)

1. Print ('- random_sample-')

2. Print (np.random.random_sample (size= (2jin2)

3. Print ('- random-')

4. Print (np.random.random (size= (2jin2)

5. Print ('- ranf-')

6. Print (np.random.ranf (size= (2jin2)

7. Print ('- sample-')

8. Print (np.random.sample (size= (2jin2)

1.-random_sample-

2. [[0.34966859 0.85655008]

3. [0.16045328 0.87908218]]

4.-random-

5. [[0.25303772 0.45417512]

6. [0.76053763 0.12454433]]

7.-ranf-

8. [[0.0379055 0.51288667]

9. [0.71819639 0.97292903]]

10.-sample-

11. [[0.59942807 0.80211491]

12. [0.36233939 0.12607092]]

5 numpy.random.choice ()

Numpy.random.choice (a, size=None, replace=True, p=None)

Generate random numbers from a given one-dimensional array

Parameters: an is an one-dimensional array similar to data or integers; size is an array dimension; p is the probability of the occurrence of data in an array

When an is an integer, the corresponding one-dimensional array is np.arange (a)

1. Np.random.choice (5pm 3)

1. Array ([4,1,4])

1. Np.random.choice (5,3, replace=False)

2. # when replace is False, the generated random number cannot have duplicate values

1. Array ([0,3,1])

1. Np.random.choice (5Jing size = (3Jing 2))

1. Array ([1, 0]

2. [4, 2]

3. [3, 3]])

1. Demo_list = ['lenovo',' sansumg','moto','xiaomi', 'iphone']

2. Np.random.choice (demo_list,size= (3p3))

1. Array ([['moto',' iphone', 'xiaomi']

2. ['lenovo',' xiaomi', 'xiaomi']

3. ['xiaomi',' lenovo', 'iphone']

4. Dtype='

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