用Python计算KDE轮廓的面积

用Python计算KDE轮廓的面积,python,matplotlib,kde,Python,Matplotlib,Kde,因此,我通过以下方式在python中完成了KDE: y = coords[:,1] ## A set of y coordinates of the red points x = coords[:,0] ## A set of x coordinates of the red points k = gaussian_kde(np.vstack([x, y])) k.set_bandwidth(bw_method=k.factor / 2.) xi, yi = np.mgrid[x.mi

因此,我通过以下方式在python中完成了KDE:

y = coords[:,1]   ## A set of y coordinates of the red points
x = coords[:,0]   ## A set of x coordinates of the red points
k = gaussian_kde(np.vstack([x, y]))
k.set_bandwidth(bw_method=k.factor / 2.)

xi, yi = np.mgrid[x.min():x.max():x.size**0.8*1j,y.min():y.max():y.size**0.8*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))

fig, ax = plt.subplots(1, 1, figsize=(16, 8))

for coord in coordsold:
  cv2.circle(sample,
           (coord[1],coord[0]),2,
            (220, 0, 0), 1)

ax.contourf(yi, xi, zi.reshape(xi.shape), alpha=0.5)
    
ax.set_axis_off()
ax.imshow(sample)
我想计算的是等高线所占的面积。我想也许我可以把轮廓变成多边形对象,然后用这种方法计算面积,但我不确定。如果有人有一个聪明的解决方案,我们将不胜感激

见附件:


请添加一个最小运行示例。例如,
coords
的值是什么?这将对您有所帮助。