【问题标题】:Non linear regression using curve_fit使用curve_fit的非线性回归
【发布时间】:2018-03-26 03:57:28
【问题描述】:

我试图将我的数据拟合到下面编写的函数中,但是当使用curve_fit 时,结果与数据完全不匹配。

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit

nu=[0.00,0.03,0.01,-0.02,0.00,-0.06]
data=np.loadtxt('impedancia.txt')
use=np.transpose(data)

Z=use[0]
omega=use[1]

def func(x,a,b,c):
   return a/(x**2)+b+c*x**2

popt,poc=curve_fit(func,omega,Z)
plt.plot(omega,Z,'bo',markersize=3.5)
plt.plot(omega,func(omega,*popt))`

我想知道是否有人可以帮助我。

【问题讨论】:

  • curve_fit() 中启动非线性求解器的默认起始参数默认都是 1.0,如果它们不是由用户提供的,如您发布的这段代码中所示。有时这些默认值不是最优的,所以这可能是这里的问题。如果您发布指向数据的链接,我可以使用 scipy.optimize.differential_evolution 模块来确定初始参数估计并以答案的形式发布代码,因为 cmets 不允许格式化代码。
  • 好的,谢谢你的帮助,这是数据链接:link

标签: python scipy regression


【解决方案1】:

这是我的代码和绘制结果,其中 scipy.optimize.differential_evolution 模块用于估计非线性求解器的初始参数。请注意,此代码使用类似于您的洛伦兹峰值方程的变体,但是第 20 和 21 行允许您选择方程。您的代码中的峰值方程似乎不适合数据的窄峰以及当前选择的推荐峰值方程。

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import warnings

from scipy.optimize import differential_evolution


# bounds on parameters are set in generate_Initial_Parameters() below
def func_original(x,a,b,c):
   return a/(x**2)+b+c*x**2


# bounds on parameters are set in generate_Initial_Parameters() below
def func_recommended(x,a,b,c):
    return a / (b + (x-c)**2)

# select peak function here
#func = func_original
func = func_recommended


# function for genetic algorithm to minimize (sum of squared error)
# bounds on parameters are set in generate_Initial_Parameters() below
def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
    return np.sum((yData - func(xData, *parameterTuple)) ** 2)


def generate_Initial_Parameters():
    # data min and max used for bounds
    maxX = max(xData)
    minX = min(xData)
    maxY = max(yData)
    minY = min(yData)

    minSearch = min([minX, minY])
    maxSearch = max([maxX, maxY])

    parameterBounds = []
    parameterBounds.append([minSearch, maxSearch]) # parameter bounds for a
    parameterBounds.append([minSearch, maxSearch]) # parameter bounds for b
    parameterBounds.append([minSearch, maxSearch]) # parameter bounds for c

    # "seed" the numpy random number generator for repeatable results
    result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
    return result.x

# load data from text file
data=np.loadtxt('impedancia.txt')
use=np.transpose(data)

yData=use[0]
xData=use[1]

# generate initial parameter values
initialParameters = generate_Initial_Parameters()

# curve fit the data
fittedParameters, niepewnosci = curve_fit(func, xData, yData, initialParameters)

# create values for display of fitted peak function
a, b, c = fittedParameters
y_fit = func(xData, a, b, c)

plt.plot(xData, yData, 'D') # plot the raw data
plt.plot(xData, y_fit) # plot the equation using the fitted parameters
plt.show()

print(fittedParameters)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-19
    • 2017-06-17
    • 1970-01-01
    • 2019-10-09
    • 2015-01-19
    • 2012-09-17
    • 2013-10-25
    • 2021-03-11
    相关资源
    最近更新 更多