Diferencia entre np.aleatorio.seed () y np.aleatorio.RandomState()


Sé que para sembrar la aleatoriedad de numpy.al azar, y ser capaz de reproducirlo, debo nosotros:

import numpy as np
np.random.seed(1234)

Pero, ¿qué hace np.random.RandomState() hacer?

Author: alecxe, 2014-04-10

3 answers

Si desea establecer la semilla que las llamadas usarán a np.random..., use np.random.seed:

np.random.seed(1234)
np.random.uniform(0, 10, 5)
#array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808])
np.random.rand(2,3)
#array([[ 0.27259261,  0.27646426,  0.80187218],
#       [ 0.95813935,  0.87593263,  0.35781727]])

Si desea utilizar la clase, debe guardar una instancia de la clase, iniciada con una semilla:

r = np.random.RandomState(1234)
r.uniform(0, 10, 5)
#array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808])

Y mantiene el estado como antes:

r.rand(2,3)
#array([[ 0.27259261,  0.27646426,  0.80187218],
#       [ 0.95813935,  0.87593263,  0.35781727]])

Puedes ver el estado del tipo de clase 'global' con

np.random.get_state()

Y de su propia instancia de clase con

r.get_state()
 42
Author: askewchan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-10 17:24:22

Al azar.seed es un método para rellenar al azar.Contenedor RandomState.

De numpy docs:

numpy.random.seed(seed=None)

Siembra el generador.

Este método se llama cuando se inicializa RandomState. Se puede llamar de nuevo para volver a sembrar el generador. Para obtener más información, consulte RandomState.

class numpy.random.RandomState

Contenedor para el generador de números pseudo-aleatorios Mersenne Twister.

 8
Author: Dmitry Nazarov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-10 17:13:25
np.random.RandomState()

Construye un generador de números aleatorios. No tiene ningún efecto sobre las funciones independientes en np.random, pero debe usarse explícitamente:

>>> rng = np.random.RandomState(42)
>>> rng.randn(4)
array([ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986])
>>> rng2 = np.random.RandomState(42)
>>> rng2.randn(4)
array([ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986])
 3
Author: Fred Foo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-11 08:51:33