To create a sparse matrix, we can use the scipy.sparse
module. Specifically, we can create a sparse matrix of size 100x100 and fill it with 100 random entries using scipy.sparse.random
. Here is the code:
main.py114 chars5 lines
In this example, density
specifies the fraction of non-zero entries in the matrix. You can adjust it to create more or fewer entries.
To create a dense matrix, we can use the numpy
module. We can randomly generate a 100x100 array and then set all but 100 entries to 0. Here is the code:
main.py139 chars4 lines
Finally, we can use the sys.getsizeof
function to determine the sizes of these matrices in bytes:
main.py178 chars7 lines
The exact sizes will depend on your system, but the sparse matrix should be much smaller than the dense matrix, since it only stores non-zero values.
gistlibby LogSnag