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

时间:2018-08-02 07:12:19

标签: python python-3.x scipy interpolation gaussian

我分别对此问题有一个类似的答案:RBF interpolation: LinAlgError: singular matrix https://i.stack.imgur.com/QpGM6.png

但是我想用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.

1 个答案:

答案 0 :(得分:4)

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

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

kde vs rbf

这是对提供的数据使用高斯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');

rbf example