本文整理汇总了Python中numpy.random.RandomState方法的典型用法代码示例。如果您正苦于以下问题:Python random.RandomState方法的具体用法?Python random.RandomState怎么用?Python random.RandomState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.random
的用法示例。
在下文中一共展示了random.RandomState方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_function
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def check_function(self, function, sz):
from threading import Thread
out1 = np.empty((len(self.seeds),) + sz)
out2 = np.empty((len(self.seeds),) + sz)
# threaded generation
t = [Thread(target=function, args=(np.random.RandomState(s), o))
for s, o in zip(self.seeds, out1)]
[x.start() for x in t]
[x.join() for x in t]
# the same serial
for s, o in zip(self.seeds, out2):
function(np.random.RandomState(s), o)
# these platforms change x87 fpu precision mode in threads
if np.intp().dtype.itemsize == 4 and sys.platform == "win32":
assert_array_almost_equal(out1, out2)
else:
assert_array_equal(out1, out2)
示例2: test_group_var_generic_1d
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_group_var_generic_1d(self):
prng = RandomState(1234)
out = (np.nan * np.ones((5, 1))).astype(self.dtype)
counts = np.zeros(5, dtype='int64')
values = 10 * prng.rand(15, 1).astype(self.dtype)
labels = np.tile(np.arange(5), (3, )).astype('int64')
expected_out = (np.squeeze(values)
.reshape((5, 3), order='F')
.std(axis=1, ddof=1) ** 2)[:, np.newaxis]
expected_counts = counts + 3
self.algo(out, counts, values, labels)
assert np.allclose(out, expected_out, self.rtol)
tm.assert_numpy_array_equal(counts, expected_counts)
示例3: test_random_state
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_random_state():
import numpy.random as npr
# Check with seed
state = com.random_state(5)
assert state.uniform() == npr.RandomState(5).uniform()
# Check with random state object
state2 = npr.RandomState(10)
assert com.random_state(state2).uniform() == npr.RandomState(10).uniform()
# check with no arg random state
assert com.random_state() is np.random
# Error for floats or strings
with pytest.raises(ValueError):
com.random_state('test')
with pytest.raises(ValueError):
com.random_state(5.5)
示例4: test_failprob_threshold_basic
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_failprob_threshold_basic():
"""Sanity check on failprob_threshold: Verify, for a relatively large failure
probability, that the failure threshold it returns for a simple test
statistic actually results in failures at approximately the right
frequency.
"""
prngstate = RandomState(0)
def sample(n):
return prngstate.normal(0, 1, n)
target_prob = 1e-1
test_sample_size = 6
prob, thresh = failprob_threshold(
sample(1000), test_sample_size, target_prob)
samples = [all(v < thresh for v in sample(test_sample_size))
for _ in xrange(int(100 / target_prob))]
assert 50 < samples.count(True) < 200
示例5: compute_kullback_leibler_check_statistic
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def compute_kullback_leibler_check_statistic(n=100, prngstate=None):
"""Compute the lowest of the survival function and the CDF of the exact KL
divergence KL(N(mu1,s1)||N(mu2,s2)) w.r.t. the sample distribution of the
KL divergence drawn by computing log(P(x|N(mu1,s1)))-log(P(x|N(mu2,s2)))
over a sample x~N(mu1,s1). If we are computing the KL divergence
accurately, the exact value should fall squarely in the sample, and the
tail probabilities should be relatively large.
"""
if prngstate is None:
raise TypeError('Must explicitly specify numpy.random.RandomState')
mu1 = mu2 = 0
s1 = 1
s2 = 2
exact = gaussian_kl_divergence(mu1, s1, mu2, s2)
sample = prngstate.normal(mu1, s1, n)
lpdf1 = gaussian_log_pdf(mu1, s1)
lpdf2 = gaussian_log_pdf(mu2, s2)
estimate, std = kl.kullback_leibler(sample, lpdf1, lpdf2)
# This computes the minimum of the left and right tail probabilities of the
# exact KL divergence vs a gaussian fit to the sample estimate. There is a
# distinct negative skew to the samples used to compute `estimate`, so this
# statistic is not uniform. Nonetheless, we do not expect it to get too
# small.
return erfc(abs(exact - estimate) / std) / 2
示例6: generate_random_dataset
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def generate_random_dataset(dataset_size, sequence_avg_size):
"""
Build a dummy data structure with random numbers similar to our
usual datasets that is a list of pairs of sequences of numbers:
[ ( [1, 2, 10, ...], [3, 5, 6, 7, ...]),
( [5, 3], [34, 23, 44, 1, ...] ),
... ]
"""
random_generator = RandomState(42)
dataset = []
for i in range(0, dataset_size):
item = []
# Each item contains 2 sequences.
for j in range(0, 2):
sequence_length = random_generator.randint(sequence_avg_size - 5, sequence_avg_size + 5)
# sequence_length = random_generator.randint(1, 5)
sequence = random_generator.randint(0, 100, size=sequence_length)
item.append(sequence)
item = tuple(item)
dataset.append(item)
return dataset
示例7: defaultNumPyInit
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def defaultNumPyInit(task, NP, rnd=rand, **kwargs):
r"""Initialize starting population that is represented with `numpy.ndarray` with shape `{NP, task.D}`.
Args:
task (Task): Optimization task.
NP (int): Number of individuals in population.
rnd (Optional[mtrand.RandomState]): Random number generator.
kwargs (Dict[str, Any]): Additional arguments.
Returns:
Tuple[numpy.ndarray, numpy.ndarray[float]]:
1. New population with shape `{NP, task.D}`.
2. New population function/fitness values.
"""
pop = task.Lower + rnd.rand(NP, task.D) * task.bRange
fpop = apply_along_axis(task.eval, 1, pop)
return pop, fpop
示例8: defaultIndividualInit
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def defaultIndividualInit(task, NP, rnd=rand, itype=None, **kwargs):
r"""Initialize `NP` individuals of type `itype`.
Args:
task (Task): Optimization task.
NP (int): Number of individuals in population.
rnd (Optional[mtrand.RandomState]): Random number generator.
itype (Optional[Individual]): Class of individual in population.
kwargs (Dict[str, Any]): Additional arguments.
Returns:
Tuple[numpy.ndarray[Individual], numpy.ndarray[float]:
1. Initialized individuals.
2. Initialized individuals function/fitness values.
"""
pop = objects2array([itype(task=task, rnd=rnd, e=True) for _ in range(NP)])
return pop, asarray([x.f for x in pop])
示例9: test_random_state
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_random_state():
import numpy.random as npr
# Check with seed
state = com._random_state(5)
assert state.uniform() == npr.RandomState(5).uniform()
# Check with random state object
state2 = npr.RandomState(10)
assert (com._random_state(state2).uniform() ==
npr.RandomState(10).uniform())
# check with no arg random state
assert com._random_state() is np.random
# Error for floats or strings
with pytest.raises(ValueError):
com._random_state('test')
with pytest.raises(ValueError):
com._random_state(5.5)
示例10: test_issue_30
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_issue_30():
# From the issue:
vec = np.array([33., 44., 58., 49., 46., 98., 97.])
arm = AutoARIMA(out_of_sample_size=1, seasonal=False,
suppress_warnings=True)
arm.fit(vec)
# This is a way to force it:
ARIMA(order=(0, 1, 0), out_of_sample_size=1).fit(vec)
# Want to make sure it works with exog arrays as well
exog = np.random.RandomState(1).rand(vec.shape[0], 2)
auto_arima(vec, exogenous=exog, out_of_sample_size=1,
seasonal=False,
suppress_warnings=True)
# This is a way to force it:
ARIMA(order=(0, 1, 0), out_of_sample_size=1).fit(vec, exogenous=exog)
示例11: test_two_sample_conf_int
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_two_sample_conf_int():
prng = RandomState(42)
# Shift is -1
x = np.array(range(5))
y = np.array(range(1, 6))
res = two_sample_conf_int(x, y, seed=prng)
expected_ci = (-3.5, 1.0012461)
assert_almost_equal(res, expected_ci)
res = two_sample_conf_int(x, y, seed=prng, alternative="upper")
expected_ci = (-5, 1)
assert_almost_equal(res, expected_ci)
res = two_sample_conf_int(x, y, seed=prng, alternative="lower")
expected_ci = (-3, 5)
assert_almost_equal(res, expected_ci)
# Specify shift with a function pair
shift = (lambda u, d: u + d, lambda u, d: u - d)
res = two_sample_conf_int(x, y, seed=5, shift=shift)
assert_almost_equal(res, (-3.5, 1))
# Specify shift with a multiplicative pair
shift = (lambda u, d: u * d, lambda u, d: u / d)
res = two_sample_conf_int(x, y, seed=5, shift=shift)
assert_almost_equal(res, (-1, -1))
示例12: test_permute_incidence_fixed_sums
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_permute_incidence_fixed_sums():
prng = RandomState(42)
x0 = prng.randint(2, size=80).reshape((8, 10))
x1 = permute_incidence_fixed_sums(x0)
K = 5
m = []
for i in range(1000):
x2 = permute_incidence_fixed_sums(x0, k=K)
m.append(np.sum(x0 != x2))
np.testing.assert_(max(m) <= K * 4,
"Too many swaps occurred")
for axis in (0, 1):
for test_arr in (x1, x2):
np.testing.assert_array_equal(x0.sum(axis=axis),
test_arr.sum(axis=axis))
示例13: test_npc
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def test_npc():
prng = RandomState(55)
pvalues = np.linspace(0.05, 0.9, num=5)
distr = prng.uniform(low=0, high=10, size=500).reshape(100, 5)
res = npc(pvalues, distr, "fisher", "greater", plus1=False)
np.testing.assert_almost_equal(res, 0.33)
res = npc(pvalues, distr, "fisher", "less", plus1=False)
np.testing.assert_almost_equal(res, 0.33)
res = npc(pvalues, distr, "fisher", "two-sided", plus1=False)
np.testing.assert_almost_equal(res, 0.31)
res = npc(pvalues, distr, "liptak", "greater", plus1=False)
np.testing.assert_almost_equal(res, 0.35)
res = npc(pvalues, distr, "tippett", "greater", plus1=False)
np.testing.assert_almost_equal(res, 0.25)
res = npc(pvalues, distr, "fisher",
alternatives=np.array(["less", "greater", "less",
"greater", "two-sided"]), plus1=False)
np.testing.assert_almost_equal(res, 0.38)
示例14: __call__
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def __call__(self, shape, dtype=None):
if self.nb_filters is not None:
kernel_shape = tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
else:
kernel_shape = (int(self.input_dim), self.kernel_size[-1])
fan_in, fan_out = initializers._compute_fans(
tuple(self.kernel_size) + (self.input_dim, self.nb_filters)
)
if self.criterion == 'glorot':
s = 1. / (fan_in + fan_out)
elif self.criterion == 'he':
s = 1. / fan_in
else:
raise ValueError('Invalid criterion: ' + self.criterion)
rng = RandomState(self.seed)
modulus = rng.rayleigh(scale=s, size=kernel_shape)
phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape)
weight_real = modulus * np.cos(phase)
weight_imag = modulus * np.sin(phase)
weight = np.concatenate([weight_real, weight_imag], axis=-1)
return weight
示例15: list_random_circuits_onelen
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import RandomState [as 别名]
def list_random_circuits_onelen(opLabels, length, count, seed=None):
"""
Create a list of random operation sequences of a given length.
Parameters
----------
opLabels : tuple
tuple of operation labels to include in operation sequences.
length : int
the operation sequence length.
count : int
the number of random strings to create.
seed : int, optional
If not None, a seed for numpy's random number generator.
Returns
-------
list of Circuits
A list of random operation sequences as Circuit objects.
"""
ret = []
rndm = _rndm.RandomState(seed) # ok if seed is None
opLabels = list(opLabels) # b/c we need to index it below
for i in range(count): # pylint: disable=unused-variable
r = rndm.random_sample(length) * len(opLabels)
ret.append(_cir.Circuit([opLabels[int(k)] for k in r]))
return ret