使用curve_fit进行非线性回归

时间:2018-03-25 11:43:31

标签: python scipy regression

我试图将我的数据放到下面写的函数中,但是当使用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))`

enter image description here

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

1 个答案:

答案 0 :(得分:0)

Here is my code and plotted result, with the scipy.optimize.differential_evolution module used to estimate initial parameters for the non-linear solver. Note that this code uses a variation on the Lorentzian peak equation similar to yours, however lines 20 and 21 allow you to select the equation. The peak equation in your code does not appear to fit the narrow peak of the data as well as the recommended peak equation currently selected.

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)

fitted equation