Python interp1d vs. UnivariateSpline

后端 未结 4 1774
攒了一身酷
攒了一身酷 2020-12-28 20:21

I\'m trying to port some MatLab code over to Scipy, and I\'ve tried two different functions from scipy.interpolate, interp1d and UnivariateSpline. The interp1d results matc

4条回答
  •  情歌与酒
    2020-12-28 20:52

    Works for me,

    from scipy import allclose, linspace
    from scipy.interpolate import interp1d, UnivariateSpline
    
    from numpy.random import normal
    
    from pylab import plot, show
    
    n = 2**5
    
    x = linspace(0,3,n)
    y = (2*x**2 + 3*x + 1) + normal(0.0,2.0,n)
    
    i = interp1d(x,y,kind=3)
    u = UnivariateSpline(x,y,k=3,s=0)
    
    m = 2**4
    
    t = linspace(1,2,m)
    
    plot(x,y,'r,')
    plot(t,i(t),'b')
    plot(t,u(t),'g')
    
    print allclose(i(t),u(t)) # evaluates to True
    
    show()
    

    This gives me,

    enter image description here

提交回复
热议问题