当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python SciPy sparse.eye用法及代码示例

本文简要介绍 python 语言中 scipy.sparse.eye 的用法。

用法:

scipy.sparse.eye(m, n=None, k=0, dtype=<class 'float'>, format=None)#

对角线上的稀疏矩阵

返回一个稀疏 (m x n) 矩阵,其中第 k 个对角线全为 1,其他全为 0。

参数

m int

矩阵中的行数。

n 整数,可选

列数。默认值:米。

k 整数,可选

放置的对角线。默认值:0(主对角线)。

dtype dtype,可选

矩阵的数据类型。

format str,可选

结果的稀疏格式,例如format=”csr”等。

例子

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

相关用法


注:本文由纯净天空筛选整理自scipy.org大神的英文原创作品 scipy.sparse.eye。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。