Post A6qibQNuJGbC6lZ8ka by guillaume@aleph.land
(DIR) More posts by guillaume@aleph.land
(DIR) Post #A6qibOM3r9vxoSgday by guillaume@aleph.land
2021-05-02T13:11:40Z
0 likes, 0 repeats
A trick to form large sparse matrices quickly in #python / #numpy. It should maybe have been obvious, but it took me a while to find it.You could do >>> m = scipy.sparse.lil_matrix(n, n)and then assign values one by one like>>> m[i, j] = vbut this is still kinda slow.It's better to do:>>> rows = [...] # list/array of row indices>>> cols = [...] # list/array of column indices>>> vals = [...] # list/array of values>>> m = scipy.sparse.coo_matrix((vals, (rows, cols)), shape=(n, n))
(DIR) Post #A6qibQNuJGbC6lZ8ka by guillaume@aleph.land
2021-05-02T13:13:08Z
0 likes, 0 repeats
On a 60000×60000 sparse matrix, my code went from about 4.5 to 1.5 seconds to build it like that.