from pandas import read_csv
from pandas import datetime
from matplotlib import pyplot
num_of_order = 3
def parser(x):
return datetime.strptime('190'+x, '%Y-%m')
series = read_csv('shampoo.csv',header=0, index_col=0, parse_dates=True,
squeeze=True, date_parser=parser)
upsampled = series.resample('D').mean()
interpolated = upsampled.interpolate(method='spline', order=num_of_order)
print(interpolated.head(32))
interpolated.plot()
pyplot.title('order=%d'% num_of_order)
pyplot.savefig('D:\\360\\%d.png' % num_of_order)
其中spline(三次样条插值法)原理详解:Spline(三次样条插值)
这里主要讲解spline中order的用法,order代表的是多项式的项数
pandas中interpolate的解释:Passed to scipy.interpolate.interp1d. These methods use the numerical values of the index. Both ‘polynomial’ and ‘spline’ require that you also specify an order (int), e.g. df.interpolate(method='polynomial', order=5).
下面是order为1、2、3、4、5的图像:
order=1时图像等同与linear方法,即线性插值
其中order=5时报错
已找到查找fp = s的平滑样条所允许的最大迭代次数maxit(由程序设置为20):s
返回了一个近似值,但是残差平方的相应加权和不满足abs(fp-s)/ s <tol的条件。