Python numpy.random() Examples

The following are code examples for showing how to use numpy.random(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: chainer-openai-transformer-lm   Author: soskek   File: train.py    MIT License 6 votes vote down vote up
def run_epoch():
    for xmb, mmb, ymb in iter_data(*shuffle(trX, trM, trYt, random_state=np.random),
                                   n_batch=n_batch_train, truncate=True, verbose=True):
        global n_updates
        XMB = model.xp.asarray(xmb)
        YMB = model.xp.asarray(ymb)
        MMB = model.xp.asarray(mmb)
        h = model(XMB)
        lm_logits = lm_head(h)
        clf_logits = clf_head(h, XMB)
        compute_loss_fct(XMB, YMB, MMB, clf_logits, lm_logits)
        n_updates += 1
        if n_updates in [
                1000,
                2000,
                4000,
                8000,
                16000,
                32000] and n_epochs == 0:
            log() 
Example 2
Project: prediction-constrained-topic-models   Author: dtak   File: calc_N_d_K__vb_qpiDir_qzCat.py    MIT License 6 votes vote down vote up
def make_initial_P_d_K(
        init_name,
        prng=np.random,
        alpha_K=None,
        init_P_d_K_list=None):
    K = alpha_K.size

    if init_name.count('warm'):
        return init_P_d_K_list.pop()
    elif init_name.count('uniform_sample'):
        return prng.dirichlet(np.ones(K))
    elif init_name.count('prior_sample'):
        return prng.dirichlet(alpha_K)
    elif init_name.count("prior_mean"):
        return alpha_K / np.sum(alpha_K) #np.zeros(K, dtype=alpha_K.dtype)
    else:
        raise ValueError("Unrecognized vb lstep_init_name: " + init_name) 
Example 3
Project: scanorama   Author: brianhie   File: t_sne_approx.py    MIT License 6 votes vote down vote up
def __init__(self, n_components=2, perplexity=30.0,
                 early_exaggeration=12.0, learning_rate=200.0, n_iter=1000,
                 n_iter_without_progress=300, min_grad_norm=1e-7,
                 metric="euclidean", init="random", verbose=0,
                 random_state=None, method='barnes_hut', angle=0.5):
        self.n_components = n_components
        self.perplexity = perplexity
        self.early_exaggeration = early_exaggeration
        self.learning_rate = learning_rate
        self.n_iter = n_iter
        self.n_iter_without_progress = n_iter_without_progress
        self.min_grad_norm = min_grad_norm
        self.metric = metric
        self.init = init
        self.verbose = verbose
        self.random_state = random_state
        self.method = method
        self.angle = angle 
Example 4
Project: edit-bone-constraint   Author: HolisticCoders   File: misc.py    GNU General Public License v3.0 6 votes vote down vote up
def create_random_state(random_state=None):
    """Returns a numpy.random.RandomState instance depending on input.

    Parameters
    ----------
    random_state : int or RandomState instance or None  optional (default=None)
        If int, return a numpy.random.RandomState instance set with seed=int.
        if numpy.random.RandomState instance, return it.
        if None or numpy.random, return the global random number generator used
        by numpy.random.
    """
    import numpy as np

    if random_state is None or random_state is np.random:
        return np.random.mtrand._rand
    if isinstance(random_state, np.random.RandomState):
        return random_state
    if isinstance(random_state, int):
        return np.random.RandomState(random_state)
    msg = '%r cannot be used to generate a numpy.random.RandomState instance'
    raise ValueError(msg % random_state) 
Example 5
Project: evolution-strategies-starter   Author: openai   File: viz.py    MIT License 6 votes vote down vote up
def main(env_id, policy_file, record, stochastic, extra_kwargs):
    import gym
    from gym import wrappers
    import tensorflow as tf
    from es_distributed.policies import MujocoPolicy
    import numpy as np

    env = gym.make(env_id)
    if record:
        import uuid
        env = wrappers.Monitor(env, '/tmp/' + str(uuid.uuid4()), force=True)

    if extra_kwargs:
        import json
        extra_kwargs = json.loads(extra_kwargs)

    with tf.Session():
        pi = MujocoPolicy.Load(policy_file, extra_kwargs=extra_kwargs)
        while True:
            rews, t = pi.rollout(env, render=True, random_stream=np.random if stochastic else None)
            print('return={:.4f} len={}'.format(rews.sum(), t))

            if record:
                env.close()
                return 
Example 6
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 6 votes vote down vote up
def set_random_state(self, random_state):
        """
        sets the random_state member of the object
        
        Args:
            random_state (int/np.random.RandomState/None): the random state initializer
        """
        
        self._random_state_init= random_state
        
        if random_state is None:
            self.random_state= np.random
        elif isinstance(random_state, int):
            self.random_state= np.random.RandomState(random_state)
        elif isinstance(random_state, np.random.RandomState):
            self.random_state= random_state
        elif random_state is np.random:
            self.random_state= random_state
        else:
            raise ValueError("random state cannot be initialized by " + str(random_state)) 
Example 7
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 6 votes vote down vote up
def __init__(self, n_neighbors= 5, threshold= 0.5, n_jobs= 1, random_state= None):
        """
        Constructor of the sampling object
        
        Args:
            n_neighbors (int): number of neighbors in nearest neighbor component
            threshold (float): threshold*n_neighbors gives the threshold z described in the paper
            n_jobs (int): number of parallel jobs
            random_state (int/RandomState/None): initializer of random_state, like in sklearn
        """
        super().__init__()
        self.check_greater_or_equal(n_neighbors, "n_neighbors", 1)
        self.check_in_range(threshold, "threshold", [0, 1])
        self.check_n_jobs(n_jobs, 'n_jobs')
        
        self.n_neighbors= n_neighbors
        self.threshold= threshold
        self.n_jobs= n_jobs
        
        # random state takes no effect for this technique
        self.set_random_state(random_state) 
Example 8
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 6 votes vote down vote up
def __init__(self, dataset, validator, cache_path= None, random_state= None):
        """
        Constructor of Folding object
        
        Args:
            dataset (dict): dataset dictionary with keys 'data', 'target' and 'DESCR'
            validator (obj): cross-validator object
            cache_path (str): path to cache directory
            random_state (int/np.random.RandomState/None): initializer of the random state
        """
        self.dataset= dataset
        self.db_name= self.dataset['name']
        self.validator= validator
        self.cache_path= cache_path
        self.filename= 'folding_' + self.db_name + '.pickle'
        self.db_size= len(dataset['data'])
        self.db_n_attr= len(dataset['data'][0])
        self.imbalanced_ratio= np.sum(self.dataset['target'] == 0)/np.sum(self.dataset['target'] == 1)
        self.random_state= random_state 
Example 9
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 6 votes vote down vote up
def __init__(self, sampling, classifiers, n_threads= None, random_state= None):
        """
        Constructor of an Evaluation object
        
        Args:
            sampling (obj): Sampling object
            classifiers (list(obj)): classifier objects
            n_threads (int/None): number of threads
            random_state (int/np.random.RandomState/None): random state initializer
        """
        self.sampling= sampling
        self.classifiers= classifiers
        self.n_threads= n_threads
        self.cache_path= sampling.cache_path
        self.filename= self.sampling.standardized_filename('eval')
        self.random_state= random_state
        
        self.labels= []
        for i in range(len(classifiers)):
            label= str((self.sampling.get_params(), classifiers[i].__class__.__name__, classifiers[i].get_params()))
            self.labels.append(label) 
Example 10
Project: DRCOG_Urbansim   Author: apdjustino   File: misc.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def test_uniquend(self):
        from numpy.random import randint
             
        b = randint(0, 5, size=100)
        result = uniquend(b)
        self.assertTrue( all([i in b for i in result]) )
        self.assertTrue( all([i in result for i in b]) )
        self.assertTrue( set(b) == set(result) )
        
        b = randint(0, 3, size=100)
        b.resize((50,2))
        result = uniquend(b)
        self.assertTrue( all([i in b for i in result]) )
        self.assertTrue( all([i in result for i in b]) )
        self.assertTrue( set(totuple(b)) == set(totuple(result)) )
        
        b = randint(0, 2, size=54)
        b.resize((9,3,2))
        result = uniquend(b)
        self.assertTrue( all([i in b for i in result]) )
        self.assertTrue( all([i in result for i in b]) )
        self.assertTrue( set(totuple(b)) == set(totuple(result)) ) 
Example 11
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 6 votes vote down vote up
def elastic_transform(image, alpha=1000, sigma=30, spline_order=1, mode='nearest', random_state=np.random):
    """Elastic deformation of image as described in [Simard2003]_.
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
       Convolutional Neural Networks applied to Visual Document Analysis", in
       Proc. of the International Conference on Document Analysis and
       Recognition, 2003.
    """
    assert image.ndim == 3
    shape = image.shape[:2]

    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1),
                         sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1),
                         sigma, mode="constant", cval=0) * alpha

    x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), indexing='ij')
    indices = [np.reshape(x + dx, (-1, 1)), np.reshape(y + dy, (-1, 1))]
    result = np.empty_like(image)
    for i in range(image.shape[2]):
        result[:, :, i] = map_coordinates(
            image[:, :, i], indices, order=spline_order, mode=mode).reshape(shape)
    return result 
