python Sparse matrices 单位矩阵

这篇博客介绍了如何在Python中使用不同的稀疏格式创建单位矩阵,包括bsr_matrix、coo_matrix、csc_matrix、csr_matrix、dia_matrix和dok_matrix。还提供了创建单位矩阵的示例代码,展示如何将它们转换为数组或指定格式。

identity(n[, dtype, format]) Identity matrix in sparse format Returns an identity matrix with shape (n,n) using a given sparse format and dtype.

创建n X n单位矩阵。

format:

bsr_matrix(arg1[, shape, dtype, copy, blocksize]) Block Sparse Row matrix This can be instantiated in several ways: bsr_matrix(D, [blocksize=(R,C)]) where D is a dense matrix or 2-D ndarray.
coo_matrix(arg1[, shape, dtype, copy]) A sparse matrix in COOrdinate format.
csc_matrix(arg1[, shape, dtype, copy]) Compressed Sparse Column matrix This can be instantiated in several ways: csc_matrix(D) with a dense matrix or rank-2 ndarray D csc_matrix(S) with another sparse matrix S (equivalent to S.tocsc()) csc_matrix((M, N), [dtype]) to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype=’d’.
csr_matrix(arg1[, shape, dtype, copy]) Compressed Sparse Row matrix This can be instantiated in several ways: csr_matrix(D) with a dense matrix or rank-2 ndarray D csr_matrix(S) with another sparse matrix S (equivalent to S.tocsr()) csr_matrix((M, N), [dtype]) to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype=’d’.
dia_matrix(arg1[, shape, dtype, copy]) Sparse matrix with DIAgonal storage This can be instantiated in several ways: dia_matrix(D) with a dense matrix dia_matrix(S) with another sparse matrix S (equivalent to S.todia()) dia_matrix((M, N), [dtype]) to construct an empty matrix with shape (M, N), dtype is optional, defaulting to dtype=’d’.
dok_matrix(arg1[, shape, dtype, copy]) Dictionary Of Keys based sparse matrix.
lil_matrix(arg1[, shape, dtype, copy]) Row-based linked list sparse matrix This is a structure for constructing sparse matrices incrementally.
spmatrix([maxprint]) This class provides a base class for all sparse matrices.


======================================================================================

Examples


>>> from scipy.sparse import identity
>>> identity(3).toarray()
array([[ 1.,  0.,  0.],
            [ 0.,  1.,  0.],
            [ 0.,  0.,  1.]])
>>> identity(3, dtype='int8', format='dia')
<3x3 sparse matrix of type '<type 'numpy.int8'>'
        with 3 stored elements (1 diagonals) in DIAgonal format>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值