我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用itertools.accumulate()。
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 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 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 __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 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 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 phase_starts(self): return list(itertools.accumulate([0] + [phase.length for phase in self.phase_list]))
def foo3(s): join_2_strings = lambda x,y: '.'.join((x, y)) return itertools.accumulate(s.split('.'), join_2_strings)
def running(lst, fn): return list(accumulate(lst, fn))
def _ts(self): lengths = [c.length for c in self._curves] length = sum(lengths) out = [] for i, j in enumerate(accumulate(lengths[:-1])): self._curves[i].req_length = lengths[i] out.append(j / length) self._curves[-1].req_length = max( 0, lengths[-1] - (length - self.req_length), ) out.append(1) return out
def choose_weighted(self, *weighted_choices): choices, weights = zip(*weighted_choices) cumdist = list(itertools.accumulate(weights)) x = self.random() * cumdist[-1] return choices[bisect.bisect(cumdist, x)]
def _split(self, obj): # "hello there world" -> ["hello", "hello there", "hello there world"] from itertools import accumulate return list(accumulate(obj.split(), lambda x, y: f'{x} {y}'))
def cumsum(self): return Vec(itertools.accumulate(self))
def is_valid_program(p): """ 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 generate_sentence(cfdist, word, num=15): sentence = [] # Generate words until we meet a period while word!='.': sentence.append(word) # Generate the next word based on probability choices, weights = zip(*cfdist[word].items()) cumdist = list(itertools.accumulate(weights)) x = random.random() * cumdist[-1] word = choices[bisect.bisect(cumdist, x)] return ' '.join(sentence)
def get_interval_offsets_txt(lines: List[str]) -> Iterator[Tuple[int, int]]: """Return all the intervals corresponding to the ``lines`` passed as parameter: [(0, n), (n, m), …] where the values are the character position of the beginning and end of each line, counting from the first character of the file (start at 0)""" idx_first_char = 0 cumulative_lines_length = list(itertools.accumulate(list(map(len, lines)))) return zip([idx_first_char] + cumulative_lines_length, cumulative_lines_length)
def from_pydata(self, vertices, edges, faces): """ Make a mesh from a list of vertices/edges/faces Until we have a nicer way to make geometry, use this. :arg vertices: float triplets each representing (X, Y, Z) eg: [(0.0, 1.0, 0.5), ...]. :type vertices: iterable object :arg edges: int pairs, each pair contains two indices to the *vertices* argument. eg: [(1, 2), ...] :type edges: iterable object :arg faces: iterator of faces, each faces contains three or more indices to the *vertices* argument. eg: [(5, 6, 8, 9), (1, 2, 3), ...] :type faces: iterable object .. warning:: Invalid mesh data *(out of range indices, edges with matching indices, 2 sided faces... etc)* are **not** prevented. If the data used for mesh creation isn't known to be valid, run :class:`Mesh.validate` after this function. """ from itertools import chain, islice, accumulate face_lengths = tuple(map(len, faces)) self.vertices.add(len(vertices)) self.edges.add(len(edges)) self.loops.add(sum(face_lengths)) self.polygons.add(len(faces)) self.vertices.foreach_set("co", tuple(chain.from_iterable(vertices))) self.edges.foreach_set("vertices", tuple(chain.from_iterable(edges))) vertex_indices = tuple(chain.from_iterable(faces)) loop_starts = tuple(islice(chain([0], accumulate(face_lengths)), len(faces))) self.polygons.foreach_set("loop_total", face_lengths) self.polygons.foreach_set("loop_start", loop_starts) self.polygons.foreach_set("vertices", vertex_indices) # if no edges - calculate them if faces and (not edges): self.update(calc_edges=True)
def plackett_luce(rankings, tolerance=1e-9, check_assumption=True, normalize=True, verbose=False): '''This algorithm returns the MLE of the Plackett-Luce ranking parameters over a given set of rankings. It requires that the set of players is unable to be split into two disjoint sets where nobody from set A has beaten anyone from set B. If this assumption fails, the algorithm will diverge. If the assumption is checked and fails, the algorithm will short-circuit and return None. Input is a list of dictionaries, where each dictionary corresponds to an individual ranking and contains the player : finish for that ranking. Output is a dictionary containing player : plackett_luce_parameter keys and values. ''' players = set(key for ranking in rankings for key in ranking.keys()) rankings = [sorted(ranking.keys(),key=ranking.get) for ranking in rankings] if verbose: print('Using native Python implementation of Plackett-Luce.') print('{:,} unique players found.'.format(len(players))) print('{:,} rankings found.'.format(len(rankings))) if check_assumption: edges = [(source, dest) for ranking in rankings for source, dest in combinations(ranking, 2)] scc_count = len(set(scc(edges).values())) if verbose: if scc_count == 1: print ('No disjoint sets found. Algorithm convergence conditions are met.') else: print('{:,} disjoint sets found. Algorithm will diverge.'.format(scc_count)) if scc_count != 1: return None ws = Counter(name for ranking in rankings for name in ranking[:-1]) gammas = {player : 1.0 / len(players) for player in players} gdiff = float('inf') iteration = 0 start = time.perf_counter() while gdiff > tolerance: _gammas = gammas gamma_sums = [list(accumulate(1 / s for s in reversed(list(accumulate(gammas[finisher] for finisher in reversed(ranking)))))) for ranking in rankings] gammas = {player : ws[player] / sum(gamma_sum[min(ranking.index(player), len(ranking) - 2)] for ranking, gamma_sum in zip(rankings, gamma_sums) if player in ranking) for player in players} if normalize: gammas = {player : gamma / sum(gammas.values()) for player, gamma in gammas.items()} pgdiff = gdiff gdiff = sqrt(sum((gamma - _gammas[player]) ** 2 for player, gamma in gammas.items())) iteration += 1 if verbose: now = time.perf_counter() print("%d %.2f seconds L2=%.2e" % (iteration, now-start, gdiff)) if gdiff > pgdiff: print("Gamma difference increased, %.4e %.4e" % (gdiff, pgdiff)) start = now return gammas