使用三个参数 returns 最小化 scipy 中的函数初始猜测
Minimizing a function in scipy with three parameters returns the initial guess
一组坐标为 x
和 y
的点看起来像 this。我想在 y = 0
下面的区域中构造一条形式为 - a - np.exp(-(x - b)/c)
的曲线,其中参数 a
、b
和 c
的条件是 90 y = 0
下面的 % 点被这一行和相关函数括起来。
我已经编写了以下代码来执行此操作,但是 minimize
函数给出了初始猜测结果,我不知道我遗漏了什么。
from scipy.optimize import minimize
import numpy as np
def enclosed_points(params):
a, b, c = params
den = (y < 0).sum() # Calculate the number of points with y coordinate below y0
func = - a - np.exp(-(x - b)/c) # Calculate the value of the function for each x
num = ((y < 0) & (y > func)).sum() # Calculate the number of points with y coordinate
# below y0 and above the function
return np.abs(num/den - 0.9) # Return the absolute value of the difference between
# the ratio of num and den and the target number (0.9)
initial_guess = [0.1, 0.2, 1] # Dummy initial guess
result = minimize(enclosed_points, initial_guess)
编辑。 Here 我已经以 npy 格式上传了整个数据的随机样本。
好吧,我尝试了一些不同的方法并更改了您的代码的某些部分:
def func(x, a, b, c):
return - a - np.exp(-(x - b)/c)
def enclosed_points(params):
a, b, c = params
loss1 = y[np.argwhere( y > 0)]
loss2 = loss1[np.argwhere( loss1 < func(x[np.argwhere( y > 0)], a, b, c) )]
loss = ((loss2.sum() / len(y)) - 0.9)**2
return loss
initial_guess = [-0.1, 0.2, 1]
result = minimize(enclosed_points, initial_guess, method='SLSQP', options={'eps': 1e-2})
loss1 和 loss2 与你的损失函数做同样的工作,但我将 abs 改为 2 的幂(因为在我看来它更常见)(也添加了“method='SLSQP', options={'eps': 1e-2}
”到你的基于最小化器的在 Whosebug 上的另一个 post 上;尝试仔细阅读它们并熟悉他们关于这个最小化器的问题)。但是,我认为主要问题是您的问题是 non-convex,而 minimize
函数试图找到局部最小值。请参阅 post 以获得全面的描述。
最后,我会说用 [-0.1, 0.2, 1] 初始点我可以找到一个解决方案(为 a
尝试不同的负值,也许你可以找到另一个解决方案:))
祝你好运
一组坐标为 x
和 y
的点看起来像 this。我想在 y = 0
下面的区域中构造一条形式为 - a - np.exp(-(x - b)/c)
的曲线,其中参数 a
、b
和 c
的条件是 90 y = 0
下面的 % 点被这一行和相关函数括起来。
我已经编写了以下代码来执行此操作,但是 minimize
函数给出了初始猜测结果,我不知道我遗漏了什么。
from scipy.optimize import minimize
import numpy as np
def enclosed_points(params):
a, b, c = params
den = (y < 0).sum() # Calculate the number of points with y coordinate below y0
func = - a - np.exp(-(x - b)/c) # Calculate the value of the function for each x
num = ((y < 0) & (y > func)).sum() # Calculate the number of points with y coordinate
# below y0 and above the function
return np.abs(num/den - 0.9) # Return the absolute value of the difference between
# the ratio of num and den and the target number (0.9)
initial_guess = [0.1, 0.2, 1] # Dummy initial guess
result = minimize(enclosed_points, initial_guess)
编辑。 Here 我已经以 npy 格式上传了整个数据的随机样本。
好吧,我尝试了一些不同的方法并更改了您的代码的某些部分:
def func(x, a, b, c):
return - a - np.exp(-(x - b)/c)
def enclosed_points(params):
a, b, c = params
loss1 = y[np.argwhere( y > 0)]
loss2 = loss1[np.argwhere( loss1 < func(x[np.argwhere( y > 0)], a, b, c) )]
loss = ((loss2.sum() / len(y)) - 0.9)**2
return loss
initial_guess = [-0.1, 0.2, 1]
result = minimize(enclosed_points, initial_guess, method='SLSQP', options={'eps': 1e-2})
loss1 和 loss2 与你的损失函数做同样的工作,但我将 abs 改为 2 的幂(因为在我看来它更常见)(也添加了“method='SLSQP', options={'eps': 1e-2}
”到你的基于最小化器的在 Whosebug 上的另一个 post 上;尝试仔细阅读它们并熟悉他们关于这个最小化器的问题)。但是,我认为主要问题是您的问题是 non-convex,而 minimize
函数试图找到局部最小值。请参阅 a
尝试不同的负值,也许你可以找到另一个解决方案:))
祝你好运