我是R的新手,我想创建一个函数,它会占用几个点,找到这些点的中心(就像物质的中心),并从这些点画出分隔组的线点,中心位于点的中间。类似于制作馅饼:从中心我们分割馅饼,以获得相同数量的零件。
我用于查找中心的代码和情节本身如下:
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)
情节:
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]]))
所以函数应该这样做:
答案 0 :(得分:2)
您可以执行以下操作(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)
是您想要的群集数量)
<log level="full">
<property name="ROOT" expression="$ctx:resultOM"/>
<property name="resultOM.test" xmlns:ns="http://ws.apache.org/ns/synapse" expression="$ctx:resultOM//ns:test1"/>
</log>
红点是聚类中心,橙点是连续中心之间的中点。中心的顺序是通过将它们转换为极坐标并使用角度来确定的。