BetterDocs
Home
Docs

Creation | np.tri()

Method:

np.tri(N, M=None, k=0, dtype=np.float64, *,like=None)

Generates a 2D array with a triangular (lower or upper) shape.

Returns:

np.ndarray

Parameters:

N: int-

The number of rows.

import numpy as np
result = np.tri(N=3)
print(result)
'''
Output:
[[1. 0. 0.]
 [1. 1. 0.]
 [1. 1. 1.]]
'''

M: int, Optional-

The number of columns (default is N).

import numpy as np
result = np.tri(N=3, M=2)
print(result)
'''
Output:
[[1. 0.]
 [1. 1.]
 [1. 1.]]
'''

k: int, Optional-

The diagonal offset (default 0). A positive value refers to an upper diagonal, and a negative value to a lower diagonal.

k = 0 (default) +

k = 1 +

k = -1 +

dtype: np.float64, Optional-

The desired data-type for the NumPy array is inferred from the dtype specified.

import numpy as np
result = np.tri(N=3, M=3, k=0, dtype=np.int32)
print(result)
'''
Output:
[[1 0 0]
 [1 1 0]
 [1 1 1]]
'''

Values: +

By passing the dtype, you are implicitly converting the values to the passed dtype.

like: None, Optional-

The like parameter in np.tri() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy