Skip to content Skip to sidebar Skip to footer

How To Column_stack A Numpy Array With A Scipy Sparse Matrix?

I have the following matrices: A.toarray() array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0,

Solution 1:

2 issues

  • there isn't a sparse.column_stack
  • you are mixing a sparse matrix and dense array

2 smaller examples:

In [129]: A=sparse.csr_matrix([[1,0,0],[0,1,0]])
In [130]: B=np.array([1,2])

Using np.column_stack gives your error:

In [131]: np.column_stack((A,B))
... 
ValueError: all the input array dimensions exceptfor the concatenation axis must match exactly

But if I first turn A into an array, column_stack does fine:

In [132]: np.column_stack((A.A, B))
Out[132]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]])

the equivalent with concatenate:

In [133]: np.concatenate((A.A, B[:,None]), axis=1)
Out[133]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]])

there is a sparse.hstack. For that I need to turn B into a sparse matrix as well. Transpose works because it is now a matrix (as opposed to a 1d array):

In [134]: sparse.hstack((A,sparse.csr_matrix(B).T))
Out[134]: 
<2x4 sparse matrix of type'<class 'numpy.int32'>'
    with 4 stored elements in COOrdinate format>
In [135]: _.A
Out[135]: 
array([[1, 0, 0, 1],
       [0, 1, 0, 2]], dtype=int32)

Solution 2:

Did you try the following?

C=np.vstack((A.T,B)).T

With sample values:

A = array([[1, 2, 3], [4, 5, 6]])
>>>> A.shape
(2, 3)
B = array([7, 8])
>>> B.shape
(2,)
C=np.vstack((A.T,B)).T
>>> C.shape
(2, 4)

If A is a sparse matrix, and you want to maintain the output as sparse, you could do:

C=np.vstack((A.A.T,B)).T
D=csr_matrix((C))

Post a Comment for "How To Column_stack A Numpy Array With A Scipy Sparse Matrix?"