使用 python 反向求解变量
back solve for a variable using python
我正在尝试编写一个函数来反向求解 python 中另一个函数的变量,有点像 Excel 求解器所做的。
为了简化我的示例,我有一个函数接受多个变量然后计算价格。我会将实际值 (a、b、c、d、x) 传递给此函数,因此它 returns 是一个数值。
def calc_price(a,b,c,d,x):
value = a+b*c-d + x
return value
现在我得到了一个目标价格,以及 a、b、c、d。只有未知的是变量x,所以我想反解变量x。我想将其构建到一个函数中,该函数接收与 calc_price 相同的变量,以及一个附加变量 target_price.
def solve(target_price, a,b,c,d):
#this function takes in values for target_price, a,b,c,d
#and should do something like this:
target_price = calc_price(a,b,c,d,x)
solve for x <---------this is the part I'm not sure how to do
return x
我创建了一个像下面这样的函数来通过循环反向求解值 x 但它在计算大型数据集时效率低下,所以我正在寻找更有效的解决方案。
def solve(target_price,a,b,c,d):
x = 0.01
while x < 1:
if abs(target_price - calc_price(a,b,c,d,x)) < 0.001:
return x
x += 0.001
谢谢!
将此视为一个演示(因为你的任务对我来说仍然有点不清楚)并确保阅读 scipy's docs 以了解这些方法提供的基本保证。
有人可能会争辩说,基于 root-finding 的方法更合适(我们在这里最小化一个函数;因此残差函数中的 abs 构造),但这里的这种方法不需要你给出一些包围间隔。
代码:
import numpy as np
from scipy.optimize import minimize_scalar
np.random.seed(0)
""" Utils """
def calc_price(x, a, b, c, d):
value = a+b*c-d + x
return value
def calc_price_res(x, target, a, b, c, d):
value = a+b*c-d + x
return abs(value - target) # we are looking for f(x) == 0
""" Create fake-data (typically the job of OP!) """
a, b, c, d, x = np.random.random(size=5)
fake_target = calc_price(x, a, b, c, d)
print('a, b, c, d: ', a, b, c, d)
print('real x: ', x)
print('target: ', fake_target)
print('noisy obj (just to be sure): ', calc_price_res(x, fake_target, a, b, c, d))
""" Solve """
res = minimize_scalar(calc_price_res, args=(fake_target, a, b, c, d))
print('optimized x: ', res.x)
print('optimized fun: ', res.fun)
输出:
a, b, c, d: 0.548813503927 0.715189366372 0.602763376072 0.544883182997
real x: 0.423654799339
target: 0.858675077275
noisy obj (just to be sure): 0.0
optimized x: 0.423654796297
optimized fun: 3.04165614917e-09
我正在尝试编写一个函数来反向求解 python 中另一个函数的变量,有点像 Excel 求解器所做的。
为了简化我的示例,我有一个函数接受多个变量然后计算价格。我会将实际值 (a、b、c、d、x) 传递给此函数,因此它 returns 是一个数值。
def calc_price(a,b,c,d,x):
value = a+b*c-d + x
return value
现在我得到了一个目标价格,以及 a、b、c、d。只有未知的是变量x,所以我想反解变量x。我想将其构建到一个函数中,该函数接收与 calc_price 相同的变量,以及一个附加变量 target_price.
def solve(target_price, a,b,c,d):
#this function takes in values for target_price, a,b,c,d
#and should do something like this:
target_price = calc_price(a,b,c,d,x)
solve for x <---------this is the part I'm not sure how to do
return x
我创建了一个像下面这样的函数来通过循环反向求解值 x 但它在计算大型数据集时效率低下,所以我正在寻找更有效的解决方案。
def solve(target_price,a,b,c,d):
x = 0.01
while x < 1:
if abs(target_price - calc_price(a,b,c,d,x)) < 0.001:
return x
x += 0.001
谢谢!
将此视为一个演示(因为你的任务对我来说仍然有点不清楚)并确保阅读 scipy's docs 以了解这些方法提供的基本保证。
有人可能会争辩说,基于 root-finding 的方法更合适(我们在这里最小化一个函数;因此残差函数中的 abs 构造),但这里的这种方法不需要你给出一些包围间隔。
代码:
import numpy as np
from scipy.optimize import minimize_scalar
np.random.seed(0)
""" Utils """
def calc_price(x, a, b, c, d):
value = a+b*c-d + x
return value
def calc_price_res(x, target, a, b, c, d):
value = a+b*c-d + x
return abs(value - target) # we are looking for f(x) == 0
""" Create fake-data (typically the job of OP!) """
a, b, c, d, x = np.random.random(size=5)
fake_target = calc_price(x, a, b, c, d)
print('a, b, c, d: ', a, b, c, d)
print('real x: ', x)
print('target: ', fake_target)
print('noisy obj (just to be sure): ', calc_price_res(x, fake_target, a, b, c, d))
""" Solve """
res = minimize_scalar(calc_price_res, args=(fake_target, a, b, c, d))
print('optimized x: ', res.x)
print('optimized fun: ', res.fun)
输出:
a, b, c, d: 0.548813503927 0.715189366372 0.602763376072 0.544883182997
real x: 0.423654799339
target: 0.858675077275
noisy obj (just to be sure): 0.0
optimized x: 0.423654796297
optimized fun: 3.04165614917e-09