Create A Sparse Matrix
Goal¶
This post aims to create a sparse matrix in python using following modules:
- Numpy
- Scipy
Reference:
- Scipy Document
- Chris Albon's blog (I look at his post's title and wrote my own contents to deepen my understanding about the topic.)
Library¶
In [8]:
import numpy as np
import scipy.sparse
Create a sparse matrix using csr_matrix¶
CSR stands for "Compressed Sparse Row" matrix
In [9]:
nrow = 10000
ncol = 10000
# CSR stands for "Compressed Sparse Row" matrix
arr_sparse = scipy.sparse.csr_matrix((nrow, ncol))
arr_sparse
Out[9]:
Modify elements¶
In [13]:
arr_sparse[100, 1000] = 10.
arr_sparse
Out[13]:
Math Opeartion¶
In [16]:
# Addition is not supported
arr_sparse + 1
In [19]:
arr_sparse - 1
In [17]:
arr_sparse * 2
Out[17]:
In [18]:
arr_sparse / 2
Out[18]:
Min & Max¶
In [26]:
arr_sparse.min()
Out[26]:
In [27]:
arr_sparse.max()
Out[27]:
Comments
Comments powered by Disqus