Example 12
Project: LaserTOF   Author: kyleuckert   File: construct.py    MIT License 6 votes vote down vote up
def rand(m, n, density=0.01, format="coo", dtype=None, random_state=None):
    """Generate a sparse matrix of the given shape and density with uniformly
    distributed values.

    Parameters
    ----------
    m, n : int
        shape of the matrix
    density : real, optional
        density of the generated matrix: density equal to one means a full
        matrix, density of 0 means a matrix with no non-zero items.
    format : str, optional
        sparse matrix format.
    dtype : dtype, optional
        type of the returned matrix values.
    random_state : {numpy.random.RandomState, int}, optional
        Random number generator or random seed. If not given, the singleton
        numpy.random will be used.

    Notes
    -----
    Only float types are supported for now.

    """
    return random(m, n, density, format, dtype, random_state) 
Example 13
Project: LaserTOF   Author: kyleuckert   File: _util.py    MIT License 6 votes vote down vote up
def check_random_state(seed):
    """Turn seed into a np.random.RandomState instance

    If seed is None (or np.random), return the RandomState singleton used
    by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    """
    if seed is None or seed is np.random:
        return np.random.mtrand._rand
    if isinstance(seed, (numbers.Integral, np.integer)):
        return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return seed
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
                     ' instance' % seed) 
