Python 2.7 无法在Scipy中使用Rbf插值数据
我尝试使用Rbf插值数据Python 2.7 无法在Scipy中使用Rbf插值数据,python-2.7,numpy,scipy,Python 2.7,Numpy,Scipy,我尝试使用Rbf插值数据 import numpy as np, matplotlib.pyplot as plt from scipy.interpolate import Rbf x = np.array([110, 112, 114, 115, 119, 120, 122, 124]).astype(float) y = np.array([60, 61, 63, 67, 68, 70, 75, 81]).astype(float) d = np.array([4, 6, 5, 3,
import numpy as np, matplotlib.pyplot as plt
from scipy.interpolate import Rbf
x = np.array([110, 112, 114, 115, 119, 120, 122, 124]).astype(float)
y = np.array([60, 61, 63, 67, 68, 70, 75, 81]).astype(float)
d = np.array([4, 6, 5, 3, 2, 1, 7, 9]).astype(float)
ulx, lrx = np.min(x), np.max(x)
uly, lry = np.max(y), np.min(y)
xi = np.linspace(ulx, lrx, 4)
yi = np.linspace(uly, lry, 4)
rbfi = Rbf(x, y, d)
di = rbfi(xi, yi)
plt.imshow(di)
plt.show()
然而,我得到:
TypeError: Invalid dimensions for image data
解决方案如何?使用您的原始数据(来自SO Withgriddata
),这将起作用:
清理x
,y
,删除异常值:
yreg=y.reshape(15,15)[:,[0]].repeat(15,1).flatten()
xreg=x.reshape(15,15)[[0],:].repeat(15,0).flatten()
ulx, lrx = np.min(xreg), np.max(xreg)
uly, lry = np.max(yreg), np.min(yreg)
N = 20
xi = np.linspace(ulx, lrx, N)
yi = np.linspace(uly, lry, N)
# grided_data = interpolate.griddata((xreg, yreg), z, (xi.reshape(1,-1), yi.reshape(-1,1)),
method='nearest',fill_value=0)
我认为Rbf
(以及类似的插值器)不像griddata
那样处理广播。所以我必须构造2个向量来定义所有的插值点
yyi=np.repeat(yi,N)
xxi=np.repeat(xi[None,:],N,0).flatten()
rbfi=interpolate.Rbf(xreg,yreg,z,function='linear')
zzi=rbfi(xxi,yyi).reshape(N,N)
时间方面,Rbf
明显比griddata
慢
使用xreg
,yreg
,插值结果(两种方法)看起来类似于z的图像。重塑(15,15)
-左下角有两个正方形平台的正方形