Detailed usage of np.random.seed() in numpy

In machine learning and deep learning, we often use np.random.seed() to use random number seeds to make the random numbers generated each time the same.

numpy.randn.randn(d0,d1,...,dn)

  • The randn function generates data with a high probability between (-2.58~+2.58) according to the given dimension
  • The randn function returns a sample or a set of samples with a standard normal distribution
  • dn represents each dimension
  • The return value is an array of the specified dimension

import numpy as np

a = np.random.randn(2,4)  #4*2矩阵
print(a)

b = np.random.randn(4,3,2)  #shape:4*3*2
print(b)

We will study np.random.seed() with two questions:

  1. Is np.random.seed() always effective?

  2. What is the parameter function of np.random.seed(Argument)?

EG experiment

# -*- coding: utf-8 -*- 
# @Time : 2019/10/26 20:57 
# @Author : BaoBao
# @Mail : [email protected] 
# @File : random.seed.py 
# @Software: PyCharm

import numpy as np

if __name__ == '__main__':
    i = 0
    while (i < 6):
        if (i < 3):
            np.random.seed(0)
            print(np.random.randn(1, 5))
        else:
            print(np.random.randn(1, 5))
            pass
        i += 1

    print("-------------------")
    i = 0
    while (i < 2):
        print(np.random.randn(1, 5))
        i += 1
    print(np.random.randn(2, 5))

    print("---------reset----------")
    np.random.seed(0)
    i = 0
    while (i < 8):
        print(np.random.randn(1, 5))
        i += 1

Run the screenshot:

  It can be seen that np.random.seed() is always valid for the following random numbers.

  After using random.seed() twice, even after jumping out of the loop, the result of generating random numbers is still the same. After jumping out of the while loop for the first time, enter the second while loop,

The two random arrays obtained are indeed different from those with random number seeds added. However, the result of adding the random number seed later is the same as the previous result in the eight cycles. illustrate,

The random number seed has always had an impact on subsequent results. At the same time, after adding the random number seed, the following arrays are generated in a certain order.

The role of EG random number seed parameters

# -*- coding: utf-8 -*- 
# @Time : 2019/10/26 20:57 
# @Author : BaoBao
# @Mail : [email protected] 
# @File : random.seed.py 
# @Software: PyCharm
import numpy as np

if __name__ == '__main__':
    i = 0
    np.random.seed(0)
    while (i < 3):
        print(np.random.randn(1, 5))
        i += 1
    i = 0
    print("---------------------")
    np.random.seed(1)
    i = 0
    while (i < 3):
        print(np.random.randn(1, 5))
        i += 1

Run the screenshot:

  When the random number seed parameter is 0 and 1, the result of the generated random number is the same. Description This parameter specifies the starting position of a random number generation. Each parameter corresponds to a position.

And after the parameter is determined, the generation sequence of the following random numbers is also determined. So, how to choose the parameters of the random number seed? This parameter is just to determine the starting position of the random number, which can be randomly assigned.

Guess you like

Origin blog.csdn.net/qq_42514371/article/details/122850834