为什么 dataframe.interpolate 与样条曲线会产生意想不到的波浪

问题描述 投票:0回答:1

我正在尝试使用 dataframe.interpolate 来填充丢失的数据。这是我的测试:

from itertools import product
df=pd.DataFrame.from_dict({
    1.5      :[np.nan    ,91.219     ,np.nan     ,np.nan     ,102.102    ,np.nan     ,np.nan     ], 
    2.0      :[np.nan    ,np.nan     ,np.nan     ,np.nan     ,103.711    ,np.nan     ,103.031    ], 
    2.5      :[np.nan    ,98.25      ,np.nan     ,100.406    ,104.695    ,np.nan     ,104.938    ], 
    3.0      :[np.nan    ,101.578    ,np.nan     ,102.969    ,104.875    ,np.nan     ,105.242    ], 
    3.5      :[np.nan    ,103.859    ,87.93      ,104.531    ,104.906    ,np.nan     ,105.32     ], 
    4.0      :[np.nan    ,105.156    ,94.469     ,105.656    ,105.844    ,89.68      ,106.523    ], 
    4.5      :[94.266    ,106.039    ,96.82      ,106.75     ,103.156    ,93.703     ,107.938    ], 
    5.0      :[97.336    ,107.953    ,98.602     ,107.906    ,104.25     ,96.547     ,109.703    ], 
    5.5      :[99.664    ,110.438    ,100.203    ,108.906    ,100.375    ,98.844     ,110.188    ], 
    6.0      :[101.344   ,112.703    ,101.492    ,108.688    ,102.906    ,100.68     ,110.5      ], 
    6.5      :[102.313   ,112.078    ,102.266    ,108.813    ,104.5      ,101.875    ,104    ], 
    7.0      :[102.656   ,114.469    ,102.242    ,108.813    ,np.nan     ,102.625    ,109    ], 
    7.5      :[103.25    ,np.nan     ,102.594    ,108.813    ,np.nan     ,103.234    ,109    ], 
    }, orient='index')
df.plot(title='original')
for int_method,int_order in list(product(['spline'],range(1,4)))+[
    (x,3) for x in ['nearest', 'zero', 'slinear', 'quadratic', 'cubic', 'barycentric', 'polynomial',
              'krogh', 'piecewise_polynomial', 'pchip', 'akima', 'cubicspline','from_derivatives','linear',
              ]
]:
    spl=df.interpolate(limit_direction='both',method=int_method,order=int_order)
    spl.plot(title=f'{int_method},{int_order}')

似乎只有样条曲线可以给我我需要的外推。然而,我发现它似乎增加了一些意想不到的波动:

有人可以帮助我了解发生了什么,甚至提供一些关于如何改进的建议(我知道“改进”在这里是一个模糊的短语。我自己找不到明确的定义)? 谢谢!

python interpolation missing-data spline extrapolation
1个回答
0
投票

这是因为 pandas.DataFrame.interpolate 在内部调用 scipy.interpolate,并且 scipy.interpolate 会在进行插值之前对 X 进行排序(在图中它们位于 Y 轴上)。显然这不是我们想要的,它混合了左侧和右侧的动量。

© www.soinside.com 2019 - 2024. All rights reserved.