【问题标题】:Least squares function with 5 unknown parameters具有 5 个未知参数的最小二乘函数
【发布时间】:2019-01-03 19:20:05
【问题描述】:

我在估计 5 个未知参数 a、b、c、d、e 时遇到问题,这些参数肯定位于区间中。它只是看起来像这样:

import numpy as np
from scipy.optimize import curve_fit

diap_a = np.arange(0.01, 1, 0.2)
diap_b = np.arange(0.01, 30, 5)
diap_c = np.arange(0.01, 2, 0.5)
diap_d = np.arange(0.01, 2, 0.5)
diap_e = np.arange(0.01, 0.3, 0.03)
X = np.arange(0.01, 1, 0.01) 

def func(a, b, c, d, e):
   return a + b + c + d + e #for example
Y = func(a, b, c, d, e)

我有这样的数据(预期值)

Y1 = [60, 59, 58, 57, 56, 55, 50, 30, 10]
X1 = [0.048, 0.049, 0.05, 0.05, 0.06, 0.089, 0.1, 0.12, 0.134]

我试图以这种方式实现它:

popt, pcov = curve_fit(func, a, b, c, d, e, Y1, X1)

找到有助于拟合曲线的最佳 a、b、c、d、e

plt.plot(Y, X)
plt.show()

但它不起作用。

结果是:

OptimizeWarning: Covariance of the parameters could not be estimated

抱歉,我对问题的表述不当。

【问题讨论】:

  • 这不是curve_fit 的用法。你读过documentation吗?
  • 是的,我试图使用文档中的示例来实现,但不明白如何包含这 5 个参数。

标签: python python-2.7 machine-learning optimization curve-fitting


【解决方案1】:

根据 curve_fit() 文档,您的 curve_fit() 应该将 func、X1 和 Y1 作为前三个参数。按照目前的编码,func() 将始终返回一个与 X1 无关且无法拟合数据的单个值。这是一个使用具有三个参数的数据并使用 scipy 的所有 1.0 的默认初始参数估计值的示例图形拟合器 - 这些并不总是最优的。如果您的数据不适用于任何给定函数,则可能是初始参数估计值,因此 scipy 有一个遗传算法模块可以在需要时帮助找到这些估计值。

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

xData = numpy.array([0.048, 0.049, 0.05, 0.05, 0.06, 0.089, 0.1, 0.12, 0.134])
yData = numpy.array([60, 59, 58, 57, 56, 55, 50, 30, 10])


def func(x, a, b, c): # simple quadratic example
    return (a * numpy.square(x)) + b * x + c


# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0])

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

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('Parameters:', fittedParameters)
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
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(xModel, *fittedParameters)

    # now the model as a line plot
    axes.plot(xModel, 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    相关资源
    最近更新 更多