scipy 将一个稀疏矩阵的所有行附加到另一个 [英] scipy append all rows of one sparse matrix to another
问题描述
我有一个 numpy 矩阵,想在其中附加另一个矩阵.
I have a numpy matrix and want to append another matrix to that.
两个矩阵的形状:
m1.shape = (2777, 5902) m2.shape = (695, 5902)
我想将 m2 附加到 m1 以便新矩阵具有形状:
I want to append m2 to m1 so that the new matrix is of shape:
m_new.shape = (3472, 5902)
当我使用 numpy.append 或 numpy.concatenate 时,我只会得到一个包含两个矩阵和形状 (2,1) 的新数组.
When I use numpy.append or numpy.concatenate I just get a new array with the two matrix in it and the shape (2,1).
你们有没有人知道如何从两个矩阵中得到一个大矩阵?
Any one of you have an Idea how to get one big matrix out of the two?
附加信息:两者都是稀疏矩阵.
Additional info: both are sparse matrices.
m1看起来像
(0, 1660) 0.444122811195
(0, 3562) 0.260868771714
(0, 4743) 0.288149437574
(0, 4985) 0.514889706991
(0, 5215) 0.272163636657
(0, 5721) 0.559006134727
(1, 555) 0.0992498400527
(1, 770) 0.133145289523
(1, 790) 0.0939044698233
(1, 1097) 0.259867567986
(1, 1285) 0.188836288168
(1, 1366) 0.24707459927
(1, 1499) 0.237997843516
(1, 1559) 0.120069347224
(1, 1701) 0.17660176488
(1, 1926) 0.185678520634
(1, 2177) 0.163066377369
(1, 2641) 0.079958199952
(1, 2937) 0.259867567986
(1, 3551) 0.198471489351
(1, 3562) 0.0926197593026
(1, 3593) 0.100537828805
(1, 4122) 0.198471489351
(1, 4538) 0.57162654484
(1, 4827) 0.105808609537
m2 看起来像:
(0, 327) 0.0770581299315
(0, 966) 0.309858753157
(0, 1231) 0.286870892505
(0, 1384) 0.281385698712
(0, 1817) 0.204495931592
(0, 2284) 0.182420951496
(0, 2414) 0.114591086901
(0, 2490) 0.261442040482
(0, 3122) 0.321676138471
(0, 3151) 0.286870892505
(0, 4031) 0.172251612658
(0, 5149) 0.25839783806
(0, 5215) 0.125806303262
(0, 5225) 0.336280781816
(0, 5231) 0.135930403721
(0, 5294) 0.145049459537
(0, 5794) 0.20145172917
(0, 5821) 0.224439589822
(1, 327) 0.191031948626
(1, 1171) 0.62081265022
矩阵类型为:
<class 'scipy.sparse.csr.csr_matrix'> <class 'scipy.sparse.csr.csr_matrix'>
已解决:
m_new = scipy.sparse.vstack((m1, m2))
成功了
感谢您的帮助.
推荐答案
您可以在您的情况下使用 numpy.vstack
(或 numpy.hstack
,当矩阵形状为(x,y) 和 (x,z))
You can use numpy.vstack
in your case (or numpy.hstack
, when matrices shapes are (x,y) and (x,z))
示例:
a = np.zeros((3,7))
b = np.zeros((46,7))
c = np.vstack((a,b))
print c.shape
#(49,7)
这篇关于scipy 将一个稀疏矩阵的所有行附加到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!