通过使用scipy.optimize.fmin_cobyla得到错误的结果 [英] Get the wrong result by using scipy.optimize.fmin_cobyla

查看:136
本文介绍了通过使用scipy.optimize.fmin_cobyla得到错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对scipy非常陌生,现在我通过做一些小尝试来努力使用函数in scipy.optimize.

I am very new to scipy and now I struggle to use functions in scipy.optimize by making some little experiment.

我试图通过找到误差值最低的参数来拟合sin函数.

I tried to fit a sin-function by finding the parameter, which has lowest error-value.

使用的功能是fmin_cobyla

代码如下:

import matplotlib.pyplot as plt
from scipy.optimize import fmin_cobyla
from scipy.optimize import fmin_slsqp
from scipy.optimize import leastsq
import numpy as np
from sympy import *

noise = np.random.randn(100)

def func_model(x, para):
    ''' Model: y = a*sin(2*k*pi*x+theta)'''
    a, k, theta = para
    return a*np.sin(2*k*np.pi*x+theta)

def func_noise(x, para):
    a, k, theta = para
    return a*np.sin(2*k*np.pi*x+theta) + noise

def func_error(para_guess):
    '''error_func'''
    x_seq = np.linspace(-2*np.pi, 0, 100)
    para_fact = [10, 0.34, np.pi/6]
    data = func_noise(x_seq, para_fact)
    error_value  = data - func_model(x_seq, para_guess)
    return error_value

# 1<a<15  0<k<1  0<theta<pi/2
constraints = [lambda x: 15 - x[0], lambda x: x[0]- 1, \
               lambda x: 1 - x[1],  lambda x: x[1], \
               lambda x: np.pi/2 - x[2], lambda x: x[2]]

para_guess_init = np.array([7, 0.2, 0])

solution = fmin_cobyla(func_error, para_guess_init, constraints)
print(solution)   # supposed to be like [10, 0.34, np.pi/6]

xx = np.linspace(-2*np.pi, 0, 100)
plt.plot(xx, func_model(xx, [10, 0.34, np.pi/6]), label="raw")
plt.plot(xx, func_noise(xx, [10, 0.34, np.pi/6]), label="with noise")
plt.plot(xx, func_model(xx, solution), label="fitted")
plt.legend()
plt.show()

运行后我得到了结果

solution = [1.6655938 0.59868667 0.0731335]

solution = [1.6655938 0.59868667 0.0731335]

这肯定不是正确的答案

有人可以帮我吗.预先感谢..

Could someone help me. Thanks in advance..

推荐答案

这里似乎有两件事显然是错误的:首先,您每次调用目标函数时都在改变噪声,因此您的优化试图达到目标一个移动的目标.在调用fmin_cobyla之前设置模拟数据:

There are two things that seem obviously wrong here: firstly, you're changing the noise each time your objective function is called, so your optimization is trying to hit a moving target. Set the simulated data before calling fmin_cobyla:

the_noise = np.random.randn(100)
data = func_noise(x_seq, para_fact)

此外,您的func_error应该返回每个点的模型和数据之间的差,而不是平方和差:

Also, your func_error should return the difference between the model and the data for each point, not the sum-of-squares difference:

def func_error(para_guess):
    error_value = data - func_model(x_seq, para_guess)
    return error_value

您仍然可能会发现fmin_cobyla难以找到受约束的最小值...进行一些预处理以更好地估计相位或频率的初始猜测可能会帮助您.

You still may find that fmin_cobyla struggles to find the constrained minimum... some pre-processing to better estimate the initial guess for the phase or frequency might help you here.

这篇关于通过使用scipy.optimize.fmin_cobyla得到错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