我有以下類型的數據/劇情優化修剪的K-means用於聚類具有多個異常值的2D數據?更好的方法?
只看單獨的數據點,這幾乎是不可能判斷其中峯是應該的,但如果在ggplot與2D密度平滑拉我得到這些非常漂亮的山峯,在那裏我可以直觀地計算出〜我想要找到的10組點數。 「有效團體」的確切數量當然是要討論的。
數據在這裏: https://pastebin.com/5wquw7UF
library(ggplot2)
library(colorRamps)
library(tclust)
ggplot(data = df, aes(x = x, y = y)) +
stat_density2d(geom = "raster",
aes(fill = ..density..),
contour = FALSE) +
geom_point(col = "white", alpha = 0.1) +
scale_x_continuous(expand = c(0,0),
limits = c(0,1)) +
scale_y_continuous(expand = c(0,0),
limits = c(0,1)) +
theme_tufte(base_size = 11, base_family = "Helvetica") +
theme(axis.text = element_text(color = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.7),
legend.key.height = unit(2.5,"line"),
legend.key.width = unit(1, "line")) +
scale_fill_gradientn(name = "Density",
colours = matlab.like(1000))
我看着修剪集羣,與包tclust
。通過擺弄我已經能夠拿出下面的數據。然而,無論我如何擺弄這些參數,我都無法像我看到的那樣,讓團隊看起來「緊張」。特別是第5組似乎潛入它不屬於的地方。第10組也有點奇怪,但隔離到足以丟棄之後。
有沒有更好的方法,或者它只是我不理解如何正確設置參數?
set.seed(2)
trimmed_cluster <- tclust(
x = df,
k = 10, # 9
alpha = 0.1, # 0.1
drop.empty.clust = FALSE,
equal.weights = TRUE,
restr = c("sigma", "eigen"), # sigma
restr.fact = 1
)
df$cluster <- trimmed_cluster$cluster
trimmed_cluster_centers <- data.frame(t(trimmed_cluster$centers))
df_clustered <- subset(df, cluster != 0)
ggplot(data = df, aes(x = x, y = y)) +
stat_density2d(geom = "raster",
aes(fill = ..density..),
contour = FALSE) +
geom_point(data = df_clustered, aes(x = x, y = y, col = as.factor(cluster))) +
geom_text(data = trimmed_cluster_centers,
aes(x = x, y = y, label = as.character(1:length(trimmed_cluster_centers$x))),
size = 5,
fontface = "bold",
col = "yellow2") +
scale_x_continuous(expand = c(0,0),
limits = c(0,1)) +
scale_y_continuous(expand = c(0,0),
limits = c(0,1)) +
theme_tufte(base_size = 11, base_family = "Helvetica") +
theme(axis.text = element_text(color = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.7),
legend.key.height = unit(0.8,"line"),
legend.key.width = unit(0.5, "line")) +
scale_fill_gradientn(name = "Density",
colours = matlab.like(1000)) +
scale_color_brewer(name = "cluster ID",
type = "qual",
palette = "Spectral")
**基於密度的**聚類的經典DBSCAN算法呢? –
這是*完美*,我現在幾乎覺得很蠢! –