2017-08-16 35 views
0

2次元グリッド上の関数の値zを指定します。グリッド座標はxとyと呼ばれます。SmoothBivariateSplineによる区分線形補間

ここで、任意の(x、y)に対してzの値を得ることができるように、データを補間したいと思います。

私の例がクリアされている、私は私のコードを短くしたくないことを確認するために:あなたが見たよう

import numpy as np 
import pylab as pl 
from scipy.interpolate import SmoothBivariateSpline 
from scipy.interpolate import interp1d 

def Func(x, y): # Define a test-function. 
    return y**2 # A parabola. 

SparseGridX, SparseGridY = np.mgrid[0:1:5j, 0:1:5j] # Generate a sparsely meshed grid. 

SparseGridZ = Func(SparseGridX, SparseGridY) # A function evaluated for np-arrays of arbitrary shape, again yields np-arrays of the same shape. Hence, this yields again an array. 

pl.figure(num='Sparsely sampled function.') 
pl.pcolor(SparseGridX, SparseGridY, SparseGridZ) # Plot the sparsely sampled function on the grid. 
pl.colorbar();pl.title("Sparsely sampled function.") 

FineListX = np.linspace(0,1,20) # Generate a fine-mesh list along x... 
FineListY = np.linspace(0,1,20) # ... as well as along y... 
FineGridX, FineGridY = np.mgrid[0:1:20j, 0:1:20j] # ... and the corresponding fine-mesh grid. 

ListedSparseGridX = SparseGridX.flatten() # Attain the list of x-coordinates in the fine-mesh grid. 
ListedSparseGridY = SparseGridY.flatten() # Attain the list of y-coordinates in the fine-mesh grid. 

ListedSparseGridZ = Func(ListedSparseGridX,ListedSparseGridY) # This yields a list, in which an element is the result of Func for the corresponding combination of elements of FineListX and FineListY. 

IntObj = SmoothBivariateSpline(ListedSparseGridX, ListedSparseGridY, ListedSparseGridZ, kx=1, ky=1) # This yields an interpolated object of the sparsely meshed points. 
Interpolation = IntObj(FineListX,FineListX) # This evaluates the interpolated object at the points of the fine-mesh grid and returns the corresponding array. 

pl.figure(num='Fine-meshed interpolation.') 
pl.pcolor(FineGridX, FineGridY, Interpolation) # Plot the interpolation in a fine-mesh grid. 
pl.colorbar();pl.title("Fine-meshed interpolation.") 

IntObj1Dim = interp1d(SparseGridY[3], SparseGridZ[3]) # This yields a one-dimensional interpolated object. The index 3 is arbitrary. 
Interpolation1Dim = IntObj1Dim(FineListY) # This evaluates the interpolated object along the fine-meshed y-coordinate. 

pl.figure(num="Plot only along y-coordinate.") 
pl.plot(SparseGridY[3],SparseGridZ[3], label="Points to interpolate", marker="o", ls="None") # Plot the points (along y) that were used for the interpolation. 
pl.plot(FineListY,Interpolation[5], label="Interpolation via SmoothBivariateSpline") # Plot the interpolation (along y). 
pl.plot(FineListY,Interpolation1Dim, label="Interpolation via interp1d") # Plot the one-dimensional interpolation. 
pl.legend(loc="best") 
pl.title("Plot only along y-coordinate.") 

、interp1dは正確にポイントを満たしている区分線形関数を与えます。対照的に、SmoothBivariateSplineは直線を生成します。私の目的は、interp1dで生成されたような区分的線形でもあるSmoothBivariateSplineを使った補間を生成することです。

SmoothBivariateSplineの引数sで試してみましたが、成功しませんでした。特に、私はs=0を試しました。この場合、補間はデータ点を正確に満たす必要がありますが、データ点を正確に満たす必要はありません。

ありがとうございます!

答えて

0

ここで私はLinearNDInterpolatorがより有用であることを発見しました。

ListedSparseGrid = np.asarray([(i,j) for i, j in zip(ListedSparseGridX,ListedSparseGridY)]) # Attain a list of all pairs of the elements of ListedSparseGridX and ListedSparseGridY. 
IntObjLNDI = LinearNDInterpolator(ListedSparseGrid, ListedSparseGridZ) # This yields an interpolated object of the sparsely meshed points. 
InterpolationLNDI = np.asarray([[IntObjLNDI(i,j) for j in FineListY] for i in FineListX]) # This evaluates the interpolated object at the points of the fine-mesh grid and returns the corresponding array. 

pl.figure(num='Fine-meshed interpolation using LinearNDInterpolator.') 
pl.pcolor(FineGridX, FineGridY, InterpolationLNDI) # Plot the interpolation in a fine-mesh grid. 
pl.colorbar();pl.title("Fine-meshed interpolation using LinearNDInterpolator.") 

一つと呼ばれる図を

pl.plot(FineListY,InterpolationLNDI[40], label="Interpolation via LinearNDInterpolator") # Plot the interpolation (along y). 

を添加することによって見ることができるように1-Dの結果はinterp1dの結果に等しい「だけy座標に沿ってプロット。」上記。