0

Have a sparse matrix of shape (10,64) and have a numpy array with shape (10,). I want to concat the the matrices so the new matrix has dimensions (10,65). I have tried this but I keep on getting this error 'ValueError: zero-dimensional arrays cannot be concatenated'

d = np.concatenate((a,np.concatenate(b)[:,None]),axis=1)

EDIT:

Working Solution

from scipy.sparse import csr_matrix
import numpy as np

C=np.vstack((A.A.T,B)).T
D=csr_matrix((C)) # 
swalga
  • 11
  • 3
  • `np.column_stack((a,b)).shape` gives me `(10, 65)` on random arrays, if not you should include an example to show why or may be check [this](https://stackoverflow.com/questions/38498299/how-to-column-stack-a-numpy-array-with-a-scipy-sparse-matrix) – anky Jun 22 '20 at 17:04
  • @anky. I got this error when i used column_stack. ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1 and the array at index 1 has size 10 – swalga Jun 22 '20 at 17:07
  • yeah i missed sparse matrix , edited my comment, does the link help..? – anky Jun 22 '20 at 17:09
  • 1
    Yep that worked, Thanks – swalga Jun 22 '20 at 17:15
  • If you want the result to be a sparse matrix, I'd suggest using one of the `sparse` 'stack' functions. Those get the `coo` attributes of the input arrays, combine them, and make new `coo` matrix from that. `np.concatenate` tries to turn all its inputs into `numpy` arrays, but `np.asarray(sparse_M)` is the wrong way to do that. – hpaulj Jun 22 '20 at 19:00

0 Answers0