Skip to content Skip to sidebar Skip to footer

`np.concatenate` A Numpy Array With A Sparse Matrix

A dataset contains numerical and categorial variables, and I split then into two parts: cont_data = data[cont_variables].values disc_data = data[disc_variables].values Then I use

Solution 1:

Sparse matrices are not subclasses of numpy arrays; so numpy methods often don't work. Use sparse functions instead, such as sparse.vstack and sparse.hstack. But all inputs then have to be sparse.

Or make the sparse matrix dense first, with .toarray(), and use np.concatenate.

Do you want the result to sparse or dense?

In [32]: sparse.vstack((sparse.csr_matrix(np.arange(10)),sparse.csr_matrix(np.on
    ...: es((3,10)))))
Out[32]: 
<4x10 sparse matrix of type '<class 'numpy.float64'>'
    with 39 stored elements in Compressed Sparse Row format>
In [33]: np.concatenate((sparse.csr_matrix(np.arange(10)).A,np.ones((3,10))))
Out[33]: 
array([[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

Post a Comment for "`np.concatenate` A Numpy Array With A Sparse Matrix"