コード例 #1
0
ファイル: random.py プロジェクト: wsw1066/cpython
    def choices(self, population, weights=None, *, cum_weights=None, k=1):
        """Return a k sized list of population elements chosen with replacement.

        If the relative weights or cumulative weights are not specified,
        the selections are made with equal probability.

        """
        random = self.random
        n = len(population)
        if cum_weights is None:
            if weights is None:
                _int = int
                n += 0.0  # convert to float for a small speed improvement
                return [population[_int(random() * n)] for i in range(k)]
            cum_weights = list(_accumulate(weights))
        elif weights is not None:
            raise TypeError(
                'Cannot specify both weights and cumulative weights')
        if len(cum_weights) != n:
            raise ValueError(
                'The number of weights does not match the population')
        bisect = _bisect
        total = cum_weights[-1] + 0.0  # convert to float
        hi = n - 1
        return [
            population[bisect(cum_weights,
                              random() * total, 0, hi)] for i in range(k)
        ]
コード例 #2
0
def choices(population, weights=None, *, cum_weights=None, k=1):
    """Return a k sized list of population elements chosen with replacement.
    If the relative weights or cumulative weights are not specified,
    the selections are made with equal probability.

    from: https://github.com/python/cpython/blob/master/Lib/random.py
    """
    n = len(population)
    if cum_weights is None:
        if weights is None:
            _int = int
            n += 0.0  # convert to float for a small speed improvement
            return [
                population[_int(random.random() * n)]
                for i in _repeat(None, k)
            ]
        cum_weights = list(_accumulate(weights))
    elif weights is not None:
        raise TypeError('Cannot specify both weights and cumulative weights')
    if len(cum_weights) != n:
        raise ValueError('The number of weights does not match the population')
    total = cum_weights[-1] + 0.0  # convert to float
    if total <= 0.0:
        raise ValueError('Total of weights must be greater than zero')
    bisect = _bisect
    hi = n - 1
    return [
        population[bisect(cum_weights,
                          random.random() * total, 0, hi)]
        for i in _repeat(None, k)
    ]
コード例 #3
0
def choices(population, weights=None, *, cum_weights=None, k=1):
    n = len(population)
    if cum_weights is None:
        if weights is None:
            _int = int
            n += 0.0  # convert to float for a small speed improvement
            return [population[_int(random.random() * n)] for i in _repeat(None, k)]
        cum_weights = list(_accumulate(weights))
    elif weights is not None:
        raise TypeError('Cannot specify both weights and cumulative weights')
    if len(cum_weights) != n:
        raise ValueError('The number of weights does not match the population')
    bisect = _bisect
    total = cum_weights[-1] + 0.0  # convert to float
    hi = n - 1
    return [population[bisect(cum_weights, random.random() * total, 0, hi)]
            for i in _repeat(None, k)]
コード例 #4
0
    def choices(self, population, weights=None, *, cum_weights=None, k=1):
        """Return a k sized list of population elements chosen with replacement.

        If the relative weights or cumulative weights are not specified,
        the selections are made with equal probability.

        """
        random = self.random
        n = len(population)
        if cum_weights is None:
            if weights is None:
                floor = _floor
                n += 0.0  # convert to float for a small speed improvement
                return [
                    population[floor(random() * n)] for i in _repeat(None, k)
                ]
            try:
                cum_weights = list(_accumulate(weights))
            except TypeError:
                if not isinstance(weights, int):
                    raise
                k = weights
                raise TypeError(
                    f'The number of choices must be a keyword argument: {k=}'
                ) from None
        elif weights is not None:
            raise TypeError(
                'Cannot specify both weights and cumulative weights')
        if len(cum_weights) != n:
            raise ValueError(
                'The number of weights does not match the population')
        total = cum_weights[-1] + 0.0  # convert to float
        if total <= 0.0:
            raise ValueError('Total of weights must be greater than zero')
        if not _isfinite(total):
            raise ValueError('Total of weights must be finite')
        bisect = _bisect
        hi = n - 1
        return [
            population[bisect(cum_weights,
                              random() * total, 0, hi)]
            for i in _repeat(None, k)
        ]
コード例 #5
0
    def __init__(self, curve):
        """Re-parameterize a spline to have constant speed.

        For splines in Euclidean space this amounts to arc-length
        parameterization.

        However, this class is implemented in a way that also allows using
        rotation splines which will be re-parameterized to have constant
        angular speed.

        The parameter *s* represents the cumulative arc-length or the
        cumulative rotation angle, respectively.

        """
        self.curve = curve
        lengths = (
            self._integrated_speed(i, t0, t1)
            for i, (t0, t1) in enumerate(zip(curve.grid, curve.grid[1:])))

        # NB: "initial" argument to itertools.accumulate since Python 3.8
        #self.grid = list(_accumulate(lengths, initial=0))
        self.grid = [0] + list(_accumulate(lengths))
コード例 #6
0
ファイル: random.py プロジェクト: Eyepea/cpython
    def choices(self, population, weights=None, *, cum_weights=None, k=1):
        """Return a k sized list of population elements chosen with replacement.

        If the relative weights or cumulative weights are not specified,
        the selections are made with equal probability.

        """
        random = self.random
        n = len(population)
        if cum_weights is None:
            if weights is None:
                _int = int
                n += 0.0    # convert to float for a small speed improvement
                return [population[_int(random() * n)] for i in _repeat(None, k)]
            cum_weights = list(_accumulate(weights))
        elif weights is not None:
            raise TypeError('Cannot specify both weights and cumulative weights')
        if len(cum_weights) != n:
            raise ValueError('The number of weights does not match the population')
        bisect = _bisect
        total = cum_weights[-1] + 0.0   # convert to float
        hi = n - 1
        return [population[bisect(cum_weights, random() * total, 0, hi)]
                for i in _repeat(None, k)]
