如何使用 Numpy 创建单位矩阵?
pythonnumpyserver side programmingprogramming更新于 2024/9/21 17:48:17
在此程序中,我们将打印一个大小为 nxn 的单位矩阵,其中 n 将作为用户的输入。我们将使用 numpy 库中的identity() 函数,该函数将元素的维度和数据类型作为参数
算法
步骤 1:导入 numpy。 步骤 2:将维度作为用户的输入。 步骤 3:使用 numpy.identity() 函数打印单位矩阵。
示例代码
import numpy as np
dimension = int(input("输入恒等矩阵的维度:"))
identity_matrix = np.identity(dimension, dtype="int")
print(identity_matrix)
输出
输入恒等矩阵的维度:5 [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

