scipy.sparse的稀疏矩阵存储:

import numpy as np
import scipy.sparse as sparse

#coo_matrix: 座标格式 (即 IJV, 三维格式)
#csc_matrix: 压缩列格式
#csr_matrix: 压缩行格式

# Convert this matrix to COOrdinate format.
A = lil.tocoo()
for i,j,d in zip(A.row, A.col, A.data):
    a[i,j] = d

# Return a dense matrix representation of this matrix.
mtx = sparse.coo_matrix((3, 4), dtype=np.int8)
mtx.todense()

# csr to coo
L = L.tocoo()

coo_matrix()

# Constructing a matrix using ijv format
row  = np.array([0, 3, 1, 0])
col  = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
       [0, 7, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 5]])

isspmatrix_coo()

from scipy.sparse import coo_matrix, isspmatrix_coo
isspmatrix_coo(coo_matrix([[5]]))
True

scipy.sparse.diags

# Construct a sparse matrix from diagonals.
# 从对角线构造一个稀疏矩阵

>>> from scipy.sparse import diags
>>> diagonals = [[1, 2, 3, 4], [1, 2, 3], [1, 2]]

# scipy.sparse.diags(diagonals, offsets=0)
>>> diags(diagonals, [0, -1, 2]).toarray()
array([[1, 0, 1, 0],
       [1, 2, 0, 2],
       [0, 2, 3, 0],
       [0, 0, 3, 4]])

>>> diags([1, 2, 3], 1).toarray()
array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  2.,  0.],
       [ 0.,  0.,  0.,  3.],
       [ 0.,  0.,  0.,  0.]])

scipy.sparse.spdiags()

# Return a sparse matrix from diagonals.
from scipy.sparse import spdiags
data = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
diags = np.array([0, -1, 2])
spdiags(data, diags, 4, 4).toarray()
array([[1, 0, 3, 0],
       [1, 2, 0, 4],
       [0, 2, 3, 0],
       [0, 0, 3, 4]])

求特征值eigenvalues

from numpy import linalg as LA

>>> w, v = LA.eig(np.diag((1, 2, 3)))
>>> w; v
array([1., 2., 3.])
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

求解线性方程组

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy arrays
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])

#Passing the values to the solve function
x = linalg.solve(a, b)

#printing the result array
print(x)
[2, -2, 9]

特征值和特征向量

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
A = np.array([[1,2],[3,4]])

#Passing the values to the eig function
l, v = linalg.eig(A)

#printing the result for eigen values
print(l)
[-0.37228132+0.j  5.37228132+0.j]

#printing the result for eigen vectors
print(v)
[[-0.82456484 -0.41597356]
 [ 0.56576746 -0.90937671]]

scipy.sparse.rand

# 生成随机的稀疏矩阵
>>> from scipy.sparse import rand
>>> matrix = rand(3, 4, density=0.25, format="csr", random_state=42)
>>> matrix
<3x4 sparse matrix of type '<class 'numpy.float64'>'
   with 3 stored elements in Compressed Sparse Row format>
>>> matrix.todense()
matrix([[0.05641158, 0.        , 0.        , 0.65088847],
        [0.        , 0.        , 0.        , 0.14286682],
        [0.        , 0.        , 0.        , 0.        ]])

scipy.sparse.csr_matrix

>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> csr_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)


>>> row = np.array([0, 0, 1, 2, 2, 2])
>>> col = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])

scipy.sparse.identity

# 单位矩阵
>>> from scipy.sparse import identity
>>> identity(3).toarray()
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

scipy.sparse.linalg.eigsh

# 求特征矩阵
# Find k eigenvalues and eigenvectors of the real symmetric square matrix or complex hermitian matrix A.

>>> from scipy.sparse.linalg import eigsh
>>> identity = np.eye(13)
>>> eigenvalues, eigenvectors = eigsh(identity, k=6)
>>> eigenvalues
array([1., 1., 1., 1., 1., 1.])
>>> eigenvectors.shape
(13, 6)