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