``````

In [1]:

import numpy as np

``````
``````

In [2]:

a = np.arange(16).reshape(4, 4)
print(a)

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]
[12 13 14 15]]

``````
``````

In [3]:

print(np.triu(a))

``````
``````

[[ 0  1  2  3]
[ 0  5  6  7]
[ 0  0 10 11]
[ 0  0  0 15]]

``````
``````

In [4]:

print(np.triu(a, k=2))

``````
``````

[[0 0 2 3]
[0 0 0 7]
[0 0 0 0]
[0 0 0 0]]

``````
``````

In [5]:

print(np.triu(a, k=-1))

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 0  9 10 11]
[ 0  0 14 15]]

``````
``````

In [6]:

a = np.arange(12).reshape(3, 4)
print(a)

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

``````
``````

In [7]:

print(np.triu(a))

``````
``````

[[ 0  1  2  3]
[ 0  5  6  7]
[ 0  0 10 11]]

``````
``````

In [8]:

print(np.triu(a, k=-1))

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 0  9 10 11]]

``````
``````

In [9]:

print(np.triu(np.arange(16).reshape(1, 1, 4, 4)))

``````
``````

[[[[ 0  1  2  3]
[ 0  5  6  7]
[ 0  0 10 11]
[ 0  0  0 15]]]]

``````