我实际上想要获得k = 2:63的db索引。当我在循环中运行kmeans时会发生此问题,否则它不会显示错误。
这是我的主要功能
for(i in 2:63) {
print(i)
# kmeans++ algorithm
r_cluster_result = kmpp(r_cluster_mat,i)
r_cluster_data = data.frame(r_cluster_result$cluster)
db = index.DB(x=r_cluster,cl=r_cluster_result$cluster,d=NULL,centrotypes="centroids",p=2,q=1)
db_values = c(db_values,db$DB)
}
这是我的kmeans ++功能代码
kmpp <- function(X, k) {
n <- nrow(X)
C <- numeric(k)
C[1] <- sample(1:n, 1)
for (i in 2:k) {
dm <- distmat(X, X[C, ])
pr <- apply(dm, 1, min);
pr[C] <- 0
C[i] <- sample(1:n, 1, prob = pr)
}
kmeans(X, X[C, ])
}
错误消息
Error in sample(1:n, 1, prob = pr) : NA in probability vector
In addition: Warning message:
In sqrt(XX + YY - 2 * XY) : NaNs produced
dput(X)是