Weighted random sample without replacement in python

后端 未结 3 1213
别那么骄傲
别那么骄傲 2020-12-07 01:55

I need to obtain a k-sized sample without replacement from a population, where each member of the population has a associated weight (W).

Numpy\'s rando

3条回答
  •  遥遥无期
    2020-12-07 02:09

    You can use np.random.choice with replace=False as follows:

    np.random.choice(vec,size,replace=False, p=P)
    

    where vec is your population and P is the weight vector.

    For example:

    import numpy as np
    vec=[1,2,3]
    P=[0.5,0.2,0.3]
    np.random.choice(vec,size=2,replace=False, p=P)
    

提交回复
热议问题