Reputation: 191
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?
Upvotes: 1
Views: 412
Reputation: 191
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])
Upvotes: 1
Reputation: 36599
Use scipy hstack to stack the column density with matrix
from scipy.sparse import hstack
new_matrix = hstack([matrix, density])
Upvotes: 1