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
if cum_weights is None:
if weights is None:
_int = int
total = len(population)
return [population[_int(random() * total)] for i in range(k)]
cum_weights = list(_itertools.accumulate(weights))
elif weights is not None:
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]
return [population[bisect(cum_weights, random() * total)] for i in range(k)]
## -------------------- real-valued distributions -------------------
## -------------------- uniform distribution -------------------
python类accumulate()的实例源码
def train_stats(m, trainloader, param_list = None):
stats = {}
params = filtered_params(m, param_list)
counts = 0,0
for counts in enumerate(accumulate((reduce(lambda d1,d2: d1*d2, p[1].size()) for p in params)) ):
pass
stats['variables_optimized'] = counts[0] + 1
stats['params_optimized'] = counts[1]
before = time.time()
losses = train(m, trainloader, param_list=param_list)
stats['training_time'] = time.time() - before
stats['training_loss'] = losses[-1] if len(losses) else float('nan')
stats['training_losses'] = losses
return stats
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
if cum_weights is None:
if weights is None:
_int = int
total = len(population)
return [population[_int(random() * total)] for i in range(k)]
cum_weights = list(_itertools.accumulate(weights))
elif weights is not None:
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]
return [population[bisect(cum_weights, random() * total)] for i in range(k)]
## -------------------- real-valued distributions -------------------
## -------------------- uniform distribution -------------------
def scanr(col, func=add, acc=None):
'''
Use a given accumulator value to build a list of values obtained
by repeatedly applying acc = func(next(list), acc) from the right.
WARNING: Right folds and scans will blow up for infinite generators!
'''
try:
col = reversed(col)
except TypeError:
col = reversed(list(col))
if acc is not None:
col = chain([acc], col)
return list(itools.accumulate(col, func))
def iscanr(col, func=add, acc=None):
'''
Use a given accumulator value to build a stream of values obtained
by repeatedly applying acc = func(next(list), acc) from the right.
WARNING: Right folds and scans will blow up for infinite generators!
'''
try:
col = reversed(col)
except TypeError:
col = reversed(list(col))
if acc is not None:
col = chain([acc], col)
for element in itools.accumulate(col, func):
yield element
def register(item, category, weight):
entry = Registry._registered_categories.get(category)
category_list = entry[0] if entry else None
weight_table = entry[1] if entry else None
if not category_list:
category_list = []
if not weight_table:
weight_table = []
if (item, weight) not in category_list:
category_list.append((weight, item))
weight_table = list(accumulate([i[0] for i in category_list]))
Registry._registered_categories[category] = category_list, weight_table
def select(self, population, fitness):
'''
Select a pair of parent using FPS algorithm.
'''
# Normalize fitness values for all individuals.
fit = population.all_fits(fitness)
min_fit = min(fit)
fit = [(i - min_fit) for i in fit]
# Create roulette wheel.
sum_fit = sum(fit)
wheel = list(accumulate([i/sum_fit for i in fit]))
# Select a father and a mother.
father_idx = bisect_right(wheel, random())
father = population[father_idx]
mother_idx = (father_idx + 1) % len(wheel)
mother = population[mother_idx]
return father, mother
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
if cum_weights is None:
if weights is None:
_int = int
total = len(population)
return [population[_int(random() * total)] for i in range(k)]
cum_weights = list(_itertools.accumulate(weights))
elif weights is not None:
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]
return [population[bisect(cum_weights, random() * total)] for i in range(k)]
## -------------------- real-valued distributions -------------------
## -------------------- uniform distribution -------------------
def __init__(self, srmfile):
self.header = SRMHeader(srmfile)
self.summary_marker = SRMSummaryMarker(srmfile)
self.markers = [SRMMarker(srmfile)
for _ in range(self.header.marker_count)]
blocks = [SRMBlock(srmfile)
for _ in range(self.header.block_count)]
block_ends = accumulate(block.chunk_count for block in blocks)
for block, end in zip(blocks, block_ends):
setattr(block, 'end', end)
self.blocks = blocks
self.calibration = SRMCalibrationData(srmfile)
self.data_count = sum(block.chunk_count for block in blocks)
def __init__(self, distribution, label):
"""
Creates a new state that contains a distribution of transitions
to other states with a specified label.
:param distribution: an iterable of state - transition probability pairs
:param label: the label of the state
"""
self.prob = {
d[0]: Fraction(d[1]) for d in distribution \
if 0 < Fraction(d[1]) <= 1
}
self.cum_prob = list(accumulate(v for v in self.prob.values()))
if self.cum_prob[-1] != 1:
raise ValueError("Transitions from state " + str(label) +
" do not form a probability distribution")
self.label = label
def sim_game(homeo, homed, awayo, awayd, HFAmult):
import random
from itertools import accumulate
import bisect
from math import fsum
homeodds,awayodds = single_match_odds(homeo, homed, awayo, awayd, HFAmult)
hometotalodds = list(accumulate(homeodds))
awaytotalodds = list(accumulate(awayodds))
homerand = random.uniform(0,hometotalodds[-1])
awayrand = random.uniform(0,awaytotalodds[-1])
home_goals = bisect.bisect(hometotalodds, homerand)
away_goals = bisect.bisect(awaytotalodds, awayrand)
return home_goals, away_goals
## @brief Class representing a single soccer match-up.
# @details Includes team names and home field multiplier (default 1, set to 0
# if neutral-field). Also holds score, and simulates the game to get it if need
# be.
# @since FutbolRatings 0.4.0, 2016.06.13
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.
This function is borrowed from the python 3.6 'random' package.
"""
if cum_weights is None:
if weights is None:
_int = int
total = len(population)
return [population[_int(random() * total)] 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) != len(population):
raise ValueError('The number of weights does not match the population')
total = cum_weights[-1]
return [population[bisect(cum_weights, random() * total)] for i in range(k)]
def is_valid_program(self,p):
"""checks whether program p makes a syntactically valid tree.
checks that the accumulated program length is always greater than the
accumulated arities, indicating that the appropriate number of arguments is
alway present for functions. It then checks that the sum of arties +1
exactly equals the length of the stack, indicating that there are no
missing arguments.
"""
# print("p:",p)
arities = list(a.arity[a.in_type] for a in p)
accu_arities = list(accumulate(arities))
accu_len = list(np.arange(len(p))+1)
check = list(a < b for a,b in zip(accu_arities,accu_len))
# print("accu_arities:",accu_arities)
# print("accu_len:",accu_len)
# print("accu_arities < accu_len:",accu_arities<accu_len)
return all(check) and sum(a.arity[a.in_type] for a in p) +1 == len(p) and len(p)>0
def scanl(col, func=add, acc=None):
'''
Fold a collection from the left using a binary function
and an accumulator into a list of values
'''
if acc is not None:
col = chain([acc], col)
return list(itools.accumulate(col, func))
def iscanl(col, func=add, acc=None):
'''
Fold a collection from the left using a binary function
and an accumulator into a stream of values
'''
if acc is not None:
col = chain([acc], col)
for element in itools.accumulate(col, func):
yield element
def prefSum(a):
return list(accumulate(a))
def accumulate(iterable):
" Super simpel 'accumulate' implementation. "
total = 0
for item in iterable:
total += item
yield total
def accumulate(iterable):
" Super simpel 'accumulate' implementation. "
total = 0
for item in iterable:
total += item
yield total
def select(self, population, fitness):
'''
Select a pair of parent individuals using linear ranking method.
'''
# Individual number.
NP = len(population)
# Add rank to all individuals in population.
all_fits = population.all_fits(fitness)
indvs = population.individuals
sorted_indvs = sorted(indvs,
key=lambda indv: all_fits[indvs.index(indv)])
# Assign selection probabilities linearly.
# NOTE: Here the rank i belongs to {1, ..., N}
p = lambda i: (self.pmin + (self.pmax - self.pmin)*(i-1)/(NP-1))
probabilities = [self.pmin] + [p(i) for i in range(2, NP)] + [self.pmax]
# Normalize probabilities.
psum = sum(probabilities)
wheel = list(accumulate([p/psum for p in probabilities]))
# Select parents.
father_idx = bisect_right(wheel, random())
father = sorted_indvs[father_idx]
mother_idx = (father_idx + 1) % len(wheel)
mother = sorted_indvs[mother_idx]
return father, mother
def select(self, population, fitness):
'''
Select a pair of parent individuals using exponential ranking method.
'''
# Individual number.
NP = len(population)
# Add rank to all individuals in population.
all_fits = population.all_fits(fitness)
indvs = population.individuals
sorted_indvs = sorted(indvs,
key=lambda indv: all_fits[indvs.index(indv)])
# NOTE: Here the rank i belongs to {1, ..., N}
p = lambda i: self.base**(NP - i)
probabilities = [p(i) for i in range(1, NP + 1)]
# Normalize probabilities.
psum = sum(probabilities)
wheel = list(accumulate([p/psum for p in probabilities]))
# Select parents.
father_idx = bisect_right(wheel, random())
father = sorted_indvs[father_idx]
mother_idx = (father_idx + 1) % len(wheel)
mother = sorted_indvs[mother_idx]
return father, mother
def _get_gene_indices(self):
'''
Helper function to get gene slice indices.
'''
end_indices = list(accumulate(self.lengths))
start_indices = [0] + end_indices[: -1]
return list(zip(start_indices, end_indices))
def __init__(self, weights):
self.totals = list(itertools.accumulate(weights))
self.total = self.totals[-1]
def counter_random(counter, filter=None):
"""Return a single random elements from the Counter collection, weighted by count."""
if filter is not None:
counter = { k : v for k,v in counter.items() if filter(k) }
if len(counter) == 0:
raise Exception("No matching elements in Counter collection")
seq = list(counter.keys())
cum = list(itertools.accumulate(list(counter.values()), op.add))
return seq[bisect.bisect_left(cum, random.uniform(0, cum[-1]))]
def weighted_choices(seq, weights, n):
"""Return random elements from a sequence, according to the given relative weights."""
cum = list(itertools.accumulate(weights, op.add))
return [seq[bisect.bisect_left(cum, random.uniform(0, cum[-1]))] for i in range(n)]
def _choose_comp(self, comps):
weights = list((comp, self.pheromones[comp]**self.alpha * heuristic**self.beta) for comp, heuristic in comps)
# Pseudo-random proportional update (ACS)
if random.random() < self.q0:
comp, weight = max(weights, key=itemgetter(1))
return comp
else:
weights = list(itertools.accumulate(weights, lambda acc, t: (t[0], acc[1] + t[1])))
_, total_weight = weights[-1]
threshold = random.random() * total_weight
return next(comp for comp, w in weights if w >= threshold)
def accumulate(iterable):
" Super simpel 'accumulate' implementation. "
total = 0
for item in iterable:
total += item
yield total
def create_quantiles(items: Sequence, lower_bound, upper_bound):
"""Create quantile start and end boundaries."""
interval = (upper_bound - lower_bound) / len(items)
quantiles = ((g, (x - interval, x)) for g, x in
zip(items, accumulate(repeat(interval, len(items)))))
return quantiles
def __call__(self, x):
segs = list(itertools.accumulate(
clf.n_input for clf in self.classifiers
))
if segs:
xs = cf.split_axis(x, segs, 1)
else:
xs = [x]
y = self.segmenter(xs[-1])
zs = tuple(clf(x) for x, clf in zip(xs[:-1], self.classifiers))
return y, zs
def data_deal_function():
# compress()????????????.????????????????,??????????????.
# ????????????????True?????
# ??,????????????.???????Python??????????,??????
# itertools.filterfalse()???????????,??????.???????????False???True???
for item in it.compress([1, 2, 3, 4, 5], [False, True, False, 0, 1]):
print(item)
# dropwhile()?takewhile()?????????????.??????????????????????????,???????????????.
# dropwhile()??????????????????????False.?takewhile()??????????False
# ??,????????????????????????(??dropwhile????,????????????,?takewhile?????????)
def __single_digit(n):
return n < 10
for n in it.dropwhile(__single_digit, range(20)):
print(n, end=" ")
for n in it.takewhile(__single_digit, range(20)):
print(n, end=" ")
# accumulate()?????????????????????????????(??????,????????????).??,???????
# [1,2,3,4]??,???result1?1.?????????result1?2??result2,????.????????functools???reduce()????
for n in it.accumulate([1, 2, 3, 4, ]):
print(n, end=" ")
def accumulate(iterable):
" Super simpel 'accumulate' implementation. "
total = 0
for item in iterable:
total += item
yield total