【问题标题】:TypeError when fitting surface to 3D scatter data using scipy.optimize.curve_fit()使用 scipy.optimize.curve_fit() 将曲面拟合到 3D 散射数据时出现 TypeError
【发布时间】:2020-02-05 03:49:09
【问题描述】:

我有一个 Pandas DataFrame,其中的列包含 x、y 和 z 值。

import pandas as pd
df = pd.DataFrame({'Age': x,
                   'Mileage': y,
                   'Price': z})

使用scipy.optimize.curvefit() 我可以拟合一个单变量指数函数y = exp(-bx)

import numpy as np
from scipy.optimize import curve_fit

# fit y to x
def exp_function(x, a, b):
    return a * np.exp(-b * x)

popt, pcov = curve_fit(exp_function, df['Age'],    # x-values
                        df['Price'],               # y-values
                        absolute_sigma=False, maxfev=1000)

# popt
# array([2.81641498e+04, 1.29183078e-01])                      # a, b-values

但是当我尝试将相同的分析扩展到 3D 时,我遇到了 TypeError

# fit z to (x, y)
def exp_function_2(x, y, a, b, c):
    return (a/2) * (np.exp(-b * x) + np.exp(-c * y))

popt, pcov = curve_fit(exp_function_2, 
                       df['Age'],           # x-values
                       df['Mileage'],       # y-values
                       df['Price'],         # z-values
                       absolute_sigma=False, maxfev=1000)


# TypeError: exp_function_2() takes 5 positional arguments but 1518 were given

似乎它认为我将 1518 个参数(我的 Pandas 数据帧的长度)传递给 exp_function_2()

为什么我的代码适用于 2D (x, y) fit 却无法使用 3D (x, y, z) fit?

【问题讨论】:

    标签: python scipy typeerror curve-fitting scatter3d


    【解决方案1】:

    您正在使用不正确的参数调用该方法。

    原型为curve_fit(f, xdata, ydata, p0=None, ...)docs indicate,其中p0 是函数参数的起始猜测值。因此,如果您有一个TypeError,您将框架的所有 1518 个元素作为默认参数传递给您的函数,该函数当然只接受 5 个参数。您的代码在 2D 情况下工作的事实是完全不使用 p0 关键字参数的巧合。

    您需要在xdata 中将两个预测变量作为单个参数传递,然后在指数函数中解压缩它们。像这样的东西(虽然我不确定我的数据框索引是否正确,但我很少使用熊猫):

    def exp_function_2(x, a, b, c):
        return (a/2) * (np.exp(-b * x['Age']) + np.exp(-c * x['Mileage']))
    

    【讨论】:

    • 完美运行!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2014-01-02
    • 2020-05-12
    • 2019-01-10
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多