Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the byte size of a scipy.sparse matrix?

Is it possible to determine the byte size of a scipy.sparse matrix? In NumPy you can determine the size of an array by doing the following:

import numpy as np

print(np.zeros((100, 100, 100).nbytes)
8000000
like image 571
ebressert Avatar asked Jun 23 '12 21:06

ebressert


People also ask

What is the size of sparse matrix?

Sparse matrix is a matrix which contains very few non-zero elements. When a sparse matrix is represented with a 2-dimensional array, we waste a lot of space to represent that matrix. For example, consider a matrix of size 100 X 100 containing only 10 non-zero elements.

How do you read a sparse matrix in python?

One way to visualize sparse matrix is to use 2d plot. Python's matplotlib has a special function called Spy for visualizing sparse matrix. Spy is very similar to matplotlib's imshow, which is great for plotting a matrix or an array as an image. imshow works with dense matrix, while Spy works with sparse matrix.

What is a SciPy sparse matrix?

Python's SciPy provides tools for creating sparse matrices using multiple data structures, as well as tools for converting a dense matrix to a sparse matrix. The sparse matrix representation outputs the row-column tuple where the matrix contains non-zero values along with those values.

How do you read a sparse matrix?

Sparse Matrix/Sparse Array: The number of zero-valued elements divided by the total number of elements (e.g., m × n for an m × n matrix) is called the sparsity of the matrix (which is equal to 1 minus the density of the matrix). Using those definitions, a matrix will be sparse when its sparsity is greater than 0.5.


1 Answers

A sparse matrix is constructed from regular numpy arrays, so you can get the byte count for any of these just as you would a regular array.

If you just want the number of bytes of the array elements:

>>> from scipy.sparse import csr_matrix >>> a = csr_matrix(np.arange(12).reshape((4,3))) >>> a.data.nbytes 88 

If you want the byte counts of all arrays required to build the sparse matrix, then I think you want:

>>> print a.data.nbytes + a.indptr.nbytes + a.indices.nbytes 152 
like image 148
user545424 Avatar answered Sep 24 '22 04:09

user545424