gluonts.itertools module#
- class gluonts.itertools.Cached(iterable: gluonts.itertools.SizedIterable)[source]#
基类:
object
一个可迭代对象包装器,在迭代时将值缓存到列表中。
其主要用途是避免重新计算序列的元素,以防内部可迭代对象按需生成它们。
这应该用于包装确定性可迭代对象,即数据生成过程不是随机的,并且多次迭代时生成相同元素的那些可迭代对象。
- consumed: list#
- iterable: gluonts.itertools.SizedIterable#
- provider: Iterable#
- class gluonts.itertools.Chain(iterables: Collection[gluonts.itertools.SizedIterable])[source]#
基类:
object
将多个可迭代对象链接成一个。
这是
itertools.chain
的一个轻量级包装器。- iterables: Collection[gluonts.itertools.SizedIterable]#
- class gluonts.itertools.Cyclic(iterable: gluonts.itertools.SizedIterable)[source]#
基类:
object
类似于 itertools.cycle,但不存储数据。
- iterable: gluonts.itertools.SizedIterable#
- stream()[source]#
返回一个没有固定起始点的连续流。
重新迭代
Cyclic
时,它将从传入的iterable
的开头产生元素。然而,这并非总是期望的行为;例如,在训练中,我们希望将训练数据视为无限的值流,而不是在每个 epoch 都从数据集的开头开始。>>> from toolz import take >>> c = Cyclic([1, 2, 3, 4]) >>> assert list(take(5, c)) == [1, 2, 3, 4, 1] >>> assert list(take(5, c)) == [1, 2, 3, 4, 1]
>>> s = Cyclic([1, 2, 3, 4]).stream() >>> assert list(take(5, s)) == [1, 2, 3, 4, 1] >>> assert list(take(5, s)) == [2, 3, 4, 1, 2]
- class gluonts.itertools.Filter(fn: 'Callable', iterable: 'SizedIterable')[source]#
基类:
object
- fn: Callable#
- iterable: gluonts.itertools.SizedIterable#
- class gluonts.itertools.Fuse(collections: typing.List[typing.Sequence], _lengths: typing.List[int] = <factory>)[source]#
基类:
object
将多个集合融合在一起,使其行为类似于单个集合。
>>> a = [0, 1, 2] >>> b = [3, 4, 5] >>> fused = Fuse([a, b]) >>> assert len(a) + len(b) == len(fused) >>> list(fused[2:5]) [2, 3, 4] >>> list(fused[3:]) [3, 4, 5]
这类似于
Chain
,但也允许直接选择数据。虽然Chain
将一组Iterable
对象创建为一个单一的Iterable
,但Fuse
将一组 ``Collection``s 创建为一个单一的Collection
。- collections: List[Sequence]#
- class gluonts.itertools.IterableSlice(iterable, length)[source]#
基类:
object
itertools.islice 的一个可迭代版本,即可以多次迭代。
>>> isl = IterableSlice(iter([1, 2, 3, 4, 5]), 3) >>> list(isl) [1, 2, 3] >>> list(isl) [4, 5] >>> list(isl) []
这需要是一个类来支持重入迭代。
- class gluonts.itertools.Map(fn: 'Callable', iterable: 'SizedIterable')[source]#
基类:
object
- fn: Callable#
- iterable: gluonts.itertools.SizedIterable#
- class gluonts.itertools.PickleCached(iterable: gluonts.itertools.SizedIterable, cached: bool = False, _path: pathlib.Path = <factory>)[source]#
基类:
object
使用
pickle
将缓存值存储到磁盘上的iterable
缓存包装器。有关更多信息,请参阅
Cached
。- cached: bool = False#
- iterable: gluonts.itertools.SizedIterable#
- class gluonts.itertools.PseudoShuffled(iterable: gluonts.itertools.SizedIterable, shuffle_buffer_length: int)[source]#
基类:
object
以伪随机顺序从给定的可迭代对象中产生项目。
- iterable: gluonts.itertools.SizedIterable#
- shuffle_buffer_length: int#
- class gluonts.itertools.RandomYield(iterables: typing.List[typing.Iterable], probabilities: typing.Optional[typing.List[float]] = None, random_state: numpy.random.mtrand.RandomState = <factory>)[source]#
基类:
object
给定一个可迭代对象列表 iterables,从中生成样本。
如果给定了 probabilities,则根据其概率采样可迭代对象。如果未给出 probabilities,则均匀采样可迭代对象。
当一个可迭代对象耗尽时,其采样概率将设置为 0。
>>> from toolz import take >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> it = iter(RandomYield([a, b], probabilities=[1, 0])) >>> list(take(5, it)) [1, 2, 3]
>>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> it = iter(RandomYield([Cyclic(a), b], probabilities=[1, 0])) >>> list(take(5, it)) [1, 2, 3, 1, 2]
- iterables: List[Iterable]#
- probabilities: Optional[List[float]] = None#
- random_state: numpy.random.mtrand.RandomState#
- class gluonts.itertools.SizedIterableSlice(iterable, length)[source]#
基类:
gluonts.itertools.IterableSlice
与
IterableSlice
相同,但也支持 len()>>> isl = SizedIterableSlice([1, 2, 3, 4, 5], 3) >>> len(isl) 3
- class gluonts.itertools.StarMap(fn: 'Callable', iterable: 'SizedIterable')[source]#
基类:
object
- fn: Callable#
- iterable: gluonts.itertools.SizedIterable#
- gluonts.itertools.batcher(iterable: Iterable[gluonts.itertools.T], batch_size: int) Iterator[List[gluonts.itertools.T]] [source]#
将 iterable 中的元素按 batch_size 分组。
>>> list(batcher("ABCDEFG", 3)) [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
与 itertools 文档中提出的分组器不同,batcher 不会填充缺失值。
- gluonts.itertools.chop(at: int, take: int) slice [source]#
使用索引
at
和数量take
创建切片。>>> x = [0, 1, 2, 3, 4] >>> x[chop(at=1, take=2)] [1, 2] >>> >>> x[chop(at=-2, take=2)] [3, 4] >>> x[chop(at=3, take=-2)] [1, 2]
- gluonts.itertools.columns_to_rows(columns: Dict[gluonts.itertools.K, Sequence[gluonts.itertools.V]]) List[Dict[gluonts.itertools.K, gluonts.itertools.V]] [source]#
将列方向转置为行方向。
>>> columns_to_rows({'a': [1, 3], 'b': [2, 4]}) [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
- gluonts.itertools.inverse(dct: Dict[gluonts.itertools.K, gluonts.itertools.V]) Dict[gluonts.itertools.V, gluonts.itertools.K] [source]#
反转字典;键成为值,值成为键。
- gluonts.itertools.join_items(left, right, how='outer', default=None)[source]#
迭代连接的字典项目。
产生 key、left_value、right_value 的三元组。
类似于 SQL 的 JOIN 语句,连接行为由
how
控制outer
(默认): 使用左侧和右侧的键inner
: 只使用同时出现在左侧和右侧的键strict
: 类似于inner
,但如果键不匹配则抛出错误left
: 只使用left
中的键right
: 只使用right
中的键
如果任一输入中不存在某个键,则选择
default
。
- gluonts.itertools.partition(it: Iterable[gluonts.itertools.T], fn: Callable[[gluonts.itertools.T], bool]) Tuple[List[gluonts.itertools.T], List[gluonts.itertools.T]] [source]#
根据谓词 fn 将 it 分割为两个列表。
这类似于 Python itertools 文档中定义的示例,但此方法直接消耗迭代器并返回列表而不是迭代器。
- gluonts.itertools.pluck_attr(seq='__no__default__', name='__no__default__', default=<object object>)[source]#
从
seq
中的元素获取属性name
。
- gluonts.itertools.power_set(iterable)[source]#
生成给定可迭代对象的所有可能的子集,以元组形式。
>>> list(power_set(["a", "b"])) [(), ('a',), ('b',), ('a', 'b')]
改编自 https://docs.pythonlang.cn/3/library/itertools.html#itertools-recipes
- gluonts.itertools.replace(values: Sequence[gluonts.itertools.T], idx: int, value: gluonts.itertools.T) Sequence[gluonts.itertools.T] [source]#
将索引
idx
处的值替换为value
。类似于
setitem
,但用于元组。>>> replace((1, 2, 3, 4), -1, 99) (1, 2, 3, 99)
- gluonts.itertools.roundrobin(*iterables)[source]#
roundrobin(‘ABC’, ‘D’, ‘EF’) –> A D E B F C
摘自: https://docs.pythonlang.cn/3/library/itertools.html#recipes。
- gluonts.itertools.rows_to_columns(rows: typing.Sequence[typing.Dict[gluonts.itertools.K, gluonts.itertools.V]], wrap: typing.Callable[[typing.Sequence[gluonts.itertools.V]], typing.Sequence[gluonts.itertools.V]] = <function <lambda>>) Dict[gluonts.itertools.K, Sequence[gluonts.itertools.V]] [source]#
转置字典的行,得到一个包含列的字典。
>>> rows_to_columns([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]) {'a': [1, 3], 'b': [2, 4]}
这也可以理解为将每个字典的值堆叠在一起。
- gluonts.itertools.select(keys, source: dict, ignore_missing: bool = False) dict [source]#
选择 source 字典的子集。
>>> d = {"a": 1, "b": 2, "c": 3} >>> select(["a", "b"], d) {'a': 1, 'b': 2}
- gluonts.itertools.split(xs: Sequence, indices: List[int]) List[Sequence] [source]#
根据
indices
将xs
分割成子集。>>> split("abcdef", [1, 3]) ['a', 'bc', 'def']
这类似于传递索引列表时的
numpy.split
,但此版本不会将底层数据转换为数组。