【发布时间】: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