I\'m trying to build and update a sparse matrix as I read data from file.
The matrix is of size 100000X40000
What is the most efficient way of updating
This answer expands the comment of @behzad.nouri. To increment the values at the "outer product" of your lists of rows and columns indices, just create these as numpy arrays configured for broadcasting. In this case, that means put the rows into a column. For example,
In [59]: a = lil_matrix((4,4), dtype=int)
In [60]: a.A
Out[60]:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
In [61]: rows = np.array([1,3]).reshape(-1, 1)
In [62]: rows
Out[62]:
array([[1],
[3]])
In [63]: cols = np.array([0, 2, 3])
In [64]: a[rows, cols] += np.ones((rows.size, cols.size))
In [65]: a.A
Out[65]:
array([[0, 0, 0, 0],
[1, 0, 1, 1],
[0, 0, 0, 0],
[1, 0, 1, 1]])
In [66]: rows = np.array([0, 1]).reshape(-1,1)
In [67]: cols = np.array([1, 2])
In [68]: a[rows, cols] += np.ones((rows.size, cols.size))
In [69]: a.A
Out[69]:
array([[0, 1, 1, 0],
[1, 1, 2, 1],
[0, 0, 0, 0],
[1, 0, 1, 1]])