numpy.tri Method in Python


The numpy.tri method can be used to get an array of 1's at and below a given diagonal and 0's elsewhere.

Syntax

numpy.tri(N, M=None, k=0, dtype=<class 'float'>)

Parameters

numpy.tri accepts the following parameters −

  • N - It defines the number of the rows in an array.

  • M - It defines the number of columns in an array. By default, it is None.

  • k - Use k = 0, for the main diagonal, while k < 0 is below it and k > 0 is above it.

  • dtype - It is data type of the returned array. By default, it is float.

Example 1

Let us consider the following example −

# import numpy library
import numpy as np

# numpy.tri() function
y = np.tri(5, 3, 0)

# Display Tri Value
print("Tri function of Y: 
", y)

Output

It will generate the following output −

Tri function of Y:
[[1. 0. 0.]
[1. 1. 0.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

Example 2

Let us take another example −

#import numpy library
import numpy as np

# numpy.tri() function
x = np.tri(2, 4, -5, dtype=int)

# Display Tri Value
print("Tri function of X: 
", x)

Output

It will generate the following output −

Tri function of X:
[[0 0 0 0]
[0 0 0 0]]

Updated on: 11-Feb-2022

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements