将算法从Python移植到Go语言

huangapple go评论88阅读模式
英文:

Porting algorithm from Python to Go

问题

我正在尝试将这段Python代码转换为Go代码,但是在math包中找不到**beta()**函数。我应该在哪里找到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) )

这段代码使用了numpyscipy.stats库中的beta函数。如果你想在Go中实现相同的功能,你可以尝试使用Go的数学库或者寻找第三方库来提供beta分布的函数。

英文:

I am trying to port this python code to Go but there is no beta() in math package. Where can i find beta and other functions required for this?

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) )

答案1

得分: 7

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

它有一个 beta.go 源代码 实现了该函数。

英文:

If you are talking about numpy.random.beta, the Beta distribution which is a special case of the Dirichlet distribution, and is related to the Gamma distribution, you can check the project gostat.

It has a beta.go source code which implements that function.

huangapple
  • 本文由 发表于 2014年6月1日 20:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/23979900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定