scipy.sparse.csr.csr_matrix:Matrix extension

2019-05-31 05:47发布

问题:

I am using sklearn to do machine learning works.Here are my two variables:

>>> matrix
<1397x9576 sparse matrix of type '<type 'numpy.float64'>'
        with 44655 stored elements in Compressed Sparse Row format>

>>> type(density)
<type 'list'>
>>> len(density)
1397

matrix is generated by TfidfVectorizer.fit_transform(). I want to extend the variable matrix by adding variable density as a new column.Is there any way to achieve it?

回答1:

Use scipy hstack to stack the column density with matrix

from scipy.sparse import hstack
new_matrix = hstack([matrix, density])


回答2:

here is the proper way to add another column to the matrix

 from scipy.sparse import hstack
 import numpy as np     
 from scipy.sparse import csr_matrix
 density_2 = np.array(density)
 density_3 = csr_matrix(density_2)
 density_4 = density_3.transpose()
 new_matrix = hstack([matrix, density_4])