python中怎么用numpy模块创建矩阵

38次阅读
没有评论

共计 684 个字符,预计需要花费 2 分钟才能阅读完成。

在 Python 中,可以使用 numpy 模块创建矩阵。下面是一些常用的方法:

  1. 使用 numpy 的 array 函数创建矩阵:
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)

输出:

[[1 2 3]
 [4 5 6]
 [7 8 9]]
  1. 使用 numpy 的 zeros 函数创建全零矩阵:
import numpy as np

matrix = np.zeros((3, 3))
print(matrix)

输出:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
  1. 使用 numpy 的 ones 函数创建全一矩阵:
import numpy as np

matrix = np.ones((3, 3))
print(matrix)

输出:

[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
  1. 使用 numpy 的 eye 函数创建单位矩阵:
import numpy as np

matrix = np.eye(3)
print(matrix)

输出:

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  1. 使用 numpy 的 random 函数创建随机矩阵:
import numpy as np

matrix = np.random.random((3, 3))
print(matrix)

输出类似于:

[[0.12345678 0.98765432 0.54321098]
 [0.87654321 0.23456789 0.98765432]
 [0.3456789  0.90123457 0.7654321]]

这些是使用 numpy 模块创建矩阵的一些常见方法,可以根据需要选择适合的方法创建所需的矩阵。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计684字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)