Numpy 的 tri(~)
方法創建一個表示下三角矩陣的 2D Numpy 數組。主對角線及其下方的值用 1 填充,而其他地方用 0 填充。
參數
1. N
| int
結果數組的行數。
2. M
| int
| optional
結果數組的列數。默認情況下,M=N
。
3. k
| int
| optional
要排除或包含的對角線的數量。
k
的正值表示包含。 k=1
意味著我們在主對角線之上添加了一條附加對角線。
k
的負值表示排除。 k=-1
表示排除主對角線。 k=-2
表示排除主對角線和下麵的對角線。
默認情況下, k=0
,這意味著返回一個完美的下三角形。
4. dtype
| string
或 type
| optional
結果數組的數據類型。默認情況下,dtype=float
。
返回值
表示下三角矩陣的 Numpy 數組。
例子
基本用法
創建 3 x 3 下三角矩陣:
np.tri(3)
array([[ 1., 0., 0.],
[ 1., 1., 0.],
[ 1., 1., 1.]])
要創建 int
類型的 3 x 4 下三角矩陣:
np.tri(3, 4, dtype=int)
array([[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 1, 1, 0]])
指定正 k
要包含附加對角線,請設置 k=1
:
np.tri(3, k=1, dtype=int)
array([[1, 1, 0],
[1, 1, 1],
[1, 1, 1]])
指定負 k
要排除主對角線,請設置 k=-1
:
np.tri(3, k=-1)
array([[ 0., 0., 0.],
[ 1., 0., 0.],
[ 1., 1., 0.]])
相關用法
- Python NumPy trim_zeros方法用法及代碼示例
- Python trunc()用法及代碼示例
- Python tracemalloc.Traceback.format用法及代碼示例
- Python NumPy true_divide方法用法及代碼示例
- Python NumPy transpose方法用法及代碼示例
- Python NumPy trunc方法用法及代碼示例
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python torchaudio.transforms.Fade用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.summary.scalar用法及代碼示例
- Python tf.linalg.LinearOperatorFullMatrix.matvec用法及代碼示例
- Python tf.linalg.LinearOperatorToeplitz.solve用法及代碼示例
- Python tf.raw_ops.TPUReplicatedInput用法及代碼示例
- Python tf.raw_ops.Bitcast用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python Pandas tseries.offsets.CustomBusinessHour.onOffset用法及代碼示例
- Python torch.special.gammaincc用法及代碼示例
- Python typing.get_type_hints用法及代碼示例
- Python tensorflow.math.xlog1py()用法及代碼示例
- Python tf.compat.v1.Variable.eval用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.experimental_distribute_values_from_function用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | tri method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。