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

标签 python scipy interpolation

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

它应该也能工作。

关于Python Rbf 给出没有重复坐标的奇异矩阵错误,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033459/

相关文章:

python - 类型提示 : when to annotate

python - 如何正确使用 scikit-learn 的高斯过程进行 2D 输入、1D 输出回归?

python - ValidationError 或 TypeError、ValueError - 异常

python - python中数组之间的插值

python - LDA Gensim Word -> 主题 ID 分布而不是主题 -> 单词分布

python - 为什么 scipy.ststs.contingency.expected_freq 返回的预期频率不是我所期望的?

python scipy.sparse.linalg.eigs 为连续调用提供不同的结果

python - 当坐标保存在对象中时,使用 python 中的 kd-tree 查找 k 个最近邻

r - R 样条插值

ruby-on-rails - 为什么回形针插值不起作用?