本文介绍了为什么要"scipy.optimize.minimize"?给我这么不好的身材?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 y(x,z),其中有两个变量 x z 和6个系数 a b c d e f .我有 x z 的数据,可以说是出于测试目的,系数的数据.利用这些数据,我可以计算出 y .

I have a function y(x,z) with two variables x, z and 6 coefficients a,b,c,d,e,f. I have the data for x ,z and let's say for testing purpose the data of the coefficients. With those datas I calculate my y.

然后我想用 x z 和计算出的 y 数据拟合函数以获取系数并将其与测试目的之一.

Then I want to fit the function with the datas of x,z and the calculated y to get the coefficients and compare them with the testing purpose one.

import numpy as np
from scipy.optimize import minimize

x = np.array([0,0.25,0.5,0.75,1]) # data of variable x
z = np.array([0,0.25,0.5,0.75,1]) # data of variable z

def func(pars,x,z): #my function
    a,b,c,d,e,f = pars
    return a*x**2+b*x+c+d*z+e*z*x+f*z*x**2

a = np.array([1,1,1,1,1])  #define coefficients to get the y data and compare them later with fit
b = np.array([0.5,0.5,0.5,0.5,0.5])
c = np.array([0.25,0.25,0.25,0.25,0.25])
d = np.array([1,1,1,1,1])
e = np.array([0.5,0.5,0.5,0.5,0.5])
f = np.array([0.25,0.25,0.25,0.25,0.25])

y = []
y.append(func((a,b,c,d,e,f),x,z)) #calculate the y data
print(y)

def resid(pars,x,z,y): #residual function
    return ((func(pars,x,z) - y) ** 2).sum()

pars0 = np.array([0,0,0,0,0,0])
res = minimize(resid, pars0,args=(x,z,y), method='cobyla',options={'maxiter': 5000000})
print("a = %f , b = %f, c = %f, d = %f, e = %f, f = %f" % (res.x[0], res.x[1], res.x[2], res.x[3], res.x[4], res.x[5]))

我从拟合中得到以下系数:

I am getting the following coefficients from fitting:

a = 1.181149 , b = 1.228558, c = 0.253053, d = 0.219143, e = 0.444941, f = 0.172369

与我的用于计算 y 数据的系数相比,拟合并不符合我所说的适度.有人可以解释一下我的身材为什么这么差吗?

Compared with my coefficients for calculating the y data the fitting is not realy what I would call adquate. Can someone explain me why my fit is so bad?

P.S .:如果有人想知道,我会使用 cobyla ,因为稍后我必须定义一些约束.这只是一个测试代码,可以找出我的问题所在(希望如此).

P.S.: If someone is wondering, I use cobyla because I have to define some constraints later on. This is just a testing code to find out where my problem is located (hopefully).

推荐答案

查看 res.fun ,在您的情况下,围绕 1e-5 实际上是合适的很好.

Looking at res.fun, which is in your case around 1e-5 the fit is actually quite good.

您很可能找到了目标函数的局部最小值.为了更好地理解这种行为,请尝试下面的代码.对于不同的起点,这将产生不同的结果.正如您将看到的,您正在最小化,只是没有达到全局最小值.要进行全局优化,您必须使用其他方法/方法.您还可以增加何时停止优化的条件.或使用混合方法并从不同的起始点开始,解决局部最小化问题并获得最佳价值.

Most likely you found a local mininum of your objective function. To better understand this behaviour, try the code below. This will generate different results for different startpoints. As you will see, you are minimizing, just not to the global minimum. To optimize globally, you have to use other approaches/methods. You could also increase the criteria for when to stop the optimization. Or use a hybrid approach and start at different initial points, solve the local minimization and take the best value.

for i in range(10):
    pars0 = np.random.rand(6) * 1
    res = minimize(resid, pars0, args=(x,z,y), method='cobyla',options={'maxiter': 5000000})
    print("a = %f , b = %f, c = %f, d = %f, e = %f, f = %f" % (res.x[0], res.x[1], res.x[2], res.x[3], res.x[4], res.x[5]))
    print(res.fun)

尝试在您要寻找的解决方案附近的一个初始点.这很可能会产生全局结果.如果您不知道解决方案的模糊位置,则可能必须使用混合/全局方法来实现最小化.

Try an inital point close to the solution you are seeking. This will most probably yield the global result. If you don't know the vague location of your solution, you may have to use a hybrid/global approach to the minimization.

例如,初始点:

pars0 = np.array([1,0.5,0.25,1,0.5,0.25]) + np.random.rand(6)*0.01

提供了一个非常合适的解决方案.

yields a quite fitting solution.

这篇关于为什么要"scipy.optimize.minimize"?给我这么不好的身材?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 17:18