Python中numpy.eye()函数

numpy.eye()

Python numpy.eye(R, C =None,k = 0, dtype = type < 'float' >) : eye工具返回一个2-D数组,对角线为1,其他地方为0。

对角线可以是主、上或下,这取决于可选参数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.  

返回值:

形状的数组,R x C,一个所有元素都等于零的数组。都等于零,除了对角线上的第k条。其值等于1。

Example 1:

# 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.]]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程