+3 votes
in Programming Languages by (56.5k points)

I am trying to concatenate two CSR matrices using the concatenate() function of Numpy, but the code gives ValueError.

data = np.concatenate([pos, neg], axis=0)

pos and neg are CSR matrices. 

Here are the details of the error:

Traceback (most recent call last):

  File "test_uci.py", line 211, in <module>

    res = experiment_synthetic(random_state=100)

  File "test_uci.py", line 130, in experiment_synthetic

    data = np.concatenate([pos, neg], axis=0)

  File "<__array_function__ internals>", line 5, in concatenate

ValueError: zero-dimensional arrays cannot be concatenated

How can I fix the error?

1 Answer

+2 votes
by (73.8k points)
selected by
 
Best answer

I do not think you can use the concatenate() function of Numpy to concatenate CSR matrices. You need to use the vstack() or hstack() function of the sparse module of scipy.

Since you are using axis=0, you can use the following code to fix the error.

from scipy import sparse

data = sparse.vstack((pos, neg), format='csr')  # merge the data


...