Suppose I have a list or a list of lists (each list with the same size). How do I convert to a sparse vector or sparse matrix, respectively?
Sparse data is data that has mostly unused elements (elements that don't carry any information ). It can be an array like this one: [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0] Sparse Data: is a data set where most of the item values are zero.
from scipy.sparse import csc_matrix. # Creating a 3 * 4 sparse matrix. sparseMatrix = csc_matrix(( 3 , 4 ), dtype = np.int8).toarray() # Print the sparse matrix.
lil_matrix((M, N), [dtype]) to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype='d'. Notes. Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.
In [5]: scipy.sparse.csr_matrix([[1, 2], [3, 0]])
Out[5]:
<2x2 sparse matrix of type '<type 'numpy.int64'>'
with 3 stored elements in Compressed Sparse Row format>
In [6]: scipy.sparse.csr_matrix([1, 2])
Out[6]:
<1x2 sparse matrix of type '<type 'numpy.int64'>'
with 2 stored elements in Compressed Sparse Row format>
scipy.sparse.whatever_matrix_type(your_data_structure)
. It's quite analogous to what you'd do to get a regular array. Note that there is no sparse vector or sparse ndarray class, only sparse matrices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With