Python中的 numpy.eye()
numpy.eye(R, C = None, k = 0, dtype = type <'float'>) : – eye 工具返回一个二维数组,其中对角线为1 ,其他位置为0 。对角线可以是 main、upper 或 lower,具体取决于可选参数k 。正k用于上对角线,负k用于下对角线, 0 k (默认)用于主对角线。
参数 :
R : Number of rows
C : [optional] Number of columns; By default M = N
k : [int, optional, 0 by default]
Diagonal we require; k>0 means diagonal above main diagonal or vice versa.
dtype : [optional, float(by Default)] Data type of returned array.
返回:
array of shape, R x C, an array where all elements
are equal to zero, except for the k-th diagonal,
whose values are equal to one.
例子:
Python
# Python Programming illustrating
# numpy.eye method
import numpy as geek
# 2x2 matrix with 1's on main diagonal
b = geek.eye(2, dtype = float)
print("Matrix b : \n", b)
# matrix with R=4 C=5 and 1 on diagonal
# below main diagonal
a = geek.eye(4, 5, k = -1)
print("\nMatrix a : \n", a)
输出 :
Matrix b :
[[ 1. 0.]
[ 0. 1.]]
Matrix a :
[[ 0. 0. 0. 0. 0.]
[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]]
笔记 :
这些代码不会在在线 ID 上运行。请在您的系统上运行它们以探索工作。