对可迭代的大型笛卡尔积进行随机采样
我有多个可迭代对象,我需要创建这些可迭代对象的笛卡尔积,然后从生成的元组池中随机采样。问题是这些可迭代的组合总数约为 1e19 左右,所以我不可能将所有这些加载到内存中。
我的想法是使用 itertools.product
与随机数生成器结合来跳过随机数量的项目,然后一旦我到达随机选择的项目,我就会执行计算并继续,直到用完发电机。计划是做类似的事情:
from itertools import product
from random import randint
iterables = () # tuple of 18 iterables
versions = product(iterables)
def do_stuff():
# do stuff
STEP_SIZE = int(1e6)
# start both counts from 0.
# First value to be taken is start + step
# after that increment start to be equal to count and repeat
start = 0
count = 0
while True:
try:
step = randint(1, 100) * STEP_SIZE
for v in versions:
# if the count is less than required skip values while incrementing count
if count < start + step:
versions.next()
count += 1
else:
do_stuff(*v)
start = count
except StopIteration:
break
不幸的是,itertools.product对象没有next()方法,所以这不起作用。还有什么其他方法可以遍历如此大量的元组并随机采样或直接对值进行计算?
I have multiple iterables and I need to create the Cartesian product of those iterables and then randomly sample from the resulting pool of tuples. The problem is that the total number of combinations of these iterables is somewhere around 1e19, so I can't possibly load all of this into memory.
What I thought was using itertools.product
in combination with a random number generator to skip random number of items, then once I get to the randomly selected item, I perform my calculations and continue until I run out of the generator. The plan was to do something like:
from itertools import product
from random import randint
iterables = () # tuple of 18 iterables
versions = product(iterables)
def do_stuff():
# do stuff
STEP_SIZE = int(1e6)
# start both counts from 0.
# First value to be taken is start + step
# after that increment start to be equal to count and repeat
start = 0
count = 0
while True:
try:
step = randint(1, 100) * STEP_SIZE
for v in versions:
# if the count is less than required skip values while incrementing count
if count < start + step:
versions.next()
count += 1
else:
do_stuff(*v)
start = count
except StopIteration:
break
Unfortunately, itertools.product
objects don't have the next()
method, so this doesn't work. What other way is there to go through this large number of tuples and either take a random sample or directly run calculations on the values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要尝试生成笛卡尔积。使用
random.choice()
一次从一个迭代中采样以生成结果。所有可迭代对象的元素数量都很小,因此您可以将所有元素直接存储在内存中。下面是一个使用 18 个迭代器的示例,每个迭代器有 10 个元素(如注释中指定):
Don't try to generate the Cartesian product. Sample from one iterable at a time to generate your result using
random.choice()
. The number of elements across all iterables is small, so you can store all the elements in memory directly.Here's an example using 18 iterables with 10 elements each (as specified in the comment):
您使用的是哪个版本的 Python?在此过程中,
.next()
方法被弃用,取而代之的是新的next()
内置函数。这对所有迭代器都适用。例如,在当前发布的 3.10.1 下:除此之外,您没有向我们展示代码中最重要的部分:如何构建产品。
如果没有看到这一点,我只能猜测,从传递给
product()
的第一个序列中进行随机选择,然后从第二个序列中进行随机选择,效率会远,等等。一次从一个组件构建产品的随机元素。有效地选择随机乘积元组
也许有些过分了,但是这个类展示了一种特别有效的方法来做到这一点。
.index()
方法将整数i
映射到乘积中的第 i 个元组(从 0 开始)。然后从产品中选择一个随机元组只需将.index()
应用于范围(产品中元素的总数)
中的随机整数。进而
Which version of Python are you using? Somewhere along the way
.next()
methods were deprecated in favor a newnext()
built-in function. That works fine with all iterators. Here, for example, under the current released 3.10.1:Beyond that, you didn't show us the most important part of your code: how you build your product.
Without seeing that, I can only guess that it would be far more efficient to make a random choice from the first sequence passed to
product()
, then another from the second, and so on. Build a random element of the product from one component at a time.Picking a random product tuple efficiently
Perhaps overkill, but this class shows an especially efficient way to do this. The
.index()
method maps an integeri
to the i'th tuple (0-based) in the product. Then picking a random tuple from the product is simply applying.index()
to a random integer inrange(total number of elements in the product)
.and then