油藏取样 [英] Reservoir sampling
本文介绍了油藏取样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了从不确定大小的数组中检索 k 个随机数,我们使用了一种称为水库采样的技术.任何人都可以用示例代码简要强调一下它是如何发生的吗?
To retrieve k random numbers from an array of undetermined size we use a technique called reservoir sampling. Can anybody briefly highlight how it happens with a sample code?
推荐答案
我其实没有意识到有一个名字,所以我从头开始证明并实现了:
I actually did not realize there was a name for this, so I proved and implemented this from scratch:
import random
def random_subset( iterator, K ):
result = []
N = 0
for item in iterator:
N += 1
if len( result ) < K:
result.append( item )
else:
s = int(random.random() * N)
if s < K:
result[ s ] = item
return result
接近尾声的证明.
这篇关于油藏取样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文