Example 14
Project: LaserTOF   Author: kyleuckert   File: _multivariate.py    MIT License 6 votes vote down vote up
def rvs(self, alpha, size=1, random_state=None):
        """
        Draw random samples from a Dirichlet distribution.

        Parameters
        ----------
        %(_dirichlet_doc_default_callparams)s
        size : int, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of size (`size`, `N`), where `N` is the
            dimension of the random variable.

        """
        alpha = _dirichlet_check_parameters(alpha)
        random_state = self._get_random_state(random_state)
        return random_state.dirichlet(alpha, size=size) 
Example 15
Project: LaserTOF   Author: kyleuckert   File: _multivariate.py    MIT License 6 votes vote down vote up
def rvs(self, n, p, size=None, random_state=None):
        """
        Draw random samples from a Multinomial distribution.

        Parameters
        ----------
        %(_doc_default_callparams)s
        size : integer or iterable of integers, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of shape (`size`, `len(p)`)

        Notes
        -----
        %(_doc_callparams_note)s
        """
        n, p, npcond = self._process_parameters(n, p)
        random_state = self._get_random_state(random_state)
        return random_state.multinomial(n, p, size) 
Example 16
Project: Autoenv   Author: intelligent-control-lab   File: cma_es_lib.py    MIT License 6 votes vote down vote up
def mahalanobis_norm(self, dx):
        """compute the Mahalanobis norm that is induced by the adapted
        sample distribution, covariance matrix ``C`` times ``sigma**2``,
        including ``sigma_vec``. The expected Mahalanobis distance to
        the sample mean is about ``sqrt(dimension)``.

        Argument
        --------
        A *genotype* difference `dx`.

        Example
        -------
        >>> import cma, numpy
        >>> es = cma.CMAEvolutionStrategy(numpy.ones(10), 1)
        >>> xx = numpy.random.randn(2, 10)
        >>> d = es.mahalanobis_norm(es.gp.geno(xx[0]-xx[1]))

        `d` is the distance "in" the true sample distribution,
        sampled points have a typical distance of ``sqrt(2*es.N)``,
        where ``es.N`` is the dimension, and an expected distance of
        close to ``sqrt(N)`` to the sample mean. In the example,
        `d` is the Euclidean distance, because C = I and sigma = 1.

        """
        return sqrt(sum((self.D**-1. * np.dot(self.B.T, dx / self.sigma_vec))**2)) / self.sigma 
Example 17
Project: epyc   Author: simoninireland   File: test_summaryexperiments.py    GNU General Public License v2.0 5 votes vote down vote up
def do( self, params ):
        v = numpy.random.random()
        self._allvalues.append(v)
        return dict(result = v) 
Example 18
Project: epyc   Author: simoninireland   File: test_summaryexperiments.py    GNU General Public License v2.0 5 votes vote down vote up
def do( self, params ):
        v = numpy.random.random()
        if v < 0.5:
            # experiment succeeds
            self._allvalues.append(v)
            self._ran = self._ran + 1
            return dict(result = v)
        else:
            # experiment fails
            raise Exception("Failing") 
Example 19
Project: epyc   Author: simoninireland   File: test_summaryexperiments.py    GNU General Public License v2.0 5 votes vote down vote up
def do( self, params ):
        v = numpy.random.random()
        self._values.append(v)
        return dict(result = v) 
