Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python scipy.sparse.csr.csr_矩阵:矩阵扩展_Python_Machine Learning_Scipy_Scikit Learn - Fatal编程技术网

Python scipy.sparse.csr.csr_矩阵:矩阵扩展

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'> >>>

我正在使用sklearn进行机器学习工作。以下是我的两个变量:

>>> 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和矩阵都是从同一数据集生成的。