scipy.sparse.hstack vstack

First of all, the format is in line with coo_matrix to use sparse for splicing.

hstack :
concatenate the matrix by column

from scipy.sparse import coo_matrix, hstack,vstack
A = coo_matrix([[1, 2], [3, 4]])
print(A)
B = coo_matrix([[5,7], [6,8]])
print(hstack([A,B]))
print(hstack([A,B]).toarray())

output:

 (0, 0) 1
  (0, 1)    2
  (1, 0)    3
  (1, 1)    4
  (0, 0)    1
  (0, 1)    2
  (1, 0)    3
  (1, 1)    4
  (0, 2)    5
  (0, 3)    7
  (1, 2)    6
  (1, 3)    8
[[1 2 5 7]
 [3 4 6 8]]

vstack splices
the matrix according to the row, the corresponding number of columns must be equal

from scipy.sparse import coo_matrix, hstack,vstack
A = coo_matrix([[1, 2], [2, 4]])
print(A)
B = coo_matrix([[5,7], [6,8]])
print(vstack([A,B]))
print(vstack([A,B]).toarray())

output:

(0, 0)  1
  (0, 1)    2
  (1, 0)    2
  (1, 1)    4
  (0, 0)    1
  (0, 1)    2
  (1, 0)    2
  (1, 1)    4
  (2, 0)    5
  (2, 1)    7
  (3, 0)    6
  (3, 1)    8
[[1 2]
 [2 4]
 [5 7]
 [6 8]]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325653740&siteId=291194637