我分别对此问题有一个类似的答案:RBF interpolation: LinAlgError: singular matrix
python - 具有rbf和scipy的2d概率分布-LMLPHP

但是我想用rbf进行概率分布。

直到现在我的代码:

from scipy.interpolate.rbf import Rbf  # radial basis functions
import cv2
import matplotlib.pyplot as plt
import numpy as np

x = [1, 1, 2 ,3, 4, 4, 2, 6, 7]
y = [0, 2, 5, 6, 2, 4, 1, 5, 2]

rbf_adj = Rbf(x, y, function='gaussian')

plt.figure()

# Plotting the original points.
plot3 = plt.plot(x, y, 'ko', markersize=12)  # the original points.

plt.show()


我的问题是我只有点的坐标:x,y
但是我可以对z和d使用什么?

这是我的错误信息:

numpy.linalg.linalg.LinAlgError: Matrix is singular.

最佳答案

首先,这是一个一维示例,旨在强调径向基函数插值与概率分布的内核密度估计之间的差异:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

from scipy.interpolate.rbf import Rbf  # radial basis functions
from scipy.stats import gaussian_kde

coords = np.linspace(0, 2, 7)
values = np.ones_like(coords)

x_fine = np.linspace(-1, 3, 101)

rbf_interpolation = Rbf(coords, values, function='gaussian')
interpolated_y = rbf_interpolation(x_fine)

kernel_density_estimation = gaussian_kde(coords)

plt.figure()
plt.plot(coords, values, 'ko', markersize=12)
plt.plot(x_fine, interpolated_y, '-r', label='RBF Gaussian interpolation')
plt.plot(x_fine, kernel_density_estimation(x_fine), '-b', label='kernel density estimation')
plt.legend(); plt.xlabel('x')
plt.show()


python - 具有rbf和scipy的2d概率分布-LMLPHP

这是对所提供的数据使用高斯RBF进行的2D插值,并且通过将值任意设置为z = 1:

from scipy.interpolate.rbf import Rbf  # radial basis functions
import matplotlib.pyplot as plt
import numpy as np

x = [1, 1, 2 ,3, 4, 4, 2, 6, 7]
y = [0, 2, 5, 6, 2, 4, 1, 5, 2]
z = [1]*len(x)

rbf_adj = Rbf(x, y, z, function='gaussian')

x_fine = np.linspace(0, 8, 81)
y_fine = np.linspace(0, 8, 82)

x_grid, y_grid = np.meshgrid(x_fine, y_fine)

z_grid = rbf_adj(x_grid.ravel(), y_grid.ravel()).reshape(x_grid.shape)

plt.pcolor(x_fine, y_fine, z_grid);
plt.plot(x, y, 'ok');
plt.xlabel('x'); plt.ylabel('y'); plt.colorbar();
plt.title('RBF Gaussian interpolation');


python - 具有rbf和scipy的2d概率分布-LMLPHP

关于python - 具有rbf和scipy的2d概率分布,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51647590/

10-10 18:21