【问题标题】:Extrapolating using Pandas and Curve_fit error func() takes 3 positional arguments but 4 were given使用 Pandas 和 Curve_fit 推断错误 func() 需要 3 个位置参数,但给出了 4 个
【发布时间】:2020-11-17 12:06:58
【问题描述】:
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)   

我正在使用另一篇文章中的代码来推断值。我更改了 func() 使其是线性的而不是立方的,但是我收到一个错误“func() 需要 3 个位置参数,但有 4 个是给定的”

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

【问题讨论】:

    标签: python pandas scipy extrapolation


    【解决方案1】:

    使用时: 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)   
    

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      相关资源
      最近更新 更多