功能:取出稀疏矩阵的上三角部分的非零元素
例
import numpy as np
import scipy.sparse as sp
X = np.array([[1,0,4],[0,1,0],[1,0,0]])
print(X)
sX = sp.csr_matrix(X)
print(sX)
print('-----------------')
print (sp.triu(sX))
输出为
[[1 0 4]
[0 1 0]
[1 0 0]]
(0, 0) 1
(0, 2) 4
(1, 1) 1
(2, 0) 1
-----------------
(0, 0) 1
(0, 2) 4
(1, 1) 1
因为(2,0)不是上三角中元素,所以不会输出