+2 votes
in Programming Languages by (14.3k points)
edited by

I want to add a new column 'b' to an existing CSR sparse matrix 'X', but the following code is giving error: ValueError: blocks[0,:] has incompatible row dimensions. Got blocks[0,1].shape[0] == 1

X = csr_matrix((cellValue, (row, col)), shape=(a.shape[0], a.shape[1]), dtype=np.int8)

>>> X.toarray()

array([[0, 0, 0, 0, 1],

       [0, 2, 0, 0, 0],

       [0, 0, 0, 3, 0],

       [4, 0, 0, 0, 0],

       [0, 0, 5, 0, 6],

       [0, 0, 7, 0, 0],

       [0, 8, 0, 9, 0]], dtype=int8)

#add new column

b=np.array([1,2,3,4,5,6,7])

X = hstack((X,b), format='csr')

1 Answer

0 votes
by (24.7k points)

Your matrix 'X' has 7 rows, but 'b' has just 1 row. Therefore, it's giving incompatible dimension error. You need to reshape the variable 'b' to use hstack(). Also, the output will be a COO matrix instead of CSR matrix. You need to convert COO to CSR matrix.

Try the following code to add 'b' as a new column to 'X'.

>>> X = hstack((X,np.reshape(b,(len(b),1))))
>>> X
<7x6 sparse matrix of type '<class 'numpy.int32'>'
        with 16 stored elements in COOrdinate format>
>>> type(X)
<class 'scipy.sparse.coo.coo_matrix'>
>>> X=csr_matrix(X)
>>> type(X)
<class 'scipy.sparse.csr.csr_matrix'>
>>> X.toarray()
array([[0, 0, 0, 0, 1, 1],
       [0, 2, 0, 0, 0, 2],
       [0, 0, 0, 3, 0, 3],
       [4, 0, 0, 0, 0, 4],
       [0, 0, 5, 0, 6, 5],
       [0, 0, 7, 0, 0, 6],
       [0, 8, 0, 9, 0, 7]], dtype=int32)

...