コード例 #7
0
    def sample(self, population, k, *, counts=None):
        """Chooses k unique random elements from a population sequence.

        Returns a new list containing elements from the population while
        leaving the original population unchanged.  The resulting list is
        in selection order so that all sub-slices will also be valid random
        samples.  This allows raffle winners (the sample) to be partitioned
        into grand prize and second place winners (the subslices).

        Members of the population need not be hashable or unique.  If the
        population contains repeats, then each occurrence is a possible
        selection in the sample.

        Repeated elements can be specified one at a time or with the optional
        counts parameter.  For example:

            sample(['red', 'blue'], counts=[4, 2], k=5)

        is equivalent to:

            sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)

        To choose a sample from a range of integers, use range() for the
        population argument.  This is especially fast and space efficient
        for sampling from a large population:

            sample(range(10000000), 60)

        """

        # Sampling without replacement entails tracking either potential
        # selections (the pool) in a list or previous selections in a set.

        # When the number of selections is small compared to the
        # population, then tracking selections is efficient, requiring
        # only a small set and an occasional reselection.  For
        # a larger number of selections, the pool tracking method is
        # preferred since the list takes less space than the
        # set and it doesn't suffer from frequent reselections.

        # The number of calls to _randbelow() is kept at or near k, the
        # theoretical minimum.  This is important because running time
        # is dominated by _randbelow() and because it extracts the
        # least entropy from the underlying random number generators.

        # Memory requirements are kept to the smaller of a k-length
        # set or an n-length list.

        # There are other sampling algorithms that do not require
        # auxiliary memory, but they were rejected because they made
        # too many calls to _randbelow(), making them slower and
        # causing them to eat more entropy than necessary.

        if not isinstance(population, _Sequence):
            raise TypeError("Population must be a sequence.  "
                            "For dicts or sets, use sorted(d).")
        n = len(population)
        if counts is not None:
            cum_counts = list(_accumulate(counts))
            if len(cum_counts) != n:
                raise ValueError('The number of counts does not match the population')
            total = cum_counts.pop()
            if not isinstance(total, int):
                raise TypeError('Counts must be integers')
            if total <= 0:
                raise ValueError('Total of counts must be greater than zero')
            selections = self.sample(range(total), k=k)
            bisect = _bisect
            return [population[bisect(cum_counts, s)] for s in selections]
        randbelow = self._randbelow
        if not 0 <= k <= n:
            raise ValueError("Sample larger than population or is negative")
        result = [None] * k
        setsize = 21        # size of a small set minus size of an empty list
        if k > 5:
            setsize += 4 ** _ceil(_log(k * 3, 4))  # table size for big sets
        if n <= setsize:
            # An n-length list is smaller than a k-length set.
            # Invariant:  non-selected at pool[0 : n-i]
            pool = list(population)
            for i in range(k):
                j = randbelow(n - i)
                result[i] = pool[j]
                pool[j] = pool[n - i - 1]  # move non-selected item into vacancy
        else:
            selected = set()
            selected_add = selected.add
            for i in range(k):
                j = randbelow(n)
                while j in selected:
                    j = randbelow(n)
                selected_add(j)
                result[i] = population[j]
        return result
コード例 #8
0
            raise TypeError('Cannot specify both weights and cumulative weights')
        if len(cum_weights) != len(population):
            raise ValueError('The number of weights does not match the population')
        bisect = _bisect.bisect
        total = cum_weights[-1]
        hi = len(cum_weights) - 1
        return [population[bisect(cum_weights, random() * total, 0, hi)]
                for i in range(k)]
=======
        n = len(population)
        if cum_weights is None:
            if weights is None:
                _int = int
                n += 0.0    # convert to float for a small speed improvement
                return [population[_int(random() * n)] for i in _repeat(None, k)]
            cum_weights = list(_accumulate(weights))
        elif weights is not None:
            raise TypeError('Cannot specify both weights and cumulative weights')
        if len(cum_weights) != n:
            raise ValueError('The number of weights does not match the population')
        bisect = _bisect
        total = cum_weights[-1] + 0.0   # convert to float
        hi = n - 1
        return [population[bisect(cum_weights, random() * total, 0, hi)]
                for i in _repeat(None, k)]
>>>>>>> 716b15a33aed978ded8a6bde17855cb6c6aa7f78

## -------------------- real-valued distributions  -------------------

## -------------------- uniform distribution -------------------
コード例 #9
0
def accumulate_with(fn, it):
    return _accumulate(it, func=fn)
コード例 #10
0
 def __init__(self, curve):
     lengths = (curve.segment_length(i) for i in range(len(curve.segments)))
     self.grid = list(_accumulate(lengths, initial=0))
     self.curve = curve
コード例 #11
0
ファイル: branch.py プロジェクト: avcopan/automech-old
def _dir_path_sequence(dir_names):
    return tuple(_accumulate(dir_names, os.path.join))