當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。