go - 将算法从 Python 移植到 Go

标签 go

我正在尝试将此 python 代码移植到 Go,但数学包中没有 beta()。我在哪里可以找到测试版和为此所需的其他功能?

from numpy import *
from scipy.stats import beta


class BetaBandit(object):
    def __init__(self, num_options=2, prior=(1.0,1.0)):
        self.trials = zeros(shape=(num_options,), dtype=int)
        self.successes = zeros(shape=(num_options,), dtype=int)
        self.num_options = num_options
        self.prior = prior

    def add_result(self, trial_id, success):
        self.trials[trial_id] = self.trials[trial_id] + 1
        if (success):
            self.successes[trial_id] = self.successes[trial_id] + 1

    def get_recommendation(self):
        sampled_theta = []
        for i in range(self.num_options):
            #Construct beta distribution for posterior
            dist = beta(self.prior[0]+self.successes[i],
                        self.prior[1]+self.trials[i]-self.successes[i])
            #Draw sample from beta distribution
            sampled_theta += [ dist.rvs() ]
        # Return the index of the sample with the largest value
        return sampled_theta.index( max(sampled_theta) )

最佳答案

如果你说的是numpy.random.beta , Beta分布是Dirichlet分布的特例,与Gamma分布有关,可以查看项目gostat .

它有一个 beta.go source code它实现了该功能。

关于go - 将算法从 Python 移植到 Go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23979900/

相关文章:

go - 为什么这个 Go 程序没有像预期的那样进入死锁状态?

go - 如何将类型传递给 http 处理程序

go - 编译二进制比运行慢 "go run"

pointers - 理解 Go 指针

go - 如何显式清空 channel ?

go - 如何防止我的整个表被删除

go - 用libvlc播放实现previous

go - 如何在 Go 中使用密码创建 RSA 私钥?

go - 更新结构中的值不起作用

go - 区分发生恢复的 panic 和没有发生错误