[英]Interpolating array columns with PiecewisePolynomial in scipy
I'm trying to interpolate each column of a numpy array using scipy's PiecewisePolynomial
. 我正在尝试使用scipy的
PiecewisePolynomial
插入numpy数组的每一列。 I know that this is possible for scipy's interp1d
but for piecewise polynomial interpolation it does not seem to work the same way. 我知道这对于scipy的
interp1d
是可行的,但是对于分段多项式插值,它似乎并不能以相同的方式工作。 I have the following code: 我有以下代码:
import numpy as np
import scipy.interpolate as interpolate
x1=np.array([1,2,3,4])
y1=np.array([[2,3,1],[4,1,6],[1,2,7],[3,1,3]])
interp=interpolate.PiecewisePolynomial(x1,y1,axis=0)
x = np.array([1.2, 2.1, 3.3])
y = interp(x)
Which results in y = np.array([2.6112, 4.087135, 1.78648])
. 结果为
y = np.array([2.6112, 4.087135, 1.78648])
。 It seems that only the first column in y1
was taken into account for interpolation. 似乎只考虑了
y1
的第一列。 How can I make the method return the interpolated values of each column in y1
at the points specified by x
? 如何使该方法返回
x
指定的点处y1
中每一列的插值?
The scipy.interpolate.PiecewisePolynomial
inteprets the different columns of y1
as the derivatives of the function to be interpolated, whereas interp1d
interprets the columns as different functions. 所述
scipy.interpolate.PiecewisePolynomial
inteprets的不同列y1
作为函数的导数将被内插,而interp1d
解释列作为不同的功能。
It may be that you do not actually want to use the PiecewisePolynomial
at all, if you do not have the derivatives available. 如果您没有可用的导数,则可能根本就根本不想使用
PiecewisePolynomial
。 If you just want to have a smoother interpolation, then try interp1d
with, eg, kind='quadratic'
keyword argument. 如果只想有一个更平滑的插值,则尝试使用
interp1d
,例如, kind='quadratic'
关键字参数。 (See the documentation for interp1d
) (请参阅
interp1d
的文档)
Now your function looks rather interesting 现在您的功能看起来很有趣
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = linspace(0,5,200)
ax.plot(x, interp(x))
ax.plot(x1, y1[:,0], 'o')
If you try the quadratic spline interpolation: 如果尝试二次样条插值:
interp = scipy.interpolate.interp1d(x1, y1.T, kind='quadratic')
fig = plt.figure()
ax = fig.add_subplot(111)
x = linspace(1,4,200)
ip = interp(x)
ax.plot(x, ip[0], 'b')
ax.plot(x, ip[1], 'g')
ax.plot(x, ip[2], 'r')
ax.plot(x1, y1[:,0], 'bo')
ax.plot(x1, y1[:,1], 'go')
ax.plot(x1, y1[:,2], 'ro')
This might be closer to what you want: 这可能更接近您想要的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.