我有两个稀疏矩阵(从sklearn
HashVectorizer
中创建,来自两组特性-每组对应于一个特性)。我想将它们连接起来,以便以后用于集群。但是,我面临一个维数的问题,因为这两个矩阵的行维数不同。在
下面是一个例子:
Xa = [-0.57735027 -0.57735027 0.57735027 -0.57735027 -0.57735027 0.57735027
0.5 0.5 -0.5 0.5 0.5 -0.5 0.5
0.5 -0.5 0.5 -0.5 0.5 0.5 -0.5
0.5 0.5 ]
Xb = [-0.57735027 -0.57735027 0.57735027 -0.57735027 0.57735027 0.57735027
-0.5 0.5 0.5 0.5 -0.5 -0.5 0.5
-0.5 -0.5 -0.5 0.5 0.5 ]
Xa
和{<class 'scipy.sparse.csr.csr_matrix'>
类型。形状是Xa.shape = (6, 1048576) Xb.shape = (5, 1048576)
。我得到的错误是(我现在知道为什么会发生):
尽管稀疏矩阵的维数不规则,但有没有一种方法可以将它们堆叠起来?也许加些衬垫?在
我看过这些帖子:
你可以用一个空的稀疏矩阵来填充它。在
你想要水平堆叠它,所以你需要填充较小的矩阵,这样它就拥有与大矩阵相同的行数。为此,将其垂直堆叠成
(difference in number of rows, number of columns of original matrix)
的矩阵。在像这样:
结果是:
^{pr2}$相关问题 更多 >
编程相关推荐