问题描述
我尝试使用 Rbf 插入数据.
I tried to interpolate the data using 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),这有效:
With your original data (from the SO with griddata
), this works:
清理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 个向量来定义所有的插值点.
I don't think Rbf
(and similar interpolators) handle broadcasting like griddata
does. So I have to construct 2 vectors defining all the interpolation points.
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
慢.
Timing wise, Rbf
is noticeably slower than griddata
.
使用xreg
、yreg
,插值结果(两种方法)看起来类似于z.reshape(15,15)
- 左下角有 2 个正方形高原的正方形.
With xreg
, yreg
, the interpolation results (for both methods) look similar to the image of z.reshape(15,15)
- a square with 2 square plateaus in the lower left corner.
这篇关于无法在 Scipy 中使用 Rbf 插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!