Python numpy.random.RandomState() Examples

The following are 30 code examples of numpy.random.RandomState(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy.random , or try the search function .
Example #1
Source File: init.py    From deep_complex_networks with MIT License 7 votes vote down vote up
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 
Example #2
Source File: test_algos.py    From vnpy_crypto with MIT License 6 votes vote down vote up
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) 
Example #3
Source File: training_chainer_test.py    From knmt with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #4
Source File: test_common.py    From vnpy_crypto with MIT License 6 votes vote down vote up
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) 
Example #5
Source File: test_core.py    From permute with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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)) 
Example #6
Source File: test_kl.py    From bayeslite with Apache License 2.0 6 votes vote down vote up
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 
Example #7
Source File: test_threshold.py    From bayeslite with Apache License 2.0 6 votes vote down vote up
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 
Example #8
Source File: test_utils.py    From permute with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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)) 
Example #9
Source File: test_common.py    From recruit with Apache License 2.0 6 votes vote down vote up
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) 
Example #10
Source File: test_npc.py    From permute with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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) 
Example #11
Source File: test_random.py    From vnpy_crypto with MIT License 6 votes vote down vote up
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) 
Example #12
Source File: test_algos.py    From recruit with Apache License 2.0 6 votes vote down vote up
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) 
Example #13
Source File: algorithm.py    From NiaPy with MIT License 6 votes vote down vote up
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 
Example #14
Source File: algorithm.py    From NiaPy with MIT License 6 votes vote down vote up
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]) 
Example #15
Source File: test_arima.py    From pmdarima with MIT License 6 votes vote down vote up
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) 
Example #16
Source File: test_npc.py    From permute with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_minp_one_pvalue():
    prng = RandomState(55)
    pvalues = np.array([1])
    distr = prng.uniform(low=0, high=10, size=20).reshape(20, 1)
    npc(pvalues, distr, "fisher", "greater")

# TODO: more fwer_minp tests 
Example #17
Source File: test_arima.py    From pmdarima with MIT License 5 votes vote down vote up
def test_oob_for_issue_29():
    dta = sm.datasets.sunspots.load_pandas().data
    dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
    del dta["YEAR"]

    xreg = np.random.RandomState(1).rand(dta.shape[0], 3)

    # Try for cv on/off, various D levels, and various Xregs
    for d in (0, 1):
        for cv in (0, 3):
            for exog in (xreg, None):

                # surround with try/except so we can log the failing combo
                try:
                    model = ARIMA(order=(2, d, 0),
                                  out_of_sample_size=cv).fit(dta,
                                                             exogenous=exog)

                    # If exogenous is defined, we need to pass n_periods of
                    # exogenous rows to the predict function. Otherwise we'll
                    # just leave it at None
                    if exog is not None:
                        xr = exog[:3, :]
                    else:
                        xr = None

                    _, _ = model.predict(n_periods=3, return_conf_int=True,
                                         exogenous=xr)

                # Statsmodels can be fragile with ARMA coefficient
                # computation. If we encounter that, pass:
                #   ValueError: The computed initial MA coefficients are
                #       not invertible. You should induce invertibility,
                #       choose a different model order, or ...
                except Exception as ex:
                    # print("Failing combo: d=%i, cv=%i, exog=%r"
                    #       % (d, cv, exog))
                    if "invertibility" in pytest_error_str(ex):
                        pass
                    else:
                        raise 
Example #18
Source File: test_arima.py    From pmdarima with MIT License 5 votes vote down vote up
def test_with_oob_and_exog(as_pd):
    endog = hr
    exog = np.random.RandomState(1).rand(hr.shape[0], 3)
    if as_pd:
        exog = pd.DataFrame.from_records(exog)
        endog = pd.Series(hr)

    arima = ARIMA(order=(2, 1, 2),
                  suppress_warnings=True,
                  scoring='mse',
                  out_of_sample_size=10).fit(y=endog, exogenous=exog)

    # show we can get oob score and preds
    arima.oob() 
Example #19
Source File: test_npc.py    From permute with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_npc_bad_alternative():
    prng = RandomState(55)
    pvalues = np.linspace(0.05, 0.9, num=5)
    distr = prng.uniform(low=0, high=10, size=50).reshape(10, 5)
    npc(pvalues, distr, "fisher", np.array(["greater", "less"])) 
Example #20
Source File: test_npc.py    From permute with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_minp_bad_distr():
    prng = RandomState(55)
    pvalues = np.linspace(0.05, 0.9, num=5)
    distr = prng.uniform(low=0, high=10, size=20).reshape(10, 2)
    fwer_minp(pvalues, distr, "fisher", "greater") 