Example 20
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: common.py    Apache License 2.0 5 votes vote down vote up
def random_seed(seed=None):
    """
    Runs a code block with a new seed for np, mx and python's random.

    Parameters
    ----------

    seed : the seed to pass to np.random, mx.random and python's random.

    To impose rng determinism, invoke e.g. as in:

    with random_seed(1234):
        ...

    To impose rng non-determinism, invoke as in:

    with random_seed():
        ...

    Upon conclusion of the block, the rng's are returned to
    a state that is a function of their pre-block state, so
    any prior non-determinism is preserved.

    """

    try:
        next_seed = np.random.randint(0, np.iinfo(np.int32).max)
        if seed is None:
            np.random.seed()
            seed = np.random.randint(0, np.iinfo(np.int32).max)
        logger = default_logger()
        logger.debug('Setting np, mx and python random seeds = %s', seed)
        np.random.seed(seed)
        mx.random.seed(seed)
        random.seed(seed)
        yield
    finally:
        # Reinstate prior state of np.random and other generators
        np.random.seed(next_seed)
        mx.random.seed(next_seed)
        random.seed(next_seed) 
Example 21
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: common.py    Apache License 2.0 5 votes vote down vote up
def random_seed(seed=None):
    """
    Runs a code block with a new seed for np, mx and python's random.

    Parameters
    ----------

    seed : the seed to pass to np.random, mx.random and python's random.

    To impose rng determinism, invoke e.g. as in:

    with random_seed(1234):
        ...

    To impose rng non-determinism, invoke as in:

    with random_seed():
        ...

    Upon conclusion of the block, the rng's are returned to
    a state that is a function of their pre-block state, so
    any prior non-determinism is preserved.

    """

    try:
        next_seed = np.random.randint(0, np.iinfo(np.int32).max)
        if seed is None:
            np.random.seed()
            seed = np.random.randint(0, np.iinfo(np.int32).max)
        logger = default_logger()
        logger.debug('Setting np, mx and python random seeds = %s', seed)
        np.random.seed(seed)
        mx.random.seed(seed)
        random.seed(seed)
        yield
    finally:
        # Reinstate prior state of np.random and other generators
        np.random.seed(next_seed)
        mx.random.seed(next_seed)
        random.seed(next_seed) 
Example 22
Project: FCOS_GluonCV   Author: DetectionTeamUCAS   File: common.py    Apache License 2.0 5 votes vote down vote up
def random_seed(seed=None):
    """
    Runs a code block with a new seed for np, mx and python's random.

    Parameters
    ----------

    seed : the seed to pass to np.random, mx.random and python's random.

    To impose rng determinism, invoke e.g. as in:

    with random_seed(1234):
        ...

    To impose rng non-determinism, invoke as in:

    with random_seed():
        ...

    Upon conclusion of the block, the rng's are returned to
    a state that is a function of their pre-block state, so
    any prior non-determinism is preserved.

    """

    try:
        next_seed = np.random.randint(0, np.iinfo(np.int32).max)
        if seed is None:
            np.random.seed()
            seed = np.random.randint(0, np.iinfo(np.int32).max)
        logger = default_logger()
        logger.debug('Setting np, mx and python random seeds = %s', seed)
        np.random.seed(seed)
        mx.random.seed(seed)
        random.seed(seed)
        yield
    finally:
        # Reinstate prior state of np.random and other generators
        np.random.seed(next_seed)
        mx.random.seed(next_seed)
        random.seed(next_seed) 
Example 23
Project: SLiPy   Author: glentner   File: adabinner.py    GNU General Public License v2.0 5 votes vote down vote up
def __doit1d(x, hi1=None, hi2=None, thresh=None):
	hhs ={}
	for curhi in range(hi1,hi2+1):
		hh = quick_hist.quick_hist((x,), range=((0, 1),),
								nbins=[2**curhi])
		hhs[curhi]=hh

	hh0 = hhs[hi2] * 1 # accumulator of the result
	area = hh0 * 0

	two = 2
	poiss = scipy.stats.poisson(thresh)

	DeepenOrNot = lambda x: random.random() < poiss.cdf(x)
	#DeepenOrNot = lambda x: x>thresh

	def doitit(it, i):
		curhhs = hhs[it]
		if it==hi2:
			hh[i:i+2] = curhhs[i:i+2]
			area[i:i+2] = 2**(-it)
		else:
			for ii in range(i, i+2):
				curval = curhhs[ii]
				if DeepenOrNot(curval):
					doitit(it+1, ii*two)
				else:
					dx=2**(hi2-it)
					hh0[ii*dx:(ii+1)*dx]=curval
					area[ii*dx:(ii+1)*dx] = 2**(-it)
	n1 = 2**hi1
	dn = 2**(hi2-hi1)

	for i in range(n1):
			if DeepenOrNot(hhs[hi1][i]):
				doitit(hi1+1,i*two)
			else:
				hh0[i*dn:(i+1)*dn] = hhs[hi1][i]
				area[i*dn:(i+1)*dn] = 2**(-hi1)
	return hh0*1./(2**hi1*area) 
