带有样条线和日期时间对象的平滑线不起作用 [英] Smooth line with spline + datetime objects doesn't work

查看:97
本文介绍了带有样条线和日期时间对象的平滑线不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图像



scipy.interpolate.spline 含糊不清,表明可能不被支持。例如,它没有在 scipy.interpolate上列出主页或在入门教程源代码为 spline 显示它实际上调用了 spleval splmake = http://docs.scipy.org/doc/scipy/reference/interpolate.html#additional-tools rel = nofollow noreferrer>其他工具如下:


为向后兼容而存在的功能(不应在新代码中使用)。


我会遵循 cricket_007 的建议,并使用 interp1d 。这是当前建议的方法,教程和API中的详细示例,并且默认情况下允许对自变量进行不排序(任何顺序)(请参阅API中的 assume_sorted 参数)

 >>)。从scipy.interpolate导入interp1d 
>> f = interp1d(X,Y,kind ='quadratic')
>> f(Xsmooth)
array([711.74,720.14123457,726.06049383,729.49777778,
730.45308642,728.92641975,724.91777778,718.4271605,
709.4545679,698.])

如果数据排名不足,也会引发错误。

 >> f = interp1d(X,Y,kind ='cubic')




ValueError:x和y数组必须至少包含4个条目



I have been trying to make a plot smoother like it is done here, but my Xs are datetime objects that are not compatible with linspace..

I convert the Xs to matplotlib dates:

Xnew = matplotlib.dates.date2num(X)
X_smooth = np.linspace(Xnew.min(), Xnew.max(), 10)
Y_smooth = spline(Xnew, Y, X_smooth)

But then I get an empty plot, as my Y_smooth is

[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]

for some unknown reason.

How can I make this work?

EDIT

Here's what I get when I print the variables, I see nothing abnormal :

X : [datetime.date(2016, 7, 31), datetime.date(2016, 7, 30), datetime.date(2016, 7, 29)]
X new: [ 736176.  736175.  736174.]
X new max: 736176.0
X new min: 736174.0
XSMOOTH [ 736174.          736174.22222222  736174.44444444  736174.66666667
  736174.88888889  736175.11111111  736175.33333333  736175.55555556
  736175.77777778  736176.        ]
Y [711.74, 730.0, 698.0]
YSMOOTH [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

解决方案

Your X values are reversed, scipy.interpolate.spline requires the independent variable to be monotonically increasing, and this method is deprecated - use interp1d instead (see below).

>>> from scipy.interpolate import spline
>>> import numpy as np
>>> X = [736176.0, 736175.0, 736174.0]  # <-- your original X is decreasing
>>> Y = [711.74, 730.0, 698.0]
>>> Xsmooth = np.linspace(736174.0, 736176.0, 10)
>>> spline(X, Y, Xsmooth)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

reverse X and Y first and it works

>>> spline(
...     list(reversed(X)),  # <-- reverse order of X so also
...     list(reversed(Y)),  # <-- reverse order of Y to match
...     Xsmooth
... )
array([  698.        ,   262.18297973,   159.33767533,   293.62017489,
         569.18656683,   890.19293934,  1160.79538066,  1285.149979  ,
        1167.41282274,   711.74      ])

Note that many spline interpolation methods require X to be monotonically increasing:

x : (N,) array_like - 1-D array of independent input data. Must be increasing.

x : (N,) array_like - Input dimension of data points – must be increasing

The default order of scipy.interpolate.spline is cubic. Because there are only 3 data points there are large differences between a cubic spline (order=3) and a quadratic spline (order=2). The plot below shows the difference between different order splines; note: 100 points were used to smooth the fitted curve more.

The documentation for scipy.interpolate.splineis vague and suggests it may not be supported. For example, it is not listed on the scipy.interpolate main page or on the interploation tutorial. The source for spline shows that it actually calls spleval and splmake which are listed under Additional Tools as:

Functions existing for backward compatibility (should not be used in new code).

I would follow cricket_007's suggestion and use interp1d. It is the currently suggested method, it is very well documented with detailed examples in both the tutorial and API, and it allows the independent variable to be unsorted (any order) by default (see assume_sorted argument in API).

>>> from scipy.interpolate import interp1d
>>> f = interp1d(X, Y, kind='quadratic')
>>> f(Xsmooth)
array([ 711.74      ,  720.14123457,  726.06049383,  729.49777778,
        730.45308642,  728.92641975,  724.91777778,  718.4271605 ,
        709.4545679 ,  698.        ])

Also it will raise an error if the data is rank deficient.

>>> f = interp1d(X, Y, kind='cubic')

ValueError: x and y arrays must have at least 4 entries

这篇关于带有样条线和日期时间对象的平滑线不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