Python scipy.sparse.csr.csr_矩阵:矩阵扩展
我正在使用sklearn进行机器学习工作。以下是我的两个变量:Python scipy.sparse.csr.csr_矩阵:矩阵扩展,python,machine-learning,scipy,scikit-learn,Python,Machine Learning,Scipy,Scikit Learn,我正在使用sklearn进行机器学习工作。以下是我的两个变量: >>> matrix <1397x9576 sparse matrix of type '<type 'numpy.float64'>' with 44655 stored elements in Compressed Sparse Row format> >>> type(density) <type 'list'> >>>
>>> 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
>>矩阵
>>>类型(密度)
>>>len(密度)
1397
矩阵
由TfidfVectorizer.fit\u transform()
生成。我想通过添加变量density
作为新列来扩展变量matrix
。有什么方法可以实现吗?用于将列密度与matrix堆叠在一起
from scipy.sparse import hstack
new_matrix = hstack([matrix, density])
下面是向矩阵中添加另一列的正确方法
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])
@Vivek Kumar添加列时需要转置方法。是否从传递给
TfidfVectorizer
的同一数据集计算density
?@MaxU是。density和矩阵都是从同一数据集生成的。