python中numpy.eye()函数的学习

                      **python中numpy.eye()函数的学习**

numpy.eye(N, M=None, k=0, dtype=<class ‘float’>, order=‘C’)
Return a 2-D array with ones on the diagonal and zeros elsewhere. 函数eye()的作用是返回一个对角线diagonal上全是1,而其他位置全为0的一个二维数组(2D-array)。

Parameters:
N : int,Number of rows in the output.

M : int, optional,Number of columns in the output. If None, defaults to N.

k : int, optional ——K为对角元素的索引
Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.
默认的索引为0对应的是the main diagonal, index为正值对应一个upper diagonal, index为复制对应的是lower diagonal.
从后续实验结果来看,对角线的形成是以N、M中较小值为标准生成一个方阵的对角线,当索引index取值为3时则该方阵对角线向右移动3个单位,当索引index取值为-2时则该方针对角线向左移动2个单位,详情可以见下面的截图。

在这里插入图片描述

dtype : data-type, optional, Data-type of the returned array. 指的是返回的数组的类型。

order : {‘C’, ‘F’}, optional
Whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.

New in version 1.14.0.

Returns:
I : ndarray of shape (N,M) ——I为一个N行M列的矩阵,可以指定第K个对角元素处为1.
An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

See also
identity
(almost) equivalent function
diag
diagonal 2-D array from a 1-D array specified by the user.

Examples示例

np.eye(2, dtype=int)
array([[1, 0],
[0, 1]])

np.eye(3, k=1)
array([[ 0., 1., 0.],
[ 0., 0., 1.],
[ 0., 0., 0.]])

在这里插入图片描述在这里插入图片描述在这里插入图片描述

  • 25
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值