I have a matrix, $W$, which is 500,000 x 500,000 that I need to find to optimize over the following:

$W* = argmin _{W}|| AW - B||_F^2 + \lambda || W||_1^1$

Using an iterative methods, such as gradient descent. On my local machine I can't just initialize W randomly, instead I need to create a sparse matrix and then initialize some elements. Note that W* is sparse. How to do that?


1 Answer 1


From the scipy documentation

from scipy.sparse import rand
matrix = rand(500000, 500000, density=0.25, format="csr", random_state=42)

You can use the parameter density to choose how dense you want your matrix:

density equal to one means a full matrix, density of 0 means a matrix with no non-zero items.

  • $\begingroup$ wouldn't that effect the optimization performance though? $\endgroup$
    – rando
    Nov 14, 2020 at 0:42
  • $\begingroup$ In which sense? Did I answer the question on how "to create a sparse matrix and then initialize some elements"? Have you tried it? $\endgroup$
    – Ale
    Nov 14, 2020 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.