[英]Weighted sampling without replacement using gonum
I have a big array of items and another array of weights of the same size. 我有一大堆物品和另一个相同大小的重量数组。 I would like to sample without replacement from the first array based on the weights from the second array.
我想根据第二个数组的权重从第一个数组中取代而不进行替换。 Is there a way to do this using
gonum
? 有没有办法用
gonum
做到这gonum
?
Weighted
and its relative method .Take()
look exactly like what you want. Weighted
及其相对方法.Take()
看起来与你想要的完全一样。
From the doc: 来自doc:
func NewWeighted(w []float64, src *rand.Rand) Weighted
NewWeighted
returns aWeighted
for the weightsw
.NewWeighted
为权重w
返回Weighted
。 Ifsrc
isnil
,rand.Rand
is used as the random source.如果
src
为nil
,则rand.Rand
用作随机源。 Note that sampling from weights with a high variance or overall low absolute value sum may result in problems with numerical stability.注意,从具有高方差或总体低绝对值和的权重中采样可能导致数值稳定性的问题。
func (s Weighted) Take() (idx int, ok bool)
Take
returns an index from the Weighted with probability proportional to the weight of the item.Take
返回从加权的概率成比例的项的权值的索引。 The weight of the item is then set to zero .然后将项目的权重设置为零 。
Take
returnsfalse
if there are no items remaining.Take
返回false
,如果没有剩余的项目。
Therefore Take
is indeed what you need for sampling without replacement. 因此,
Take
确实是您无需更换即可获得的样品。
You can use NewWeighted
to create a Weighted
with the given weights, then use Take
to extract one index with probability based on the previously set weights, and then select the item at the extracted index from your array of samples. 您可以使用
NewWeighted
创建Weighted
给定的权重,然后使用Take
提取基于预先设定的权重概率一个指数,然后从你的样品阵列所取出的索引在选择项目。
Working example: 工作范例:
package main
import (
"fmt"
"time"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/sampleuv"
)
func main() {
samples := []string{"hello", "world", "what's", "going", "on?"}
weights := []float64{1.0, 0.55, 1.23, 1, 0.002}
w := sampleuv.NewWeighted(
weights,
rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
)
i, _ := w.Take()
fmt.Println(samples[i])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.