简体   繁体   English

稀疏地将一个稀疏矩阵的所有行附加到另一行

[英]scipy append all rows of one sparse matrix to another

I have a numpy matrix and want to append another matrix to that. 我有一个numpy矩阵,并想要附加另一个矩阵。

The two matrices have the shapes: 这两个矩阵具有以下形状:

m1.shape = (2777, 5902)  m2.shape = (695, 5902)

I want to append m2 to m1 so that the new matrix is of shape: 我想将m2附加到m1以便新矩阵具有形状:

m_new.shape = (3472, 5902)

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). 当我使用numpy.append或numpy.concatenate时,我得到一个新数组,其中包含两个矩阵和形状(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. 其他信息:两者都是稀疏矩阵。

EDIT: m1 looks like 编辑: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 looks like: 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

Type of the matrices is: 矩阵类型为:

<class 'scipy.sparse.csr.csr_matrix'> <class 'scipy.sparse.csr.csr_matrix'>

SOLVED: 解决了:

m_new = scipy.sparse.vstack((m1, m2))

did the trick 做到了

Thanks for your help. 谢谢你的帮助。

You can use numpy.vstack in your case (or numpy.hstack , when matrices shapes are (x,y) and (x,z)) 您可以使用numpy.vstack (或numpy.hstack ,当矩阵形状为(x,y)和(x,z)时)

Example: 例:

a = np.zeros((3,7))
b = np.zeros((46,7))
c = np.vstack((a,b))
print c.shape
#(49,7)

You can use concatenate ( http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.concatenate.html ): 您可以使用concatenatehttp://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.concatenate.html ):

m_new = np.concatenate((m1, m2))

For example, 例如,

a = np.zeros((5, 3))
b = np.zeros((4, 3))
print np.concatenate((a, b)).shape
# (9, 3)

That still works if you use matrices instead of arrays: 如果您使用矩阵而不是数组,那仍然可行:

print np.concatenate((np.matrix(a), np.matrix(b))).shape
# (9, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM