【问题标题】:roots of piecewise cubic hermite interpolator with pythonpython 分段三次厄米特插值器的根
【发布时间】:2016-07-06 21:56:15
【问题描述】:

我想做一些分段三次 Hermite 插值并得到多项式的 。 (我曾经在 matlab 中执行此操作,但现在想在 python 3.4 中实现此操作)。
我尝试使用 scipy PchipInterpolator。插值可以,但是当我尝试检索根时出现此错误...
我现在被困在这里,找不到任何解决方法。这是一个简单的代码,重现了我所做的事情以及确切的错误消息...

import matplotlib.pyplot as plt
from scipy import interpolate, __version__
import numpy as np

print('numpy : ' + np.__version__)
print('scipy :' + __version__)

x = np.arange(10)
y = [1., 1., 3., 2., 1., 1., 1.5, 2., 8., 1.]
f = interpolate.PchipInterpolator(x, y, axis=0, extrapolate=None)
print(f.roots()) # this produces an error !

xnew = np.arange(0, 9, 0.1)
ynew = f(xnew)   # use interpolation function returned by `PchipInterpolator`
plt.plot(x, y, 'o', xnew, ynew, '-')
plt.show()

错误信息:

numpy : 1.10.1
scipy :0.17.1
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    print(f.roots()) # this produces an error !
  File "/usr/local/lib/python3.4/dist-packages/scipy/interpolate/_monotone.py", line 105, in roots
    return (PPoly.from_bernstein_basis(self._bpoly)).roots()
AttributeError: 'PchipInterpolator' object has no attribute '_bpoly'

Process finished with exit code 1

但是 scipy 文档说:“roots() 返回插值函数的根。”

我在这里错过了什么?

【问题讨论】:

    标签: python matlab scipy interpolation


    【解决方案1】:

    这是 scipy 中的一个错误。

    (请在https://github.com/scipy/scipy/issues举报)

    作为一种解决方法,您可以手动转换为幂基:

    In [1]: from scipy.interpolate import pchip
    
    In [2]: import numpy as np
    
    In [3]: x = np.arange(10)
    
    In [4]: y = [1., 1., 3., 2., 1., 1., 1.5, 2., 8., 1.]
    
    In [5]: s = pchip(x, y)
    
    In [6]: from scipy.interpolate import PPoly
    
    In [7]: pp = PPoly.from
    PPoly.from_bernstein_basis  PPoly.from_spline
    
    In [7]: pp = PPoly.from_bernstein_basis(s)
    
    In [8]: pp.roots()
    Out[8]: array([  9.07179677,  22.92820323])
    

    编辑:如果确实使用此解决方法并且您有非默认 axis,则最好在转换时检查轴是否已处理。如果不是,请也报告。

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2013-04-06
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2016-08-07
      • 2017-10-10
      • 1970-01-01
      相关资源
      最近更新 更多