首页 文章

kknn中的specClust() - arpack迭代限制增加

提问于
浏览
0

我正在将谱聚类应用于具有4200行和2列的数据集 .

spec <- specClust(df1, centers=7, nn = 7, method = "symmetric")

我有以下错误 .

n .Call("R_igraph_arpack", func, extra, options, env, sym, PACKAGE = "igraph") : 
  At arpack.c:944 : ARPACK error, Maximum number of iterations reached
In addition: Warning message:
In .Call("R_igraph_arpack", func, extra, options, env, sym, PACKAGE = "igraph") :
  At arpack.c:776 :ARPACK solver failed to converge (1001 iterations, 0/7 eigenvectors converged)

我如何增加arpack的迭代,因为这不起作用:

spec <- specClust(df1, centers=7, nn = 7, method = "symmetric",iter.max=301000)

1 回答

  • 0

    深入 specClust... 不会将任何内容传递给 arpack 调用 .

    我认为最简单的方法是复制 specClust 代码添加 maxiter=10000 并在脚本中获取该函数 .

    specCLust2 <- function (data, centers = NULL, nn = 7, method = "symmetric", 
        gmax = NULL, max.iter = 10000, ...) 
    {
        call = match.call()
        if (is.data.frame(data)) 
            data = as.matrix(data)
        da = apply(data, 1, paste, collapse = "#")
        indUnique = which(!duplicated(da))
        indAll = match(da, da[indUnique])
        data2 = data
        data = data[indUnique, ]
        n <- nrow(data)
        data = scale(data, FALSE, TRUE)
        if (is.null(gmax)) {
            if (!is.null(centers)) 
                gmax = centers - 1L
            else gmax = 1L
        }
        test = TRUE
        while (test) {
            DC = mydist(data, nn)
            sif <- rbind(1:n, as.vector(DC[[2]]))
            g <- graph(sif, directed = FALSE)
            g <- decompose(g, min.vertices = 4)
            if (length(g) > 1) {
                if (length(g) >= gmax) 
                    nn = nn + 2
                else test = FALSE
            }
            else test = FALSE
        }
        W <- DC[[1]]
        n <- nrow(data)
        wi <- W[, nn]
        SC <- matrix(1, nrow(W), nn)
        SC[] <- wi[DC[[2]]] * wi
        W = W^2/SC
        alpha = 1/(2 * (nn + 1))
        qua = abs(qnorm(alpha))
        W = W * qua
        W = dnorm(W, sd = 1)
        DC[[1]] = W
        L = Laplacian(DC, nn, method)
        f <- function(x, extra) as.vector(extra %*% x)
        if (is.null(centers)) 
            kmax = 25
        else kmax = max(centers)
    ###
    #add the maxiter parameter to the arpack call, below
    ###    
        U <- arpack(f, extra = L, options = list(n = n, which = "SM", 
            nev = kmax, ncv = 2 * kmax, mode = 1, maxiter=max.iter), sym = TRUE)
        ind <- order(U[[1]])
        U[[2]] = U[[2]][indAll, ind]
        U[[1]] = U[[1]][ind]
        if (is.null(centers)) {
            tmp = which.max(diff(U[[1]])) + 1
            centers = which.min(AUC(U[[1]][1:tmp]))
        }
        if (method == "symmetric") {
            rs = sqrt(rowSums(U[[2]]^2))
            U[[2]] = U[[2]]/rs
        }
        result = kmeans(U[[2]], centers = centers, nstart = 20, ...)
        archeType = getClosest(U[[2]][indAll, ], result$centers)
        result$eigenvalue = U[[1]]
        result$eigenvector = U[[2]]
        result$data = data2
        result$indAll = indAll
        result$indUnique = indUnique
        result$L = L
        result$archetype = archeType
        result$call = call
        class(result) = c("specClust", "kmeans")
        result
    }
    

相关问题