How do you sample without replacement from counted label occurrences?

The counts parameter of sample represents repeated occurrences without materializing the expanded population.

Python example and interpretation

With three A occurrences and one B, drawing three can yield repeated A labels. Each occurrence is selected at most once even when labels repeat.

from random import Random
sorted(Random(12).sample(['A','B'],counts=[3,1],k=3))

Result ['A', 'A', 'B']

中文

从重复标签总体抽样时,如何不放回抽取又保留标签频数?

sample 的 counts 参数可表示每个标签有多少个不同出现位置,无需显式复制整份总体。

Python 算例与适用条件

A 有三份、B 一份,抽三项可能重复出现 A。这里不放回指同一出现位置只能抽一次,而非每种标签只能出现一次。

from random import Random
sorted(Random(12).sample(['A','B'],counts=[3,1],k=3))

结果 ['A', 'A', 'B']

References / 参考来源

Keep reading / 继续阅读