Example #21
Source File: test_morestats.py    From Computable with MIT License 5 votes vote down vote up
def test_normal(self):
        rs = RandomState(1234567890)
        x1 = rs.standard_exponential(size=50)
        x2 = rs.standard_normal(size=50)
        A,crit,sig = stats.anderson(x1)
        assert_array_less(crit[:-1], A)
        A,crit,sig = stats.anderson(x2)
        assert_array_less(A, crit[-2:]) 
Example #22
Source File: test_algos.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_group_var_large_inputs(self):

        prng = RandomState(1234)

        out = np.array([[np.nan]], dtype=self.dtype)
        counts = np.array([0], dtype='int64')
        values = (prng.rand(10 ** 6) + 10 ** 12).astype(self.dtype)
        values.shape = (10 ** 6, 1)
        labels = np.zeros(10 ** 6, dtype='int64')

        self.algo(out, counts, values, labels)

        assert counts[0] == 10 ** 6
        tm.assert_almost_equal(out[0, 0], 1.0 / 12, check_less_precise=True) 
Example #23
Source File: test_algos.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_group_var_generic_2d_all_finite(self):
        prng = RandomState(1234)

        out = (np.nan * np.ones((5, 2))).astype(self.dtype)
        counts = np.zeros(5, dtype='int64')
        values = 10 * prng.rand(10, 2).astype(self.dtype)
        labels = np.tile(np.arange(5), (2, )).astype('int64')

        expected_out = np.std(values.reshape(2, 5, 2), ddof=1, axis=0) ** 2
        expected_counts = counts + 2

        self.algo(out, counts, values, labels)
        assert np.allclose(out, expected_out, self.rtol)
        tm.assert_numpy_array_equal(counts, expected_counts) 
Example #24
Source File: test_algos.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_group_var_generic_1d_flat_labels(self):
        prng = RandomState(1234)

        out = (np.nan * np.ones((1, 1))).astype(self.dtype)
        counts = np.zeros(1, dtype='int64')
        values = 10 * prng.rand(5, 1).astype(self.dtype)
        labels = np.zeros(5, dtype='int64')

        expected_out = np.array([[values.std(ddof=1) ** 2]])
        expected_counts = counts + 5

        self.algo(out, counts, values, labels)

        assert np.allclose(out, expected_out, self.rtol)
        tm.assert_numpy_array_equal(counts, expected_counts) 
Example #25
Source File: test_random.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_poisson(self):
        max_lam = np.random.RandomState().poisson_lam_max

        lam = [1]
        bad_lam_one = [-1]
        bad_lam_two = [max_lam * 2]
        poisson = np.random.poisson
        desired = np.array([1, 1, 0])

        self.setSeed()
        actual = poisson(lam * 3)
        assert_array_equal(actual, desired)
        assert_raises(ValueError, poisson, bad_lam_one * 3)
        assert_raises(ValueError, poisson, bad_lam_two * 3) 
Example #26
Source File: test_random.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_invalid_array_shape(self):
        # gh-9832
        assert_raises(ValueError, np.random.RandomState, np.array([], dtype=np.int64))
        assert_raises(ValueError, np.random.RandomState, [[1, 2, 3]])
        assert_raises(ValueError, np.random.RandomState, [[1, 2, 3],
                                                          [4, 5, 6]]) 
Example #27
Source File: test_random.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_invalid_array(self):
        # seed must be an unsigned 32 bit integer
        assert_raises(TypeError, np.random.RandomState, [-0.5])
        assert_raises(ValueError, np.random.RandomState, [-1])
        assert_raises(ValueError, np.random.RandomState, [4294967296])
        assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296])
        assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296]) 
Example #28
Source File: test_random.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_invalid_scalar(self):
        # seed must be an unsigned 32 bit integer
        assert_raises(TypeError, np.random.RandomState, -0.5)
        assert_raises(ValueError, np.random.RandomState, -1) 
Example #29
Source File: test_random.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_array(self):
        s = np.random.RandomState(range(10))
        assert_equal(s.randint(1000), 468)
        s = np.random.RandomState(np.arange(10))
        assert_equal(s.randint(1000), 468)
        s = np.random.RandomState([0])
        assert_equal(s.randint(1000), 973)
        s = np.random.RandomState([4294967295])
        assert_equal(s.randint(1000), 265) 
Example #30
Source File: test_random.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_scalar(self):
        s = np.random.RandomState(0)
        assert_equal(s.randint(1000), 684)
        s = np.random.RandomState(4294967295)
        assert_equal(s.randint(1000), 419)