pandas.interpolate中spline(三次样条插值法)的用法

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=1时图像等同与linear方法,即线性插值
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
其中order=5时报错
在这里插入图片描述
已找到查找fp = s的平滑样条所允许的最大迭代次数maxit(由程序设置为20):s
返回了一个近似值,但是残差平方的相应加权和不满足abs(fp-s)/ s <tol的条件。

三次样条插值是一种常用的插值方,用于在给定的一组数据点上拟合一条平滑的曲线。在Python,使用Scipy库interpolate模块进行三次样条插值。以下是两种不同的代码实现: 方一:使用Pandas库读取数据 ```python import matplotlib.pyplot as plt import pandas as pd import numpy as np from scipy import interpolate # 导入数据 file = pd.read_csv('data.txt', sep='\s ', header=None, skiprows=[17], names=['x', 'value']) data = pd.DataFrame(file) # 数组切片 x = data['x'] # 取第一列数据 y = data['value'] # 取第二列数据 # 进行样条插值 tck = interpolate.splrep(x, y) xx = np.linspace(min(x), max(x), 100) yy = interpolate.splev(xx, tck, der=0) # 画图 plt.plot(x, y, 'o', xx, yy) plt.legend(['true', 'Cubic-Spline']) plt.xlabel('距离(cm)') plt.ylabel('%') plt.title('管线仪实测剖面图') # 保存图片 plt.savefig('out2.png', dpi=600) # 设置需要保存图片的分辨率 plt.show() ``` 此代码使用Pandas库读取数据,并进行样条插值后绘制图像。 方二:使用Numpy库读取数据 ```python import matplotlib.pyplot as plt from scipy import interpolate import numpy as np import matplotlib.font_manager as mpt zhfont = mpt.FontProperties(fname='/usr/share/fonts/custom/msyh.ttf') # 显示文字体 # 导入数据 file = 'data.txt' a = np.loadtxt(file) # 数组切片 x = a[:, 0 # 取第一列数据 y = a[:, 1 # 取第二列数据 # 进行样条插值 tck = interpolate.splrep(x, y) xx = np.linspace(min(x), max(x), 100) yy = interpolate.splev(xx, tck, der=0) # 画图 plt.plot(x, y, 'o', xx, yy) plt.legend(['true', 'Cubic-Spline']) plt.xlabel('距离(cm)', fontproperties=zhfont) # 注意后面的字体属性 plt.ylabel('%') plt.title('管线仪实测剖面图', fontproperties=zhfont) # 保存图片 plt.savefig('out.jpg') plt.show() ``` 此代码使用Numpy库读取数据,并进行样条插值后绘制图像。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python三次样条插值](https://blog.csdn.net/qq_41365597/article/details/90676249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.239^v12^insert_chatgpt"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值