简体   繁体   English

使用Python查找值的近似值

[英]Approximation to find a value using Python

So I have one vector of alpha, one vector of beta, and I am trying to find a theta for when the sum of all the estimates (for alpha's i to n and beta's i to n) equals 60. 所以我有一个向量alpha,一个向量beta,并且我试图找到所有估计值的总和(对于α的i到n和beta的i到n)等于60的theta。

math.exp(alpha[i] * (theta - beta[i])) / (1 + math.exp(alpha[i] * (theta - beta[i])

在此处输入图片说明

Basically what I did is start from theta = 0.0001, and iterate through, calculating all these sums, and when it is lower than 60, continue by adding 0.0001 each time, while above 60 means. 基本上,我所做的是从theta = 0.0001开始,然后进行迭代,计算所有这些总和,当它小于60时,每次添加0.0001继续,而大于60则意味着。

I found the value theta this way. 我以这种方式找到了值theta。 Problem is, it took me about 60 seconds using Python, to find a theta of 0.456. 问题是,使用Python花了我大约60秒钟才能找到theta为0.456。

What is quicker approach to find this theta (since I would like to apply this for other data)? 找到该θ的更快方法是什么(因为我想将其应用于其他数据)?

def CalcTheta(score, alpha, beta):
    theta = 0.0001
    estimate = [score-1]

    while(sum(estimate) < score):

        theta += 0.00001

        for x in range(len(beta)):
            if x == 0:
                estimate = []

            estimate.append(math.exp(alpha[x] * (theta - beta[x]))  / (1 +  math.exp(alpha[x] * (theta - beta[x]))))

    return(theta)

You could use zip and sum to compute your target function: 您可以使用zipsum来计算目标函数:

  def f(theta):
    return sum(1/(1 + exp(a*(b-theta)))) for a,b in zip(alpha, beta))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM