从凸hu中去除异常值

2024-04-26 10:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据集,我想用凸包可视化(并从凸包中得到一些统计数据)。但是,每个数据集都包含一些噪声。因此,凸包不仅覆盖了主数据云中的点,而且还覆盖了所有的离群点,使得凸包的面积相当大,而且在不同的数据集之间没有很大的差别。数据集示例如下:

enter image description here

整个区域不是单峰的,但是我们可以观察到一些异常值(特别是在左边),这些异常值扰乱了凸壳的形状。估计的KDE如下所示: enter image description here

因此,我想删除这些异常值。什么算法可以用来拟合最小面积凸包的n-k点的数据集,其中k是设置为一些数字各自的给定百分比的观察?你知道吗

请注意,图片引用了一个示例,事实上我正在处理大量不同的数据集


Tags: 数据算法区域示例可视化图片数字噪声
1条回答
网友
1楼 · 发布于 2024-04-26 10:19:28

这是在R

set.seed(42)
#DATA
x = rnorm(20)
y = rnorm(20)

#Run convex hull
i = chull(x, y)

#Draw original data and convex hull
graphics.off()
plot(x, y, pch = 19, cex = 2)
polygon(x[i], y[i])

#Get coordinates of the center
x_c = mean(x)
y_c = mean(y)

#Calculate distance of each point from the center
d = sapply(seq_along(x), function(ind){
    dist(rbind(c(x_c, y_c), c(x[ind], y[ind])))
})

#Remove k points furthest from the center
k = 2
x2 = head(x[order(d)], -k)
y2 = head(y[order(d)], -k)
i2 = chull(x2, y2)

#Draw the smaller convex hull
points(x2, y2, pch = 19, col = "red")
polygon(x2[i2], y2[i2], border = "red", lty = 2)

enter image description here

相关问题 更多 >