`np.concatenate` a numpy array with a sparse matrix

前端 未结 1 719
梦毁少年i
梦毁少年i 2021-01-06 00:25

A dataset contains numerical and categorial variables, and I split then into two parts:

cont_data = data[cont_variables].values
disc_data = data[disc_variabl         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 01:08

    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 ''
        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.]])
    

    0 讨论(0)
提交回复
热议问题