无法在 Scipy 中使用 Rbf 插入数据
Unable to interpolate data using Rbf in 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, 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
怎么解决的?
使用您的原始数据(来自 griddata
的 SO),这有效:
清理 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.reshape(15,15)
的图像 - 左下角有 2 个正方形高原的正方形。
我尝试使用 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, 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
怎么解决的?
使用您的原始数据(来自 griddata
的 SO),这有效:
清理 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.reshape(15,15)
的图像 - 左下角有 2 个正方形高原的正方形。