Add column to a sparse matrix

Bonson picture Bonson · Jan 30, 2017 · Viewed 12.8k times · Source

When I execute the following code I get a spares matrix:

import numpy as np
from scipy.sparse import csr_matrix

row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sp = csr_matrix((data, (row, col)), shape=(3, 3))
print(sp)

  (0, 0)        1
  (0, 2)        2
  (1, 2)        3
  (2, 0)        4
  (2, 1)        5
  (2, 2)        6

I want to add another column to this sparse matrix so the output is:

  (0, 0)        1
  (0, 2)        2
  (0, 3)        7
  (1, 2)        3
  (1, 3)        7
  (2, 0)        4
  (2, 1)        5
  (2, 2)        6
  (2, 3)        6

Basically I want to add another column that has the values 7,7,7.

Answer

hpaulj picture hpaulj · Jan 31, 2017

The sparse.hstack used in @Paul Panzer's link is the simplest.

In [760]: sparse.hstack((sp,np.array([7,7,7])[:,None])).A
Out[760]: 
array([[1, 0, 2, 7],
       [0, 0, 3, 7],
       [4, 5, 6, 7]], dtype=int32)

sparse.hstack is not complicated; it just calls bmat([blocks]).

sparse.bmat gets the coo attributes of all the blocks, joins them with the appropriate offself, and builds a new coo_matrix.

In this case it joins

In [771]: print(sp)
  (0, 0)    1
  (0, 2)    2
  (1, 2)    3
  (2, 0)    4
  (2, 1)    5
  (2, 2)    6
In [772]: print(sparse.coo_matrix(np.array([7,7,7])[:,None]))
  (0, 0)    7
  (1, 0)    7
  (2, 0)    7

while changing the column numbers of the last to 3.

In [761]: print(sparse.hstack((sp,np.array([7,7,7])[:,None])))
  (0, 0)    1
  (0, 2)    2
  (1, 2)    3
  (2, 0)    4
  (2, 1)    5
  (2, 2)    6
  (0, 3)    7
  (1, 3)    7
  (2, 3)    7