scipy 个稀疏矩阵的类型提示
Type hinting for scipy sparse matrices
如何输入提示scipy稀疏矩阵,如CSR, CSC, LIL
等?以下是我一直在做的,但感觉不对:
def foo(mat: scipy.sparse.csr.csr_matrix):
# Do whatever
如果我们的函数可以接受多种类型的 scipy 稀疏矩阵(即其中任何一种),我们该怎么办?
所有csr, csc, lil
都是scipy.sparse.base.spmatrix
的类型:
from scipy import sparse
c1 = sparse.lil.lil_matrix
c2 = sparse.csr.csr_matrix
c3 = sparse.csc.csc_matrix
print(c1.__bases__[0])
print(c2.__base__.__base__.__base__)
print(c3.__base__.__base__.__base__)
输出:
<class 'scipy.sparse.base.spmatrix'>
<class 'scipy.sparse.base.spmatrix'>
<class 'scipy.sparse.base.spmatrix'>
因此您可以选择:
def foo(mat: scipy.sparse.base.spmatrix):
# Do whatever
如何输入提示scipy稀疏矩阵,如CSR, CSC, LIL
等?以下是我一直在做的,但感觉不对:
def foo(mat: scipy.sparse.csr.csr_matrix):
# Do whatever
如果我们的函数可以接受多种类型的 scipy 稀疏矩阵(即其中任何一种),我们该怎么办?
所有csr, csc, lil
都是scipy.sparse.base.spmatrix
的类型:
from scipy import sparse
c1 = sparse.lil.lil_matrix
c2 = sparse.csr.csr_matrix
c3 = sparse.csc.csc_matrix
print(c1.__bases__[0])
print(c2.__base__.__base__.__base__)
print(c3.__base__.__base__.__base__)
输出:
<class 'scipy.sparse.base.spmatrix'>
<class 'scipy.sparse.base.spmatrix'>
<class 'scipy.sparse.base.spmatrix'>
因此您可以选择:
def foo(mat: scipy.sparse.base.spmatrix):
# Do whatever