Python Rbf给出奇异的矩阵错误,没有重复的坐标,为什么?

非常类似于
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.

最佳答案 我相信问题是你的意见:

    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))

它也应该工作.

点赞