scipy.sparse.

triu#

scipy.sparse.triu(A, k=0, format=None)[Quelle]#

Gibt den oberen Dreiecksteil eines spärlichen Arrays oder einer spärlichen Matrix zurück

Gibt die Elemente auf oder oberhalb der k-ten Diagonalen von A zurück.
  • k = 0 entspricht der Hauptdiagonalen

  • k > 0 liegt oberhalb der Hauptdiagonalen

  • k < 0 liegt unterhalb der Hauptdiagonalen

Parameter:
Adichtes oder spärliches Array oder Matrix

Matrix, deren oberer Dreiecksteil gewünscht ist.

kintegeroptional

Die unterste Diagonale des oberen Dreiecks.

formatstring

Spärliches Format des Ergebnisses, z. B. format=„csr“, etc.

Rückgabe:
Lspärliches Array oder Matrix

Oberer Dreiecksteil von A im spärlichen Format. Spärliches Array, wenn A ein spärliches Array ist, andernfalls Matrix.

Siehe auch

tril

unteres Dreieck im spärlichen Format

Beispiele

>>> from scipy.sparse import csr_array, triu
>>> A = csr_array([[1, 2, 0, 0, 3], [4, 5, 0, 6, 7], [0, 0, 8, 9, 0]],
...                dtype='int32')
>>> A.toarray()
array([[1, 2, 0, 0, 3],
       [4, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> triu(A).toarray()
array([[1, 2, 0, 0, 3],
       [0, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> triu(A).nnz
8
>>> triu(A, k=1).toarray()
array([[0, 2, 0, 0, 3],
       [0, 0, 0, 6, 7],
       [0, 0, 0, 9, 0]], dtype=int32)
>>> triu(A, k=-1).toarray()
array([[1, 2, 0, 0, 3],
       [4, 5, 0, 6, 7],
       [0, 0, 8, 9, 0]], dtype=int32)
>>> triu(A, format='csc')
<Compressed Sparse Column sparse array of dtype 'int32'
    with 8 stored elements and shape (3, 5)>