Search code examples
pythonpython-2.7scipysparse-matrix

Value Error:Setting an array element with sequence


def get_column_normalized_matrix(A):
    d=sp.csr_matrix.get_shape(A)[0]           
    Q=mat.zeros((d,d))
    V=mat.zeros((1,d))
    sp.csr_matrix.sum(A,axis=0,dtype='int',out=V)
    for i in range(0,d):
        if V[0,i]!=0:
            Q[:,i]=sc.divide(A[:,i],V[0,i])
    return Q

Input A is an adjacency matrix of sparse format.I am getting the above as error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in get_column_normalized_matrix
ValueError: setting an array element with a sequence.

Solution

  • The problem you have is that you are trying to assign a sparse matrix into a dense one. This is not done automatically. It is rather simple to fix, though, by turning the sparse matrix into a dense one, using .todense():

    import scipy.sparse as sp
    import numpy.matlib as mat
    import scipy as sc
    
    def get_column_normalized_matrix(A):
        d=sp.csr_matrix.get_shape(A)[0]
        Q=mat.zeros((d,d))
        V=mat.zeros((1,d))
        sp.csr_matrix.sum(A,axis=0,dtype='int',out=V)
        for i in range(0,d):
            if V[0,i]!=0:
                # Explicitly turn the sparse matrix into a dense one:
                Q[:,i]=sc.divide(A[:,i],V[0,i]).todense() 
        return Q
    

    If you instead want the output to be sparse, then you have to ensure that your output matrix Q is sparse to begin with. That can be achieved as follows:

    def get_column_normalized_matrix(A):
        d=sp.csr_matrix.get_shape(A)[0]
        Q=sp.csr_matrix(A) # Create sparse output matrix
        V=mat.zeros((1,d))
        sp.csr_matrix.sum(A,axis=0,dtype='int',out=V)
        for i in range(0,d):
            if V[0,i]!=0:
                # Update sparse matrix
                Q[:,i]=sc.divide(A[:,i],V[0,i]) 
        return Q
    

    As can be seen, Q is created as a copy of A. This makes the same element being non zero in both matrices, which ensures efficient updating, since no new elements will be added.