当使用scipy.optimize.curve_fit()将表面拟合到3D散射数据时发生TypeError >> [

问题描述 投票:0回答:1
我有一个Pandas DataFrame,其中的列包含x,y和z值。

import pandas as pd price_year_age = 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, price_year_age['Age'], # x-values price_year_age['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, price_year_age['Age'], # x-values price_year_age['Mileage'], # y-values price_year_age['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)适合却挂在3D (x, y, z)适合上?

我有一个Pandas DataFrame,其中的列包含x,y和z值。以pd price_year_age = pd.DataFrame({'Age':x,'Mileage':y,...

python scipy typeerror curve-fitting scatter3d
1个回答
0
投票
您正在使用不正确的参数调用该方法。
© www.soinside.com 2019 - 2024. All rights reserved.