Concatenate sparse matrices in Python using SciPy/Numpy Concatenate sparse matrices in Python using SciPy/Numpy python python

Concatenate sparse matrices in Python using SciPy/Numpy


You can use the scipy.sparse.hstack to concatenate sparse matrices with the same number of rows (horizontal concatenation):

from scipy.sparse import hstackhstack((X, X2))

Similarly, you can use scipy.sparse.vstack to concatenate sparse matrices with the same number of columns (vertical concatenation).

Using numpy.hstack or numpy.vstack will create an array with two sparse matrix objects.