eye函数在python中的参数设置

   2024-09-30 2880
核心提示:eye 函数是 NumPy 库中的一个函数,用于创建一个对角线上为 1,其余元素为 0 的二维数组(也称为单位矩阵)numpy.eye(N, M=None,

eye 函数是 NumPy 库中的一个函数,用于创建一个对角线上为 1,其余元素为 0 的二维数组(也称为单位矩阵)

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

参数说明:

N:输出矩阵的行数。必需参数。M:输出矩阵的列数。可选参数,默认值为 N,即创建一个方阵。k:指定对角线的位置。可选参数,默认值为 0,表示主对角线。正值表示上对角线,负值表示下对角线。dtype:输出矩阵的数据类型。可选参数,默认值为 float 类型。

示例:

import numpy as np# 创建一个 3x3 的单位矩阵identity_matrix = np.eye(3)print("3x3 identity matrix:\n", identity_matrix)# 创建一个 4x4 的单位矩阵identity_matrix_4x4 = np.eye(4)print("\n4x4 identity matrix:\n", identity_matrix_4x4)# 创建一个 3x4 的矩阵,其中第 2 条对角线上的元素为 1matrix_3x4 = np.eye(3, 4, k=1)print("\n3x4 matrix with 2nd diagonal ones:\n", matrix_3x4)# 创建一个 4x4 的整数单位矩阵integer_identity_matrix = np.eye(4, dtype=int)print("\n4x4 integer identity matrix:\n", integer_identity_matrix)

输出结果:

3x3 identity matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]4x4 identity matrix: [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]3x4 matrix with 2nd diagonal ones: [[0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]4x4 integer identity matrix: [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号