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


Python numpy.eye()用法及代码示例


numpy.eye(R,C = None,k = 0,dtype = type):返回对角线为1且其他位置为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 Programming illustrating 
# numpy.eye method 
  
import numpy as geek 
  
# 2x2 matrix with 1's on main diagnol 
b = geek.eye(2, dtype = float) 
print("Matrix b : \n", b) 
  
# matrix with R=4 C=5 and 1 on diagnol 
# below main diagnol 
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.]]

注意:
这些代码无法在online-ID上运行。请在您的系统上运行它们以探索工作原理



相关用法


注:本文由纯净天空筛选整理自 numpy.eye() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。