具有rbf和scipy的2d概率分布 [英] 2d probability distribution with rbf and scipy
问题描述
我分别对此问题有一个类似的答案: RBF插值:LinAlgError:奇异矩阵
I have something similar to this problem respectivly the answer of this problem: RBF interpolation: LinAlgError: singular matrix
但是我想用rbf进行概率分布.
But I want to do the probability distribution with 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使用什么呢?
My problem is I have only coordinates of the points: x, y But what can i use for z and d?
这是我的错误消息:
numpy.linalg.linalg.LinAlgError: Matrix is singular.
推荐答案
首先,这是一个一维示例,着重强调径向基函数内插与内核密度估计之间的差异的概率分布:
This is, first, a 1D example to emphasis the difference between the Radial Basis Function interpolation and the Kernel Density Estimation of a probability distribution:
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()
这是对所提供的数据使用高斯RBF的2D插值,并且通过将值任意设置为z = 1 :
And this is the 2D interpolation using Gaussian RBF for the provided data, and by setting arbitrarily the values to 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和scipy的2d概率分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!