简体   繁体   中英

Interpolating array columns with PiecewisePolynomial in scipy

I'm trying to interpolate each column of a numpy array using scipy's PiecewisePolynomial . I know that this is possible for scipy's interp1d but for piecewise polynomial interpolation it does not seem to work the same way. 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]) . It seems that only the first column in y1 was taken into account for interpolation. How can I make the method return the interpolated values of each column in y1 at the points specified by x ?

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.

It may be that you do not actually want to use the PiecewisePolynomial at all, if you do not have the derivatives available. If you just want to have a smoother interpolation, then try interp1d with, eg, kind='quadratic' keyword argument. (See the documentation for 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM