Python: interpolate.UnivariateSpline package 'error: (m>k) failed for hidden m: fpcurf0:m=0'

笑着哭i 提交于 2021-02-05 06:48:05

问题


I have been attempting to plot a line, along with a spline fitting. The following is a generalised version of my code. 'x_coord' and 'y_coord' are lists containing lists of float values.

import matplotlib.pyplot as plt
from scipy import interpolate as ipl

for a in range(len(x_coord)):
    plt.plot(x_coord[a],y_coord[a],label='Label')
    yinterp = ipl.UnivariateSpline(x_coord[a],y_coord[a],s=1e4)(x_coord[a])
    plt.plot(x_coord[a],yinterp,label='Spline Fit')

While I believe this has worked for me in the past, I now obtain an error message:

/.../Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/interpolate/fitpack2.pyc in __init__(self, x, y, w, bbox, k, s, ext)
    165 
    166         data = dfitpack.fpcurf0(x,y,k,w=w,
--> 167                                 xb=bbox[0],xe=bbox[1],s=s)
    168         if data[-1] == 1:
    169             # nest too small, setting to maximum bound

error: (m>k) failed for hidden m: fpcurf0:m=0 

I have seen cases of similar error messages (e.g. dfitpack.error: (m>k) failed for hidden m: fpcurf0:m=1), only in that particular case there seemed to be problems involving dictionaries, of which none are used in my code.

Any advice on this matter would be greatly appreciated.


回答1:


You are trying to instantiate a UnivariateSpline object with a zero-length array

    >>> from scipy.interpolate import UnivariateSpline
    >>> UnivariateSpline([], [])
    <snip>
    dfitpack.error: (m>k) failed for hidden m: fpcurf0:m=0
    >>> 
    >>> UnivariateSpline([1], [2])
    Traceback (most recent call last):
   <snip>
    dfitpack.error: (m>k) failed for hidden m: fpcurf0:m=1

The error is likely triggered by this line, which sets a hidden variable m to the length of x and checks that you have at least k+1 points where k is the spline degree (default is cubic, k=3).

>>> spl = UnivariateSpline(range(4), range(4))
>>> spl(2)
array(2.0)


来源:https://stackoverflow.com/questions/32230362/python-interpolate-univariatespline-package-error-mk-failed-for-hidden-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!