0

我有这个方程 a*(t^alpha)*(p_p^beta) 我想拟合得到 t 和 p_p 是自变量的 alpha 和 beta 值。我的问题是如何编写最终的拟合模型(结果)表达式。

result = model.fit(S_L1, params, t=t, p_p=p_p)

我尝试了类似上面的表达式,但我收到了这个错误:

ValueError: The input contains nan values


# Calculating unburned mass temperature
T_u = T_i*(p_filter/p_i)**((k_u-1)/k_u)                     # Linear unburned temperature
t = T_u/T_i
p_p = p_filter/p_i


# Model function.
def mod_m(t, p_p, a=1, alpha=1,beta=1):                             # Define function with initial guesses
    return a*(t**alpha)*(p_p**beta)                                 # Function for fitting


# Fitting model.
model = Model(mod_m, independent_vars=['t','p_p'] )

# Making a set of parameters:
params = model.make_params(a=10)

# Setting  min/max bounds on parameters:
params['alpha'].min = 0.0
params['beta'].min = 0.0
params['a'].min = 0.0
params['a'].max = 1e6


# Run the fit with Model.fit(Data_Array, Parameters, independent vars).
result = model.fit(S_L1, params, t=t, p_p=p_p)
4

2 回答 2

1

这是一个 Python 3 示例,它使用您的函数和测试数据。这将 scipy.optimize.curve_fit() 用于多元回归并创建 3D 数据散点图、拟合函数的 3D 曲面图和拟合函数的等高线图。请注意,我使用curve_fit 的默认scipy 初始参数。

import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import  Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels

# 3D contour plot lines
numberOfContourLines = 16


def SurfacePlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

    axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

    axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label
    axes.set_zlabel('Z Data') # Z axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ContourPlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot(x_data, y_data, 'o')

    axes.set_title('Contour Plot') # add a title for contour plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    CS = matplotlib.pyplot.contour(X, Y, Z, numberOfContourLines, colors='k')
    matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) # labels for contours

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)
    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    axes.scatter(x_data, y_data, z_data)

    axes.set_title('Scatter Plot (click-drag with mouse)')
    axes.set_xlabel('X Data')
    axes.set_ylabel('Y Data')
    axes.set_zlabel('Z Data')

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def func(data, a, alpha, beta):
    t = data[0]
    p_p = data[1]
    return a * (t**alpha) * (p_p**beta)


if __name__ == "__main__":
    xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
    yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
    zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

    data = [xData, yData, zData]

    # this example uses curve_fit()'s default initial paramter values
    fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData)

    ScatterPlot(data)
    SurfacePlot(func, data, fittedParameters)
    ContourPlot(func, data, fittedParameters)

    print('fitted prameters', fittedParameters)
于 2018-03-30T15:27:58.170 回答
0

您的问题是“我如何编写最终的拟合模型(结果)表达式?”。你自己回答了这个问题

def mod_m(t, p_p, a=1, alpha=1,beta=1): 
    return a*(t**alpha)*(p_p**beta)

model = Model(mod_m, independent_vars=['t','p_p'] )

是的,这正是如何编写拟合模型。

这本身不会导致异常

ValueError: The input contains nan values

导致这种情况的原因ValueError是您的 fit 函数nan使用您给它的参数和自变量的值生成值。那么......你为那些传递了什么值?

我建议打印出模型函数中参数的值和自变量的值。需要明确的是,幂运算很容易产生大于 1e308 的值,这将给出inf, 并导致您看到的异常。因此,您可能必须更加小心允许哪些参数值,这可能对自变量的值很敏感。

于 2018-03-31T02:20:11.407 回答