与RBF interpolation fails: LinAlgError: singular matrix非常相似,但我认为问题不同,因为我没有重复的坐标。
玩具示例:
import numpy as np
import scipy.interpolate as interp
coords = (np.array([-1, 0, 1]), np.array([-2, 0, 2]), np.array([-1, 0, 1]))
coords_mesh = np.meshgrid(*coords, indexing="ij")
fn_value = np.power(coords_mesh[0], 2) + coords_mesh[1]*coords_mesh[2] # F(x, y, z)
coords_array = np.vstack([x.flatten() for x in coords_mesh]).T # Columns are x, y, z
unique_coords_array = np.vstack({tuple(row) for row in coords_array})
unique_coords_array.shape == coords_array.shape # True, i.e. no duplicate coords
my_grid_interp = interp.RegularGridInterpolator(points=coords, values=fn_value)
my_grid_interp(np.array([0, 0, 0])) # Runs without error
my_rbf_interp = interp.Rbf(*[x.flatten() for x in coords_mesh], d=fn_value.flatten())
## Error: numpy.linalg.linalg.LinAlgError: singular matrix -- why?
我错过了什么?上面的示例使用函数F(x,y,z)= x ^ 2 + y * z。我想使用Rbf来近似该函数。据我所知,没有重复的坐标:将unique_coords_array
与coords_array
进行比较。
答案 0 :(得分:1)
我相信问题是你的意见:
my_rbf_interp = interp.Rbf(*[x.flatten() for x in coords_mesh],d=fn_value.flatten())
你应该改为:
x,y,z = [x.flatten() for x in coords_mesh]
my_rbf_interp = interp.Rbf(x,y,z,fn_value.flatten())
它应该有效。我认为你的原始公式是矩阵中的重复线,用于解决,因此与重复的问题非常相似(即奇异矩阵)。
如果你愿意的话:
d = fn_value.flatten()
my_rbf_interp = interp.Rbf(*(x,y,z,d))
它也应该有用。