Scipy:进行三次样条插值
scipy 三次样条插值
文章目录
- scipy 三次样条插值
- scipy.interpolate.CubicSpline
- scipy.interpolate.PPoly
- scipy.interpolate.PPoly
- 举例
3次样条插值即用两次连续可微的分段三次多项式插值数据,详细可参考 https://blog.****.net/bodybo/article/details/77335129
scipy.interpolate.CubicSpline
三次样条数据插值器,用两次连续可微的分段三次多项式插值数据。结果为PPoly类的对象,断点与给定数据匹配。
class
scipy.interpolate.CubicSpline
(x, y, axis=0, bc_type=‘not-a-knot’, extrapolate=Non )Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable . The result is represented as a PPoly instance with breakpoints matching the given data.
#参数表
x:xarray_like, shape (n,)
包含自变量值的一维数组。值必须是实的、有限的和严格递增的顺序。
y: yarray_like
包含因变量值的数组。它可以有任意数量的尺寸,但沿轴的长度必须与x的长度匹配。值必须是有限的。
axis: int, optional
Axis along which y is assumed to be varying. Meaning that for x[i] the corresponding values are np.take(y, i, axis=axis). Default is 0.
假设y沿其变化的轴。含义为 x[i] 对应的值为 np.take(y,i,axis = axis).缺省为0.bc_type: string or 2-tuple, optional, 字符串或2-tuple, 可选。
边界条件类型。由边界条件给出的两个附加方程需要确定每段多项式的所有系数。
如果bc_type是字符串,则指定的条件将应用于样条曲线的两端。可用条件为:
‘not-a-knot’ (默认值): 曲线末端的第一段和第二段是相同的多项式。当没有关于边界条件的信息时,这是一个很好的默认值。
‘periodic’: The interpolated functions is assumed to be periodic of period
x[-1] - x[0]
. The first and last value of y must be identical:y[0] == y[-1]
. This boundary condition will result iny'[0] == y'[-1]
andy''[0] == y''[-1]
.
插值函数假定为周期函数,周期为从x[-1]到x[0]. y的第一个与最后一个值必须相等:y[0] == y[-1]
. 这种边界条件的结果是y'[0] == y'[-1]
and `y’’[0] == y’’[-1],即 上下边界的一阶导数、二街导数相同。‘clamped’: The first derivative at curves ends are zero. Assuming a 1D y,
bc_type=((1, 0.0), (1, 0.0))
is the same condition. 曲线端点(上下限)的一阶导数为0. 假设1维的y数组,与 边界条件 bc_type = ((1, 0.0), (1, 0.0))相同。‘natural’: The second derivative at curve ends are zero. Assuming a 1D y,
bc_type=((2, 0.0), (2, 0.0))
is the same condition. 曲线端点(上下限)的二阶导数为0. 假设1维的y数组,与 边界条件 bc_type = ((2, 0.0), (2, 0.0))相同。If bc_type is a 2-tuple, the first and the second value will be applied at the curve start and end respectively. The tuple values can be one of the previously mentioned strings (except ‘periodic’) or a tuple (order, deriv_values) allowing to specify arbitrary derivatives at curve ends:
如果bc_type是2元组,则第一个值和第二个值将分别应用于曲线的起点和终点。元组值可以是前面提到的字符串之一(除了“周期性”)或元组(阶数、导数值),允许在曲线末端指定任意导数:
order: the derivative order, 1 or 2. 边界条件中导数的阶数,可以为1或2.
deriv_value: array_like containing derivative values, shape must be the same as y, excluding
axis
dimension. For example, if y is 1D, then deriv_value must be a scalar. If y is 3D with the shape (n0, n1, n2) and axis=2, then deriv_value must be 2D and have the shape (n0, n1).
包含导数的数组,shape必须与因变量y相同。 比如,y是1维,deriv_value 必须是标量。 如果y是 3维 并且axis=2, 那么 deriv_value 必须是2D, 形状为(n0,n1)extrapolate{bool, ‘periodic’, None}: optional, 可选.
If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. If None (default),
extrapolate
is set to ‘periodic’ forbc_type='periodic'
and to True otherwise.
如果是布尔值,则确定是基于第一个和最后一个间隔外推到越界点,还是返回NaNs。如果是“周期”,则使用周期外推。如果无(默认值),“extrapolate” 对于 “bc_type“=‘periodic’ 为periodic’, 其它为 ‘True’。
Parameters bc_type and interpolate
work independently, i.e. the former controls only construction of a spline, and the latter only evaluation.
参数bc_type和插值是独立工作的,即前者只控制三次样条曲线的构造,后者只进行求值。
scipy.interpolate.PPoly
参数表:
scipy.interpolate.PPoly
# 分段多项式类
class scipy.interpolate.PPoly(c, x, extrapolate=None, axis=0)
# Piecewise polynomial in terms of coefficients and breakpoints
# The polynomial between x[i] and x[i + 1] is written in the local power basis:
# S = sum(c[m, i] * (xp - x[i])**(k-m) for m in range(k+1))
# where k is the degree of the polynomial.
参数表:
c **:**ndarray, shape (k, m, …) 为k次多项式,m个区间,即k行m列数据,每一列代表一个多项式; (Polynomial coefficients, order k and
m intervals)
x **:**ndarray, shape (m+1,) 为多项式区间断点,m+1 个断点有m个区间,必须升序或降序排列 (Polynomial breakpoints. Must be sorted in either
increasing or decreasing order.)
**extrapolate :**bool or ‘periodic’, optional,‘布尔型’ 或 ‘周期型’;
如果是bool型,确定是基于第一个和最后一个间隔外推到边界点,还是返回NaNs。如果是“周期”,则使用周期外推法。默认是真的。 (If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. Default is True.
axis : int, optional,整型可选,插值的轴向,缺省为0. (Interpolation axis. Default is zero.)
举例
from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
# 构造x,y 数组,x为自变量、y为因变量
x = np.arange(10)
y = np.sin(x)
# 使用 CubiSpline 构建分段三次样条插值函数cs,cs即为scipy.interpolate.PPoly类的对象
# 默认情况下,三次样条端点边界条件为‘not-a-knot’ (默认值),曲线末端的第一段和第二段是相同的多项式。当没有关于边界条件的信息时,这是一个很好的默认值。
cs = CubicSpline(x, y)
cs为scipy.interpolate._cubic.CubicSpline 的对象,
#其中 cs.c为各分段多项式的系数,[a3i,a2i,a1i,a0i], 本案例有10个数据点,分割为9段,故有9个多项式,依次为:
p0 p1 p2 p3 p4 p5 p6 p7 p8
a3 -0.041850 -0.041850 0.146891 0.160545 0.036476 -0.120620 -0.178734 -0.025369 -0.025369
a2 -0.261272 -0.386822 -0.512372 -0.071699 0.409935 0.519363 0.157503 -0.378697 -0.454806
a1 1.144593 0.496499 -0.402696 -0.986768 -0.648533 0.280765 0.957632 0.736438 -0.097065
a0 0.000000 0.841471 0.909297 0.141120 -0.756802 -0.958924 -0.279415 0.656987 0.989358
cs的关键属性除cs.c外,还有cs.x,代表数据点
cs.c.shape, cs.x,cs(x), y
# 输出为 三次样条分段多项式的形状,x数据点,插值x数据点=原始数据y值
((4, 9),
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]),
array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,
-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849]),
array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,
-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849]))
插值调用接口简单即为 c s ( x i ) cs(x_i) cs(xi), x i x_i xi 可以为单个的数也可为数组
# 构造插值点
xs = np.arange(-0.5, 9.6, 0.1)
fig, ax = plt.subplots(figsize=(6.5, 4))
ax.plot(x, y, 'o', label='data')
ax.plot(xs, np.sin(xs), label='true')
ax.plot(xs, cs(xs), label="S-插值点")
# 一阶导数
ax.plot(xs, cs(xs, 1), label="S'-插值处的一阶导数")
ax.set_xlim(-0.5, 9.5)
ax.legend(loc='lower left', ncol=2)
plt.show()
上一篇: 用C++实现的三次样条插值
推荐阅读
-
利用曲线拟合进行插值和平滑处理
-
快速赛车路径规划--三次插值实现
-
用三重矩方程和追赶法进行三次样条插值的数值方法实验(MATLAB 代码)
-
WPF 简单实现牛顿多项式插值法和三次样条插值法
-
统计学习 04:假设检验(以 t 检验为例)和 P 值 - 要点 I. 假设检验的一般思路 假设检验 清楚你的问题是什么?期望得出什么结论? 例如,两种药物的疗效是否存在差异,自变量与因变量之间是否存在回归关系 .... 请始终牢记,假设检验回答的是是否存在某种关系的问题:它并不衡量这种关系有多大。 提出两种假设:零假设 (H0) 和备择假设 (H1) 零假设与备择假设相反,一般来说,研究的目的是证明原假设是错误的,即得出备择假设的结论。 例如,如果实验预期希望两种药物的疗效存在差异,那么 H0:μ1 - μ2 = 0;H1:μ1 - μ2 ≠ 0 H0:μ1-μ2 = 0 的一般形式称为双侧检验,而 >、<等零假设称为单侧检验。一般来说双侧检验更为常见,下面也主要介绍这种方法。 单尾或双尾测试 根据原始数据计算零假设概率分布的统计量(t 值、Z 值、F 值等)。 根据问题的性质选择合适的概率检验方法,从而计算出相应的统计量值;因此,不同情况的统计量值有不同的计算方法。 根据计算出的统计量值,利用统计软件,可以知道相应的 p 值是多少 也可以先确定一个合适的显著性水平(0.0.001....),并计算其临界值,再与我们计算出的统计量值进行比较,从而做出判断。 根据第四步的比较结果,如果 p 值小于预期的显著性水平(α,通常设定为 0.05),则认为该统计量远离原假设分布,属于小概率事件,则拒绝原假设,从而接受备择假设。 决定 要点 2:以 t 检验为例,演示上述假设检验思路。 t 检验基于 t 分布,常见的 t 检验有三种,如下图所示,但我认为第三种配对设计可能更常用(零假设:差异是否为零),下面介绍的例子就是一种配对设计 三次 t 检验 举例测量两组大鼠肝脏中维生素 A 的含量,比较两组大鼠维生素 A 含量是否有差异。数据如下 数据 (1) 预计两组大鼠的维生素 A 水平存在差异 (2) H0:μd=0,H1:μd≠0,α=0.05,双侧检验 (3) t 统计量的计算 配方 计算 上述程序计算的是*度为 7 的 t 分布情况下的 t 值。只需理解公式即可,不同的方法有不同的公式,这些交给统计软件即可。
-
深入解析:最近邻、线性、双线性和双三次图像插值方法,超实用,记得收藏哦!
-
【20220207】【信号处理】详解三次样条插值的原理
-
算法笔记:样条插值技巧
-
三次样条插值优缺点_matlab中三次样条差值
-
CUDA中一维三次样条插值的讨论与解析