微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pandas 插值方法定义

如何解决Pandas 插值方法定义

pandas documentation 中,提供了许多方法作为 pandas.DataFrame.interpolate 的参数,包括

nearest'、'zero'、'slinear'、'quadratic'、'cubic'、'spline'、'barycentric'、'polynomial':传递给 scipy.interpolate.interp1d。这些方法使用索引的数值。 “多项式”和“样条”都要求您还指定一个阶数 (int),例如df.interpolate(method='polynomial',order=5).

'krogh'、'piecewise_polynomial'、'spline'、'pchip'、'akima'、'cubicspline':围绕类似名称的 SciPy 插值方法的包装。见注释

但是,scipy documentation 表示以下选项:

kind str 或 int,可选 将插值类型指定为字符串或整数,指定要使用的样条插值器的顺序。字符串必须是“linear”、“nearest”、“nearest-up”、“zero”、“slinear”、“quadratic”、“cubic”、“prevIoUs”或“next”之一。 “零”、“线性”、“二次”和“三次”是指零阶、一阶、二阶或三阶样条插值; “上一个”和“下一个”简单地返回该点的上一个或下一个值;在插入半整数(例如 0.5、1.5)时,“nearest-up”和“nearest”不同,“nearest-up”向上舍入,“nearest”向下舍入。认为“线性”。

文档似乎有误,因为 scipy.interpolate.interp1d 不接受 barycentricpolynomial 作为有效方法。我想 barycentric 指的是 scipy.interpolate.barycentric_interpolate,但是 polynomial 指的是什么?我认为它可能等同于 piecewise_polynomial 选项,但两者给出了不同的结果。

此外,method=cubicsplinemethod=spline,order=3 给出不同的结果。这里有什么区别?

解决方法

pandas 插值方法是来自 numpyscipy 库中不同位置的插值方法的合并。

目前所有代码都位于 pandas/core/missing.py

在高层次上,它将 splits the interpolation 方法转化为由 np.iterp 处理的方法,以及由整个 scipy 库处理的其他方法。

# interpolation methods that dispatch to np.interp
NP_METHODS = ["linear","time","index","values"]

# interpolation methods that dispatch to _interpolate_scipy_wrapper
SP_METHODS = ["nearest","zero","slinear","quadratic","cubic","barycentric","krogh","spline","polynomial","from_derivatives","piecewise_polynomial","pchip","akima","cubicspline"]

然后,由于 scipy 方法被拆分为不同的方法,您可以看到 missing.py 中有大量其他包装器指示 scipy 方法。大多数方法都传递给 scipy.interpolate.interp1d;但是对于其他一些方法,有一个 dict 或其他包装方法指向这些特定的 scipy 方法。

from scipy import interpolate

alt_methods = {
    "barycentric": interpolate.barycentric_interpolate,"krogh": interpolate.krogh_interpolate,"from_derivatives": _from_derivatives,"piecewise_polynomial": _from_derivatives,}

其中 _from_derivativesmissing.py 的文档字符串表示:

def _from_derivatives(xi,yi,x,order=None,der=0,extrapolate=False):
    """
    Convenience function for interpolate.BPoly.from_derivatives.
    ...
    """

因此 TLDR,根据您指定的方法,您可以直接使用以下方法之一:

numpy.interp
scipy.interpolate.interp1d
scipy.interpolate.barycentric_interpolate
scipy.interpolate.krogh_interpolate
scipy.interpolate.BPoly.from_derivatives
scipy.interpolate.Akima1DInterpolator
scipy.interpolate.UnivariateSpline
scipy.interpolate.CubicSpline

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。