Example 24
Project: EXOSIMS   Author: dsavransky   File: test_MissionSim.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_random_seed_initialize_full(self):
        r"""Test random_seed_initialize method (longer).

        Method: Initialize with a specific random seed, and ensure the numpy
        RNG seeding mechanism was indeed called by the __init__ function.
        """
        # Note: the mission.__init__() method, in building up planets, etc., calls
        # the numpy RNG many times.  All these calls make it impossible to check the 
        # RNG state just after the object is again available to this test code.
        # So, to test, we have to ensure that np.random.seed is
        # called correctly instead of checking the RNG state after __init__ returns.

        # set up the seed, and the specs addition that includes it
        seed = 1234567890
        specs = {'seed': seed}

        # plugs in our own mock object to monitor calls to np.random.seed()
        with assertMethodIsCalled(np.random, "seed") as rng_seed_mock:
            # initialize the object
            with RedirectStreams(stdout=self.dev_null):
                mission = self.fixture(scriptfile=SimpleScript, **specs)
            # ensure np.random.seed was called once, with the given seed provided
            self.assertEqual(len(rng_seed_mock.method_args), 1, 'RNG was not seeded.')
            self.assertEqual(len(rng_seed_mock.method_args[0]), 1, 'RNG seed arguments incorrect.')
            self.assertEqual(rng_seed_mock.method_args[0][0], seed)
            # keyword argument should be an empty dictionary
            self.assertEqual(len(rng_seed_mock.method_kwargs), 1, 'RNG seeded with unexpected arguments.')
            self.assertDictEqual(rng_seed_mock.method_kwargs[0], {}, 'RNG seeded with unexpected arguments.')
        # might as well validate the object
        self.validate_object(mission) 
Example 25
Project: Anamoly-Detection   Author: msmsk05   File: combination.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def aom(scores, n_buckets=5, method='static', bootstrap_estimators=False,
        random_state=None):
    """Average of Maximum - An ensemble method for combining multiple
    estimators. See :cite:`aggarwal2015theoretical` for details.

    First dividing estimators into subgroups, take the maximum score as the
    subgroup score. Finally, take the average of all subgroup outlier scores.

    Parameters
    ----------
    scores : numpy array of shape (n_samples, n_estimators)
        The score matrix outputted from various estimators

    n_buckets : int, optional (default=5)
        The number of subgroups to build

    method : str, optional (default='static')
        {'static', 'dynamic'}, if 'dynamic', build subgroups
        randomly with dynamic bucket size.

    bootstrap_estimators : bool, optional (default=False)
        Whether estimators are drawn with replacement.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the
        random number generator; If RandomState instance, random_state is
        the random number generator; If None, the random number generator
        is the RandomState instance used by `np.random`.

    Returns
    -------
    combined_scores : Numpy array of shape (n_samples,)
        The combined outlier scores.

    """
    return _aom_moa_helper('AOM', scores, n_buckets, method,
                           bootstrap_estimators, random_state) 
Example 26
Project: Anamoly-Detection   Author: msmsk05   File: combination.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def moa(scores, n_buckets=5, method='static', bootstrap_estimators=False,
        random_state=None):
    """Maximization of Average - An ensemble method for combining multiple
    estimators. See :cite:`aggarwal2015theoretical` for details.

    First dividing estimators into subgroups, take the average score as the
    subgroup score. Finally, take the maximization of all subgroup outlier
    scores.

    Parameters
    ----------
    scores : numpy array of shape (n_samples, n_estimators)
        The score matrix outputted from various estimators

    n_buckets : int, optional (default=5)
        The number of subgroups to build

    method : str, optional (default='static')
        {'static', 'dynamic'}, if 'dynamic', build subgroups
        randomly with dynamic bucket size.

    bootstrap_estimators : bool, optional (default=False)
        Whether estimators are drawn with replacement.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the
        random number generator; If RandomState instance, random_state is
        the random number generator; If None, the random number generator
        is the RandomState instance used by `np.random`.

    Returns
    -------
    combined_scores : Numpy array of shape (n_samples,)
        The combined outlier scores.

    """
    return _aom_moa_helper('MOA', scores, n_buckets, method,
                           bootstrap_estimators, random_state) 
Example 27
Project: Anamoly-Detection   Author: msmsk05   File: feature_bagging.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _set_random_states(estimator, random_state=None):
    """Sets fixed random_state parameters for an estimator. Internal use only.
    Modified from sklearn/base.py

    Finds all parameters ending ``random_state`` and sets them to integers
    derived from ``random_state``.

    Parameters
    ----------
    estimator : estimator supporting get/set_params
        Estimator with potential randomness managed by random_state
        parameters.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Notes
    -----
    This does not necessarily set *all* ``random_state`` attributes that
    control an estimator's randomness, only those accessible through
    ``estimator.get_params()``.  ``random_state``s not controlled include
    those belonging to:

        * cross-validation splitters
        * ``scipy.stats`` rvs
    """
    random_state = check_random_state(random_state)
    to_set = {}
    for key in sorted(estimator.get_params(deep=True)):
        if key == 'random_state' or key.endswith('__random_state'):
            to_set[key] = random_state.randint(MAX_INT)

    if to_set:
        estimator.set_params(**to_set) 
