[Python] numpy.allclose() on SciPy sparse matrix

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    def simrank_sparse(A,c,maxiter,eps=1e-4):

    if not sp.issparse(A):
    raise Exception("Input is not a sparse matrix ")

    n=sp.csr_matrix.get_shape(A)[0]
    Q=misc.get_column_normalized_matrix(A)
    sim=sp.eye(n)
    I=sp.eye(n)
    sim_prev=sp.csr_matrix(sim)

    for t in range(maxiter):

    if sc.allclose(sim,sim_prev,atol=eps):
    break
    sim_prev=sc.copy(sim)
    sim=c*(Q.T*sim_prev*Q)+(1-c)*I

    print("Converge after %d iterations (eps=%f)." % (t, eps))
    return sim


    I am using sparse matrices but the numpy.allclose() function is giving errors as it only takes numpy arrays as input. I don't want to convert the sparse matrices to arrays and back to sparse matrices again, as it will be inefficient. Is there another way to check two sparse matrices for allclose()?

    Continue reading...

Compartilhe esta Página