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


Python numpy.triu()用法及代碼示例


numpy.triu(a,k = 0):返回三角形上部為w.r.t k的數組的副本
參數:

a : input array
k : [int, optional, 0 by default]
          Diagonal we require; k>0 means diagonal above main diagonal or vice versa.

返回值:

Upper triangle of a, having same shape and data-type as a.
# Python Programming illustrating 
# numpy. triu method 
  
import numpy as geek 
  
# string input 
a = geek.matrix([[1, 21, 30],  
                 [63 ,434, 3],  
                 [54, 54, 56]]) 
  
print("Main Diagonal elements : \n", geek. triu(a), "\n") 
  
print("Diagonal above main Diagonal elements : \n", geek. triu(a, 1), "\n\n") 
  
print("Main Diagonal elements : \n", geek. triu(a, -1))

輸出:


Main Diagonal elements : 
 [[  1  21  30]
 [  0 434   3]
 [  0   0  56]] 

Diagonal above main Diagonal elements : 
 [[ 0 21 30]
 [ 0  0  3]
 [ 0  0  0]] 


Main Diagonal elements : 
 [[  1  21  30]
 [ 63 434   3]
 [  0  54  56]]


相關用法


注:本文由純淨天空篩選整理自 numpy.triu() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。