【问题标题】:python curve_fitting with bad resultspython curve_fit 结果不好
【发布时间】:2018-11-12 18:53:45
【问题描述】:

the link of data from dropboxbadfitting我尝试使用curve_fit来拟合我在python中的pre_defined函数的数据,但结果远非完美。代码很简单,如下所示。我不知道出了什么问题。 由于我是python新手,有没有其他适合我的预定义函数的优化或拟合方法?

提前致谢!

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

def func(x, r1, r2, r3,l,c):
    w=2*math.pi*x
    m=r1+(r2*l*w)/(r2**2+l**2*w**2)+r3/(1+r3*c**2*w**2)
    n=(r2**2*l*w)/(r2**2+l**2*w**2)-r3**3*c*w/(1+r3*c**2*w**2)
    y= (m**2+n**2)**.5
    return y

def readdata(filename):
    x = filename.readlines()
    x = list(map(lambda s: s.strip(), x))
    x = list(map(float, x))
    return x

 # test data
f_x= open(r'C:\Users\adm\Desktop\simpletry\fre.txt')
xdata = readdata(f_x)

f_y= open(r'C:\Users\adm\Desktop\simpletry\impedance.txt')
ydata = readdata(f_y)

xdata = np.array(xdata)
ydata = np.array(ydata)
plt.semilogx(xdata, ydata, 'b-', label='data')

popt, pcov = curve_fit(func, xdata, ydata, bounds=((0, 0, 0, 0, 0), (np.inf, np.inf, np.inf, np.inf, np.inf)))
plt.semilogx(xdata, func(xdata, *popt), 'r-', label='fitted curve') 

print(popt)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

如您所料,这是一个 LCR 电路模型。现在我正在尝试用相同的参数拟合两条曲线,例如

def func1(x, r1, r2, r3,l,c):
w=2*math.pi*x
m=r1+(r2*l*w)/(r2**2+l**2*w**2)+r3/(1+r3*c**2*w**2)
return m

def func2(x, r1, r2, r3,l,c):
w=2*math.pi*x
n=(r2**2*l*w)/(r2**2+l**2*w**2)-r3**3*c*w/(1+r3*c**2*w**2)
return n

是否可以使用curve_fitting优化参数?

【问题讨论】:

  • 能否请您发布指向数据文件的链接?
  • 你知道大概的正确答案是什么吗?您可以使用它来指定可能有助于曲线拟合例程的起点和边界。
  • 看起来最适合的部分参数具有 值,因此您应该删除调用的 bounds=... 部分。 (另外,我怀疑您的拟合可能存在一些退化——即,不止一组参数也同样适合数据。)
  • 是的,这就是问题所在。我曾试图删除界限,它可以很好地适应。但事实是所有参数都必须是正数。

标签: python optimization scipy curve-fitting


【解决方案1】:

这是我使用 scipy 的差分进化遗传算法模块生成曲线拟合的初始参数估计的结果,以及函数中的简单“砖墙”以确保所有参数都是正的。 Scipy 的差分进化实现使用拉丁超立方算法来确保对参数空间的彻底搜索,这需要搜索范围 - 在本例中,这些范围取自数据最大值和最小值。我的结果:

RMSE:7.415

R 平方:0.999995

r1 = 1.16614005e+00

r2 = 2.00000664e+05

r3 = 1.54718886e+01

l = 1.94473531e+04

c = 4.32515535e+05

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

def func(x, r1, r2, r3,l,c):
    # "brick wall" ensuring all parameters are positive
    if r1 < 0.0 or r2 < 0.0 or r3 < 0.0 or l < 0.0 or c < 0.0:
        return 1.0E10 # large value gives large error, curve_fit hits a brick wall

    w=2*numpy.pi*x
    m=r1+(r2*l*w)/(r2**2+l**2*w**2)+r3/(1+r3*c**2*w**2)
    n=(r2**2*l*w)/(r2**2+l**2*w**2)-r3**3*c*w/(1+r3*c**2*w**2)
    y= (m**2+n**2)**.5
    return y


def readdata(filename):
    x = filename.readlines()
    x = list(map(lambda s: s.strip(), x))
    x = list(map(float, x))
    return x

 # test data
f_x= open('/home/zunzun/temp/data/fre.txt')
xData = readdata(f_x)

f_y= open('/home/zunzun/temp/data/impedance.txt')
yData = readdata(f_y)