Example 28
Project: edit-bone-constraint   Author: HolisticCoders   File: misc.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, rng=None):
            import numpy
            if rng is None:
                self._rng = numpy.random.mtrand._rand
            self._rng = rng 
Example 29
Project: edit-bone-constraint   Author: HolisticCoders   File: misc.py    GNU General Public License v3.0 5 votes vote down vote up
def random(self):
        return self._rng.random_sample() 
Example 30
Project: edit-bone-constraint   Author: HolisticCoders   File: misc.py    GNU General Public License v3.0 5 votes vote down vote up
def create_py_random_state(random_state=None):
    """Returns a random.Random instance depending on input.

    Parameters
    ----------
    random_state : int or random number generator or None (default=None)
        If int, return a random.Random instance set with seed=int.
        if random.Random instance, return it.
        if None or the `random` package, return the global random number
        generator used by `random`.
        if np.random package, return the global numpy random number
        generator wrapped in a PythonRandomInterface class.
        if np.random.RandomState instance, return it wrapped in
        PythonRandomInterface
        if a PythonRandomInterface instance, return it
    """
    import random
    try:
        import numpy as np
        if random_state is np.random:
            return PythonRandomInterface(np.random.mtrand._rand)
        if isinstance(random_state, np.random.RandomState):
            return PythonRandomInterface(random_state)
        if isinstance(random_state, PythonRandomInterface):
            return random_state
        has_numpy = True
    except ImportError:
        has_numpy = False

    if random_state is None or random_state is random:
        return random._inst
    if isinstance(random_state, random.Random):
        return random_state
    if isinstance(random_state, int):
        return random.Random(random_state)
    msg = '%r cannot be used to generate a random.Random instance'
    raise ValueError(msg % random_state)


# fixture for nose tests 
Example 31
Project: ConvLab   Author: ConvLab   File: search.py    MIT License 5 votes vote down vote up
def build_config_space(spec):
    '''
    Build ray config space from flattened spec.search
    Specify a config space in spec using `"{key}__{space_type}": {v}`.
    Where `{space_type}` is `grid_search` of `ray.tune`, or any function name of `np.random`:
    - `grid_search`: str/int/float. v = list of choices
    - `choice`: str/int/float. v = list of choices
    - `randint`: int. v = [low, high)
    - `uniform`: float. v = [low, high)
    - `normal`: float. v = [mean, stdev)

    For example:
    - `"explore_anneal_epi__randint": [10, 60],` will sample integers uniformly from 10 to 60 for `explore_anneal_epi`,
    - `"lr__uniform": [0.001, 0.1]`, and it will sample `lr` using `np.random.uniform(0.001, 0.1)`

    If any key uses `grid_search`, it will be combined exhaustively in combination with other random sampling.
    '''
    space_types = ('grid_search', 'choice', 'randint', 'uniform', 'normal')
    config_space = {}
    for k, v in util.flatten_dict(spec['search']).items():
        key, space_type = k.split('__')
        assert space_type in space_types, f'Please specify your search variable as {key}__<space_type> in one of {space_types}'
        if space_type == 'grid_search':
            config_space[key] = tune.grid_search(v)
        elif space_type == 'choice':
            config_space[key] = tune.sample_from(lambda spec, v=v: random.choice(v))
        else:
            np_fn = getattr(np.random, space_type)
            config_space[key] = tune.sample_from(lambda spec, v=v: np_fn(*v))
    return config_space 
Example 32
Project: imm   Author: tscholak   File: processes.py    Apache License 2.0 5 votes vote down vote up
def random_state(self):
        """
        Get or set the RandomState object for generating random variates.
        This can be either None or an existing RandomState object.
        If None (or np.random), use the RandomState singleton used by
        np.random. If already a RandomState instance, use it.
        If an int, use a new RandomState instance seeded with seed.
        """
        return self._random_state 
Example 33
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def generate_parameter_combinations(cls, dictionary, num= None):
        """
        Generates reasonable paramter combinations
        Args:
            dictionary (dict): dictionary of paramter ranges
            num (int): maximum number of combinations to generate
        """
        combinations= [dict(zip(list(dictionary.keys()), p)) for p in list(itertools.product(*list(dictionary.values())))]
        if num is None:
            return combinations
        else:
            if hasattr(cls, 'random_state'):
                return cls.random_state.choice(combinations, num, replace= False)
            else:
                return np.random.choice(combinations, num, replace= False) 
Example 34
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def __init__(self, random_state= None):
        """
        Constructor of the NoSMOTE object.
        
        random_state (int/np.random.RandomState/None): dummy parameter for the
                        compatibility of interfaces
        """
        super().__init__() 
