使用 Pandas 和 Curve_fit 错误 func() 进行外推需要 3 个位置参数,但给出了 4 个

Extrapolating using Pandas and Curve_fit error func() takes 3 positional arguments but 4 were given

def func(x,a,b):

    return   a*x + b

guess = (0.5,0.5,0.5)


fit_df = df.dropna()

col_params = {}

for col in fit_df.columns:

    x = fit_df.index.astype(float).values

    y = fit_df[col].values

    params = curve_fit(func,x,y,guess)

    col_params[col] = params[0] 
for col in df.columns:

    x = df[pd.isnull(df[col])].index.astype(float).values

    df[col][x] = func(x,*col_params[col])
print("Extrapolated data")
print(df)   

我正在使用另一个 post 的代码来推断值。我更改了 func() 以便它是线性的而不是立方体但是我得到一个错误“func() 接受 3 个位置参数但 4 个被给出”

Extrapolate values in Pandas DataFrame 是我得到原始代码的地方。我的问题是我将如何更改它以使其与线性关系一起工作

使用时: guess = (0.5,0.5) 你应该可以做到 运行.

您有参数 a, b,而 original example 有参数 a, b, c, d。 最初的猜测是针对函数中的参数 a, b,而不是 x.

用于制作插值函数的完整代码运行:

import pandas as pd
from io import StringIO
from scipy.optimize import curve_fit

df = pd.read_table(StringIO('''
                neg       neu       pos       avg
    0           NaN       NaN       NaN       NaN
    250    0.508475  0.527027  0.641292  0.558931
    500         NaN       NaN       NaN       NaN
    1000   0.650000  0.571429  0.653983  0.625137
    2000        NaN       NaN       NaN       NaN
    3000   0.619718  0.663158  0.665468  0.649448
    4000        NaN       NaN       NaN       NaN
    6000        NaN       NaN       NaN       NaN
    8000        NaN       NaN       NaN       NaN
    10000       NaN       NaN       NaN       NaN
    20000       NaN       NaN       NaN       NaN
    30000       NaN       NaN       NaN       NaN
    50000       NaN       NaN       NaN       NaN'''), sep='\s+')

# Do the original interpolation
df.interpolate(method='nearest', xis=0, inplace=True)

# Display result
print ('Interpolated data:')
print (df)
print ()

def func(x,a,b):

    return   a*x + b

guess = (0.5,0.5)


fit_df = df.dropna()

col_params = {}

for col in fit_df.columns:

    x = fit_df.index.astype(float).values

    y = fit_df[col].values

    params = curve_fit(func,x,y,guess)

    col_params[col] = params[0] 
for col in df.columns:

    x = df[pd.isnull(df[col])].index.astype(float).values

    df[col][x] = func(x,*col_params[col])
print("Extrapolated data")
print(df)