xData = numpy.array(xData)
yData = numpy.array(yData)


# function for genetic algorithm to minimize (sum of squared error)
def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
    val = func(xData, *parameterTuple)
    return numpy.sum((yData - val) ** 2.0)


def generate_Initial_Parameters():
    # min and max used for bounds
    maxX = max(xData)
    minX = min(xData)
    maxY = max(yData)
    minY = min(yData)
    minBound = min(minX, minY)
    maxBound = max(maxX, maxY)
    parameterBounds = []
    parameterBounds.append([minBound, maxBound]) # search bounds for r1
    parameterBounds.append([minBound, maxBound]) # search bounds for r2
    parameterBounds.append([minBound, maxBound]) # search bounds for r3
    parameterBounds.append([minBound, maxBound]) # search bounds for l
    parameterBounds.append([minBound, maxBound]) # search bounds for c

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

# by default, differential_evolution completes by calling curve_fit() using parameter bounds
geneticParameters = generate_Initial_Parameters()

# now call curve_fit without passing bounds from the genetic algorithm,
# just in case the best fit parameters are aoutside those bounds
fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters)
print('Fitted parameters:', fittedParameters)
print()

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print()
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    plt.semilogx(xData, yData, 'D')

    # create data for the fitted equation plot
    yModel = func(xData, *fittedParameters)

    # now the model as a line plot
    plt.semilogx(xData, yModel) 

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

【讨论】:

  • 感谢您的代码。现在我正在尝试用 **def func1(x, r1, r2, r3,l,c):** w=2*math.pi*x m=r1+(r2*l*w)/(r2**2+l**2*w**2)+r3/(1+r3*c**2*w**2) return m **def func2(x, r1, r2, r3,l,c):** w=2*math.pi*x n=(r2**2*l*w)/(r2**2+l**2*w**2)-r3**3*c*w/(1+r3*c**2*w**2) return n 等相同参数拟合两条曲线,是否可以使用曲线拟合来优化参数?
  • 在我看来,您可以简单地将示例中的拟合函数替换为您评论中的代码。请注意,我使用了“numpy.pi”,因为我没有在示例代码中“导入数学”。
【解决方案2】:

要使最小二乘回归有意义,您至少必须提供有意义的初始参数。

由于默认情况下所有参数都初始化为值 1,因此对初始回归影响最大的将是电阻器 r1,它在组合中添加了一个常数。

很可能您最终会得到以下配置:

popt
Out[241]: 
array([1.66581563e+03, 2.43663552e+02, 1.13019744e+00, 1.20233767e+00,
       5.04984535e-04])

由于m = something big + ~0 + ~0,这将输出一条看起来整洁的扁平线; n=~0 - ~0,所以y = r1

但是,如果您以不同的方式初始化参数,

popt, pcov = curve_fit(func, xdata.flatten(), ydata.flatten(), p0=[0.1,1e5,1000,1000,0.2],
    bounds=((0, 0, 0, 0, 0), (np.inf, np.inf, np.inf, np.inf, np.inf)))

你会得到一个更好看的合身,

popt
Out[244]: 
array([1.14947146e+00, 4.12512324e+05, 1.36182466e+02, 8.29771756e+04,
       1.77593448e+03])

((fitted-ydata.flatten())**2).mean()
Out[257]: 0.6099524982664816
#RMSE hence 0.78

附:由于pd.read_clipboard 的转换错误,我的数据从第二个数据点开始,其中第一行变成了标题而不是数据。不过不应该改变整体情况。

【讨论】:

  • 您是如何确定这些初始参数估计值的?
  • 通过找出它是一个类似 RLC 电路的;粗略计算 C 小于 1,R 大约为数千;从曲线的最后部分注意到 R1 应该很小。在这种情况下,恐怕是体力劳动@JamesPhillips。然后根据合适的情况,我对 C 的假设是错误的.. :(
  • 我会花一周的时间写代码,以避免 10 分钟的体力劳动......
  • 恐怕我不想把时间花在编写有效地强制优化参数初始化的代码上,而只是选择一些数字;最好是不均匀的;会成功的。也许如果我需要复制类似的数千次,但不是针对单个实例。 ;)
  • scipy 中的遗传算法允许更通用的解决方案,广泛适用于许多问题 - 因此我的回答。
猜你喜欢
  • 2020-05-08
  • 2016-02-05
  • 2018-12-22
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
相关资源
最近更新 更多