Example 35
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def __init__(self, proportion= 1.0, n_neighbors= 5, rate= 0.1, n_step= 50, n_jobs= 1, random_state= None):
        """
        Constructor of the sampling object
        
        Args:
            proportion (float): proportion of the difference of n_maj and n_min to sample
                                    e.g. 1.0 means that after sampling the number of minority
                                    samples will be equal to the number of majority samples
            n_neighbors (int): number of neighbors in nearest neighbors component
            rate (float): [0,1] rate of minority samples to turn into majority
            n_step (int): number of random configurations to check for new samples
            n_jobs (int): number of parallel jobs
            random_state (int/RandomState/None): initializer of random_state, like in sklearn
        """
        super().__init__()
        self.check_greater_or_equal(proportion, "proportion", 0)
        self.check_greater_or_equal(n_neighbors, "n_neighbors", 1)
        self.check_in_range(rate, "rate", [0,1])
        self.check_greater_or_equal(n_step, "n_step", 1)
        self.check_n_jobs(n_jobs, 'n_jobs')
        
        self.proportion= proportion
        self.n_neighbors= n_neighbors
        self.rate= rate
        self.n_step= n_step
        self.n_jobs= n_jobs
        
        self.set_random_state(random_state) 
Example 36
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def add_connection(self):
        """
        Adds a random input connection to the neuron
        """
        if len(self.mask) < self.d:
            self.mask= np.hstack([self.mask, np.array(self.random_state.choice(list(set(range(self.d)).difference(set(self.mask.tolist())))))])
            self.input_weights= np.hstack([self.input_weights, (self.random_state.random_sample()-0.5)*self.I]) 
Example 37
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def delete_connection(self):
        """
        Deletes a random input connection
        """
        if len(self.mask) > 1:
            idx= self.random_state.randint(len(self.mask))
            self.mask= np.delete(self.mask, idx)
            self.input_weights= np.delete(self.input_weights, idx) 
Example 38
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def __init__(self, activation= 'relu', hidden_layer_fraction= 0.1, alpha= 0.0001, random_state= None):
        """
        Constructor of the MLPClassifier
        
        Args:
            activation (str): name of the activation function
            hidden_layer_fraction (float): fraction of the hidden neurons of the number of input dimensions
            alpha (float): alpha parameter of the MLP classifier
            random_state (int/np.random.RandomState/None): initializer of the random state
        """
        self.activation= activation
        self.hidden_layer_fraction= hidden_layer_fraction
        self.alpha= alpha
        self.random_state= random_state 
Example 39
Project: smote_variants   Author: gykovacs   File: _smote_variants.py    MIT License 5 votes vote down vote up
def _cache_samplings(folding, samplers, scaler, max_n_sampler_par_comb= 35, n_jobs= 1, random_state= None):
    """
    
    """
    _logger.info("create sampling objects")
    sampling_objs= []
    
    if isinstance(random_state, int):
        random_state= np.random.RandomState(random_state)
    elif random_state is None:
        random_state= np.random
    
    for s in samplers:
    
        sampling_par_comb= s.parameter_combinations()
        sampling_par_comb= random_state.choice(sampling_par_comb, min([len(sampling_par_comb), max_n_sampler_par_comb]), replace= False)
        
        for spc in sampling_par_comb:
            sampling_objs.append(Sampling(folding, s, spc, scaler, random_state))
            
    # sorting sampling objects to optimize execution
    def key(x):
        if isinstance(x.sampler, ADG) or isinstance(x.sampler, AMSCO) or isinstance(x.sampler, DSRBF):
            if 'proportion' in x.sampler_parameters:
                return 30 + x.sampler_parameters['proportion']
            else:
                return 30
        elif 'proportion' in x.sampler_parameters:
            return x.sampler_parameters['proportion']
        elif OverSampling.cat_memetic in x.sampler.categories:
            return 20
        else:
            return 10
    
    sampling_objs= list(reversed(sorted(sampling_objs, key= key)))
    
    # executing sampling in parallel
    _logger.info("executing %d sampling in parallel" % len(sampling_objs))
    Parallel(n_jobs= n_jobs, batch_size= 1)(delayed(s.cache_sampling)() for s in sampling_objs)
    
    return sampling_objs 
Example 40
Project: DRCOG_Urbansim   Author: apdjustino   File: misc.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def sample_1d(population, k, probabilities):
    """Called from sample, here probabilities must be 1D array."""
    from numpy import searchsorted
    from numpy.random import random

    n = population.size
    if n <> probabilities.shape[0]:
        raise StandardError, "Mismatch in size of population and probabilities."
    cumulative_probability = probabilities.cumsum()
    if cumulative_probability[n-1]<=0:
        raise StandardError, "Sum of probabilities must be > 0."
    cumulative_probability = cumulative_probability/float(cumulative_probability[n-1])
    draw = random([k])
    choices = searchsorted(cumulative_probability, draw)
    return population[choices] 
