基于点的中心创建多边形区域 我对R很陌生,我想创建一个函数,它需要几个点,找到这些点的中心(像一些质心),从这些点画出线,将点组分开,中间点的中心。类似于制作饼片:我们从中间分割饼,得到等量的部分
我用于查找中心和绘图本身的代码如下:基于点的中心创建多边形区域 我对R很陌生,我想创建一个函数,它需要几个点,找到这些点的中心(像一些质心),从这些点画出线,将点组分开,中间点的中心。类似于制作饼片:我们从中间分割饼,得到等量的部分,r,function,plot,polygon,k-means,R,Function,Plot,Polygon,K Means,我用于查找中心和绘图本身的代码如下: distance <- function(points1, points2) { distanceMatrix <- matrix(NA, nrow=dim(points1)[1], ncol=dim(points2)[1]) for(i in 1:nrow(points2)) { distanceMatrix[,i] <- sqrt(rowSums(t(t(points1)-points2[i,])^2)) } d
distance <- function(points1, points2) {
distanceMatrix <- matrix(NA, nrow=dim(points1)[1], ncol=dim(points2)[1])
for(i in 1:nrow(points2)) {
distanceMatrix[,i] <- sqrt(rowSums(t(t(points1)-points2[i,])^2))
}
distanceMatrix
}
find_cluster <- function(x, centers, distFun, nItter=10) {
clusterHistory <- vector(nItter, mode="list")
centerHistory <- vector(nItter, mode="list")
for(i in 1:nItter) {
distsToCenters <- distFun(x, centers)
clusters <- apply(distsToCenters, 1, which.min)
centers <- apply(x, 2, tapply, clusters, mean)
# Saving history
clusterHistory[[i]] <- clusters
centerHistory[[i]] <- centers
}
list(clusters=clusterHistory, centers=centerHistory)
}
a3=as.matrix(test)
centers <- a3[sample(nrow(a3), 5),]
theResult <- find_cluster(a3, centers, myEuclid, 10)
因此,函数应该执行以下操作:
您可以这样做(
n
是您想要的集群数量)
<代码> dAT,但是否有可能将黄金线移动到图的窗口的末端,以便每个黄金线从中间的蓝色点移动到每个轴?
plot(a3, col=theResult$clusters[[i]],
main=paste("itteration:", i), xlab="x", ylab="y")
points(theResult$centers[[i]],
cex=1, pch=19, col=1:nrow(theResult$centers[[i]]))
dat <- read.table(file="test.txt", header=T)
separateClusts <- function(n, dat) {
## Cartesian to polar (is there a function for this?)
cart2pol <- function(x, y, deg = FALSE) {
r <- sqrt(x^2 + y^2)
theta <- atan(y / x)
theta[x < 0] <- theta[x < 0] + pi
theta[x >= 0 & y < 0] <- theta[x >= 0 & y < 0] + 2*pi
if (deg) theta <- theta * 180/pi
out <- cbind(r, theta)
names(out) <- c("r", "theta")
return( out )
}
## Get clusters
clusts <- kmeans(dat, n)
centers <- clusts$centers
## Center of mass of clusters
com <- matrix(colMeans(centers), ncol=2)
## Order them
cent <- t(t(centers) - c(com)) # center
pol <- cart2pol(cent[,1], cent[,2])
ord <- sort(pol[,2], index=T)$ix
ordered <- as.data.frame(centers[ord, ])
## Get midpoints
mids <- with(ordered, {
data.frame(
xmid=c(x[-1] - x[-length(x)], x[1]-x[length(x)])/2 + x,
ymid=c(y[-1] - y[-length(y)], y[1]-y[length(y)])/2 + y
)
})
## Plot
plot(dat, col=clusts$cluster)
points(com, col="blue", pch=16, cex=2)
points(centers, col="red", pch=16, cex=2)
points(mids, col="orange", pch=16, cex=2)
## Draw line segments
ms <- (tmp <- t(t(mids) - c(com)))[,2] / tmp[,1]
for (i in 1:nrow(mids))
segments(com[,1], com[,2],
com[,1] + (s <- sign(mids$x[i]-com[,1]))*5,
com[,2] + s*ms[i]*5, col="orange", lwd=2)
}
separateClusts(5, dat)