将scipy.stats.gaussian_kde应用于3D点云中

问题描述 投票:0回答:1

我在一个csv文件里有一组大约33K(x,y,z)的点,想用scipy.stats.gaussian_kde把它转换成密度值网格。我无法找到一种方法将这个点云数组转换为gaussian_kde函数的适当输入格式(然后将其输出转换成密度值网格)。谁能提供示例代码?

scipy
1个回答
2
投票

这里有一个例子和一些注释,可能会有用。gaussian_kde 希望数据和点是行堆叠的,即(# ndim, # num values),就像文档中说的那样。在你的情况下,你会 row_stack([x, y, z]) 以致于 (3, 33000).

from scipy.stats import gaussian_kde
import numpy as np
import matplotlib.pyplot as plt

# simulate some data
n = 33000
x = np.random.randn(n)
y = np.random.randn(n) * 2

# data must be stacked as (# ndim, # n values) as per docs.
data = np.row_stack((x, y))

# perform KDE
kernel = gaussian_kde(data)

# create grid over which to evaluate KDE
s = np.linspace(-8, 8, 128)
grid = np.meshgrid(s, s)
# again KDE needs points to be row_stacked
grid_points = np.row_stack([g.ravel() for g in grid])

# evaluate KDE and reshape result correctly
Z = kernel(grid_points)
Z = Z.reshape(grid[0].shape)

# plot KDE as image and overlay some data points
fig, ax = plt.subplots()

ax.matshow(Z, extent=(s.min(), s.max(), s.min(), s.max()))
ax.plot(x[::10], y[::10], 'w.', ms=1, alpha=0.3)
ax.set_xlim(s.min(), s.max())
ax.set_ylim(s.min(), s.max())

output

© www.soinside.com 2019 - 2024. All rights reserved.