Re: [問題] 分群和找位置相關問題

作者: andrew43 (討厭有好心推文後刪文者)   2018-11-02 21:40:45
原po有來信指出大概有多少資料。
我試了 dbscan,請見下例。
library(data.table)
library(dbscan)
library(ggplot2)
# 產生理想 1500 群的 20000 筆假資料
N <- 20000
d <-
data.table(
x1 = sample(seq(1:50), size = N, T) + rnorm(N, 0, 0.05),
x2 = sample(seq(1:30), size = N, T) + rnorm(N, 0, 0.05)
)
# dbscan
# eps 參數要看情況抓一下,應該不會很難抓(因為你幾乎知道實際有幾群)
# 可用的 eps 值和 0.5 可能差很多
res <- dbscan(d[, x1:x2], eps = 0.5, minPts = 1)
print(length(table(res$cluster))) # 確實抓到 50*30=1500 群
d[, g := as.character(res$cluster)]
# 取中心和每群個數
dc <- d[, list(x1c = mean(x1), x2c = mean(x2), N = .N), by = g]
ggplot(d, aes(x = x1, y = x2)) +
geom_point(size = 0.2) +
ggtitle("raw data")
ggplot(dc, aes(x = x1c, y = x2c)) +
geom_point(alpha = 0.2, size = 2) +
ggtitle("average per cluster")
ggplot(dc, aes(x = x1c, y = x2c)) +
geom_text(aes(x = x1c, y = x2c, label = N), size = 2) +
ggtitle("number of raw data per cluster")
ggplot(dc, aes(x = x1c, y = x2c)) +
geom_text(aes(x = x1c, y = x2c, label = g), size = 2) +
ggtitle("cluster ID")
※ 引述《w32123 (Ru)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 入門(寫過其他程式,只是對語法不熟悉)
: [問題敘述]:
: 各位大神大家好~
: http://i.imgur.com/tbduH5L.jpg
: 這個圖中的每一小坨是植物的位置
: 是先對每個點的光譜值做kmeans分四群後找出來的
: 四個頂點是邊界範圍
: 我想找出散布在圖中每一坨的中心點當作植物中心點
: 也想知道中心點的位置和個數
: Hclust我只放入了x,y座標讓他們分群可是感覺分不太出來QQ結果如圖
: http://i.imgur.com/mrC8b6c.jpg
: 想請問有除了分群以外的方法嗎?
: 另外想讓R自動決定分群數 有嘗試過elbow method
: 但是都會出現Error: cannot allocate vector of size 91.4 Gb
: 不知道自動決定分群數有沒有比較推薦的方法呢
: 先謝過各位大神了~
: 現有每個點的資料格式如圖
: 最左邊是序號 band1~4是光譜值 x,y是x,y座標
: http://i.imgur.com/UUPYeCU.jpg
: [程式範例]:
: plant.cluster <- hclust(dist(remain.plant.add[,1:2]),method = "ward.D2")
: png("hclust.png",res = 200,width = 2259,height = 1549)
: plot(plant.cluster)
: dev.off()
: #elbow method
: elbow.k <- function(mydata){
: dist.obj <- dist(mydata)
: hclust.obj <- hclust(dist.obj)
: css.obj <- css.hclust(dist.obj,hclust.obj)
: elbow.obj <- elbow.batch(css.obj)
: k <- elbow.obj$k
: return(k)
: }
: elbow.k(NDVI)
: #Error: cannot allocate vector of size 91.4 Gb
: [環境敘述]:
: R version 3.5.0 (2018-04-23)
: Platform: x86_64-w64-mingw32/x64 (64-bit)
: Running under: Windows >= 8 x64 (build 9200)
: Matrix products: default
: locale:
: [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
: [2] LC_CTYPE=Chinese (Traditional)_Taiwan.950
: [3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
: [4] LC_NUMERIC=C
: [5] LC_TIME=Chinese (Traditional)_Taiwan.950
: attached base packages:
: [1] stats graphics grDevices utils datasets methods
: [7] base
: other attached packages:
: [1] ggplot2_2.2.1 NbClust_3.0 rgdal_1.3-3 sp_1.3-1
: loaded via a namespace (and not attached):
: [1] Rcpp_0.12.18 lattice_0.20-35 crayon_1.3.4
: [4] grid_3.5.0 plyr_1.8.4 gtable_0.2.0
: [7] scales_1.0.0 pillar_1.3.0 rlang_0.2.2
: [10] lazyeval_0.2.1 rstudioapi_0.7 labeling_0.3
: [13] tools_3.5.0 munsell_0.5.0 compiler_3.5.0
: [16] colorspace_1.3-2 tibble_1.4.2
: [關鍵字]:
: 分群、中心點、位置
作者: obarisk (OSWALT)   2018-11-02 21:53:00
wush表示:為什麼不試試 supc XDD
作者: andrew43 (討厭有好心推文後刪文者)   2018-11-02 22:07:00
因為他先寫dbscan所以我先試這個... XDoh我懂惹。XD
作者: Wush978 (拒看低質媒體)   2018-11-03 00:06:00
沒差吧... XDDDDD 能抓老鼠的就是好喵喵
作者: w32123 (Ru)   2018-11-03 01:41:00
謝謝您特地回文~dbscan有試出來了,只是eps的部分還在嘗試要如何設定會比較好,非常感謝您!謝謝大家熱心回應~

Links booklink

Contact Us: admin [ a t ] ucptt.com