Example 41
Project: DRCOG_Urbansim   Author: apdjustino   File: misc.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def sample(population, k, probabilities=None):
    """Return a sample (array) of size k from the 'population' according to the given 'probabilities' (sampling with replacement).
    'probabilities' can be 'None', 1D or 2D array. In case of 2D array, i-th row is considered as probabilities
    for i-th draw. The probabilities do not need to sum to 1, they are rescaled within the procedure. They should
    be positive and their sum should be > 0.
    """
    from numpy import array, reshape, where, alltrue
    from numpy.random import random

    if isinstance(population, list):
        population = array(population)
    n = population.size
    if probabilities == None:
        probabilities = array(n*[1.0/n])
    if isinstance(probabilities, list):
        probabilities = array(probabilities)

    if probabilities.ndim <= 1:
        return sample_1d(population, k, probabilities)

    if (n <> probabilities.shape[1]) or (k <> probabilities.shape[0]) :
        raise StandardError, "Mismatch in size of population and probabilities."
    cumulative_probability = probabilities.cumsum(axis=1)
    if not alltrue(cumulative_probability[:,n-1]):
        raise StandardError, "Sums of probabilities must be > 0."
    cumulative_probability = cumulative_probability/reshape(cumulative_probability[:,n-1],(k,1)).astype('float32')
    draw = random([k])
    choices = reshape(n-where(reshape(draw, (k,1)) <= cumulative_probability,1,0).sum(axis=1), (k,))
    if choices.max() >= n:
        raise StandardError, "Something is wrong with the probabilities."
    return population[choices] 
Example 42
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def random_num_generator(config, random_state=np.random):
    if config[0] == 'uniform':
        ret = random_state.uniform(config[1], config[2], 1)[0]
    elif config[0] == 'lognormal':
        ret = random_state.lognormal(config[1], config[2], 1)[0]
    else:
        print(config)
        raise Exception('unsupported format')
    return ret 
Example 43
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def poisson_downsampling(image, peak, random_state=np.random):
    if not isinstance(image, np.ndarray):
        imgArr = np.array(image, dtype='float32')
    else:
        imgArr = image.astype('float32')
    Q = imgArr.max(axis=(0, 1)) / peak
    if Q[0] == 0:
        return imgArr
    ima_lambda = imgArr / Q
    noisy_img = random_state.poisson(lam=ima_lambda)
    return noisy_img.astype('float32') 
Example 44
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def __init__(self, peak, random_state=np.random):
        self.peak = peak
        self.random_state = random_state 
Example 45
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def __init__(self, mean, sigma, random_state=np.random):
        self.sigma = sigma
        self.mean = mean
        self.random_state = random_state 
Example 46
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def __init__(self, sigma, random_state=np.random):
        self.sigma = sigma
        self.random_state = random_state 
Example 47
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def __init__(self, sigma, peak, random_state=np.random):
        self.sigma = sigma
        self.peak = peak
        self.random_state = random_state 
Example 48
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def __init__(self, size, random_state=np.random):
        if isinstance(size, numbers.Number):
            self.size = (int(size), int(size))
        else:
            self.size = size
        self.random_state = random_state 
Example 49
Project: Attention-Gated-Networks   Author: ozan-oktay   File: myImageTransformations.py    MIT License 5 votes vote down vote up
def __init__(self, angle_range=(0.0, 360.0), axes=(0, 1), mode='reflect', random_state=np.random):
        assert isinstance(angle_range, tuple)
        self.angle_range = angle_range
        self.random_state = random_state
        self.axes = axes
        self.mode = mode 
Example 50
Project: RefineDet_MxNet   Author: DetectionTeamUCAS   File: common.py    Apache License 2.0 5 votes vote down vote up
def random_seed(seed=None):
    """
    Runs a code block with a new seed for np, mx and python's random.

    Parameters
    ----------

    seed : the seed to pass to np.random, mx.random and python's random.

    To impose rng determinism, invoke e.g. as in:

    with random_seed(1234):
        ...

    To impose rng non-determinism, invoke as in:

    with random_seed():
        ...

    Upon conclusion of the block, the rng's are returned to
    a state that is a function of their pre-block state, so
    any prior non-determinism is preserved.

    """

    try:
        next_seed = np.random.randint(0, np.iinfo(np.int32).max)
        if seed is None:
            np.random.seed()
            seed = np.random.randint(0, np.iinfo(np.int32).max)
        logger = default_logger()
        logger.debug('Setting np, mx and python random seeds = %s', seed)
        np.random.seed(seed)
        mx.random.seed(seed)
        random.seed(seed)
        yield
    finally:
        # Reinstate prior state of np.random and other generators
        np.random.seed(next_seed)
        mx.random.seed(next_seed)
        random.seed(next_seed)