如何在 Python 中创建向量或矩阵?


在本文中,我们将向您展示如何在 Python 中创建向量或矩阵。

NumPy 是一个 Python 库,旨在高效地处理 Python 中的数组。它速度快、易于学习且存储效率高。在 NumPy 中,我们可以生成一个 n 维数组。

什么是向量?

在 Python 中,向量由分量构成,分量是普通的数字。向量可以被认为是数字列表,向量代数可以被认为是对列表中数字执行的操作。换句话说,向量是NumPy 的 1 维数组

我们使用np.array() 方法来创建向量。

语法

np.array(list)

参数

  • 列表 - 一个一维列表,可以有一行 n 列或 n 行一列。

返回值 - 返回向量(numpy.ndarray)

从给定列表创建水平向量

在这种方法中,我们使用numpy.array() 函数从列表创建水平向量。

算法(步骤)

以下是执行所需任务应遵循的算法/步骤 -

  • 使用 import 关键字导入NumPy 模块,并使用别名。

  • 创建一个变量来存储水平的一维列表。

  • 使用numpy.array() 函数(返回一个 ndarray。ndarray 是一个满足给定要求的数组对象)通过将给定的 list_1 作为参数传递给它来创建 vector_1,即向量作为

  • 打印结果水平向量。

以下程序使用 NumPy array() 函数从列表创建水平向量并返回它 -

示例

Open Compiler
# importing numpy module with an alias name import numpy as np # creating a Horizontal 1-Dimensional list list_1 = [ 15, 20, 25, 'Hello', 'TutorialsPoint'] # creating a vector(numpy array) of the horizontal list # This is the horizontal vector vector_1 = np.array(list_1) print('Given List =',list_1) # printing the resultant horizontal vector print("The resultant horizontal vector:") print(vector_1)

输出

执行上述程序将生成以下输出 -

Given List = [15, 20, 25, 'Hello', 'TutorialsPoint']
The resultant horizontal vector:
['15' '20' '25' 'Hello' 'TutorialsPoint']

创建垂直向量

在这种方法中,我们使用 numpy.array() 函数创建垂直向量。

算法(步骤)

以下是执行所需任务应遵循的算法/步骤 -

  • 使用 import 关键字导入NumPy 模块,并使用别名。

  • 将垂直列表作为参数传递给numpy.array() 函数(返回一个 ndarray。ndarray 是一个满足给定要求的数组对象),并将此垂直向量存储在一个变量中。

  • 打印结果垂直向量。

示例

以下程序使用 NumPy array() 函数创建垂直向量并返回它 -

Open Compiler
# importing NumPy module with an alias name import numpy as np # Passing vertical list as an argument to array() function # creating a vertical vector(NumPy array) of the second list vector_2 = np.array([[5], [40], [20], ['Hello'], ['TutorialsPoint']]) # printing the resultant vertical vector print("The resultant vertical vector:") print(vector_2)

输出

执行上述程序将生成以下输出 -

The resultant vertical vector:
[['5']
 ['40']
 ['20']
 ['Hello']
 ['TutorialsPoint']]

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

使用 numpy.mat() 函数创建矩阵

在这种方法中,我们使用numpy.mat() 函数创建矩阵。

在 Python 中,mat() 方法用于将数组转换为矩阵。

参数

mat() 函数接受以下参数 -

  • 数据 - 这是输入数据或类似数组的对象。

  • 数据类型 - 表示输出矩阵的数据类型。

返回值

mat() 方法将输入解释为矩阵并返回它。

算法(步骤)

以下是执行所需任务应遵循的算法/步骤 -

  • 使用 import 关键字导入NumPy 模块,并使用别名。

  • 将嵌套列表(列表的列表)作为参数传递给numpy.mat() 函数(mat() 方法用于将数组转换为矩阵),并将此矩阵存储在一个变量中。

  • 打印结果矩阵。

示例

以下程序使用 Numpy mat() 函数创建矩阵并返回它 -

Open Compiler
# importing numpy module with an alias name import numpy as np # Creating a matrix using numpy.mat() function inputMatrix = np.mat([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("The created matrix is:") print(inputMatrix)

输出

执行上述程序将生成以下输出 -

The created matrix is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

使用 numpy.matrix() 函数创建矩阵

在这种方法中,我们使用numpy.matrix() 函数创建矩阵。

参数

numpy.matrix() 函数接受以下参数 -

  • 数据 - 这是输入数据或类似数组的对象。

  • 数据类型 - 表示输出矩阵的数据类型。

返回值

数据的矩阵表示

算法(步骤)

以下是执行所需任务应遵循的算法/步骤 -

  • 使用 import 关键字导入NumPy 模块,并使用别名。

  • 将嵌套列表(列表的列表)作为参数传递给numpy.matrix() 函数(从数据字符串或类似数组的对象中,此类返回一个矩阵。生成的矩阵是一个专门的二维数组),并将此矩阵存储在一个变量中。

  • 打印结果矩阵。

示例

以下程序使用 Numpy matrix() 函数创建矩阵并返回它 -

Open Compiler
# importing numpy module with an alias name import numpy as np # Creating a matrix using numpy.matrix() function inputMatrix = np.matrix([[5, 3, 9, 11], [4, 5, 6, 23], [7, 8, 9, 84]]) print("The created matrix is:") print(inputMatrix)

输出

执行上述程序将生成以下输出 -

The created matrix is:
[[ 5  3  9 11]
 [ 4  5  6 23]
 [ 7  8  9 84]]

结论

在本教程中,我们学习了在 Python 中生成矩阵的两种不同方法,以及如何创建垂直和水平向量。

更新于:2022 年 10 月 20 日

10K+ 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告