How np.random.seed and np.random.randn works

How does it work under the hood?

From [2]

How to use it?

Not use fixed random seed

>>> import numpy as np
>>> np.random.seed(42)
>>> a = np.random.rand(20)
>>> a
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258,0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497,0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914])>>> a = np.random.rand(20)
>>> a
array([0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998,0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041,0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203,0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249])

Use fixed random seed

>>> import numpy as np
>>> np.random.seed(42)
>>> a = np.random.rand(20)
>>> a
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258,0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497,0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914])>>> np.random.seed(42)
>>> a = np.random.rand(20)
>>> a
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258,0.02058449, 0.96990985, 0.83244264, 0.21233911, 0.18182497,0.18340451, 0.30424224, 0.52475643, 0.43194502, 0.29122914])Reference

rand and randn

rand                 Uniformly distributed values.
randn Normally distributed values
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> plt.hist(np.random.randn(10000), bins=100)
>>> plt.show()

Source code of randn

Returns        -------        Z : ndarray or float            A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from            the standard normal distribution, or a single such float if            no parameters were supplied.

Source code of np.random

==================== =========================================================Compatibilityfunctions - removedin the new API-------------------- ---------------------------------------------------------rand                 Uniformly distributed values.randn                Normally distributed values.ranf                 Uniformly distributed floating point numbers.random_integers      Uniformly distributed integers in a given range.(deprecated, use ``integers(..., closed=True)`` instead)random_sample        Alias for `random_sample`randint              Uniformly distributed integers in a given rangeseed                 Seed the legacy random number generator.
plt.hist(np.random.normal(0, 1, 10000), bins=100)

Application

>>> np.random.seed(42)>>> X = np.random.rand(10, 3)>>> Xarray([[0.37454012, 0.95071431, 0.73199394],[0.59865848, 0.15601864, 0.15599452],[0.05808361, 0.86617615, 0.60111501],[0.70807258, 0.02058449, 0.96990985],[0.83244264, 0.21233911, 0.18182497],[0.18340451, 0.30424224, 0.52475643],[0.43194502, 0.29122914, 0.61185289],[0.13949386, 0.29214465, 0.36636184],[0.45606998, 0.78517596, 0.19967378],[0.51423444, 0.59241457, 0.04645041]])>>> mask = np.random.rand(X.shape[0])>>> maskarray([0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203,0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249])>>> np.random.seed(42)>>> X = np.random.rand(10, 3)>>> Xarray([[0.37454012, 0.95071431, 0.73199394],[0.59865848, 0.15601864, 0.15599452],[0.05808361, 0.86617615, 0.60111501],[0.70807258, 0.02058449, 0.96990985],[0.83244264, 0.21233911, 0.18182497],[0.18340451, 0.30424224, 0.52475643],[0.43194502, 0.29122914, 0.61185289],[0.13949386, 0.29214465, 0.36636184],[0.45606998, 0.78517596, 0.19967378],[0.51423444, 0.59241457, 0.04645041]])>>> mask = np.random.rand(X.shape[0])>>> maskarray([0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203,0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249])>>> mask = mask > 0.5>>> sum(mask)5>>> X = X[mask]>>> Xarray([[0.37454012, 0.95071431, 0.73199394],[0.70807258, 0.02058449, 0.96990985],[0.83244264, 0.21233911, 0.18182497],[0.18340451, 0.30424224, 0.52475643],[0.45606998, 0.78517596, 0.19967378]])>>> maskarray([ True, False, False,  True,  True,  True, False, False,  True,False])

Reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store