Python 如何创建向量或矩阵
在本文中,我们将向您展示如何在Python中创建向量或矩阵。
NumPy 是一个专为在Python中高效处理数组而设计的Python库。它快速,易学,并且存储效率高。在NumPy中,我们可以生成一个n维数组。
什么是向量
在Python中,向量是由组成的普通数字构建的。向量可以被视为一串数字的列表,向量代数即在列表中的数字上进行的运算。换句话说,向量就是 NumPy的1维数组 。
我们使用 np.array() 方法来创建一个向量。
语法
np.array(list)
参数
- list - 一个一维列表,可以是一行n列或n行一列。
返回值 - 返回向量(numpy.ndarray)
从给定列表创建水平向量
在这个方法中,我们使用 numpy.array() 函数从列表创建一个水平向量。
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入带有别名的 NumPy 模块。
-
创建一个变量来存储 水平 一维列表。
-
使用 numpy.array() 函数(返回一个ndarray。ndarray是满足给定需求的数组对象)通过将给定list_1作为参数传递给它创建一个向量vector_1,即作为 行 。
-
打印结果的水平向量。
下面的程序使用NumPy的array()函数从列表创建水平向量并返回它 –
示例
# 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()函数创建垂直向量并返回结果:
# 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']]
使用numpy.mat()函数创建矩阵
在这个方法中,我们使用numpy.mat()函数来创建一个矩阵。
在Python中,mat()方法用于将一个数组转换为矩阵。
参数
mat()函数接受以下参数:
- data - 输入数据或类似数组的对象。
-
dtype - 输出矩阵的数据类型。
返回值
mat()方法将输入解释为矩阵并返回它。
步骤
执行所需任务的算法/步骤如下:
- 使用import关键字导入NumPy模块,并使用别名。
-
将嵌套列表(列表的列表)作为参数传递给numpy.mat()函数(mat()方法用于将数组转换为矩阵),并将此矩阵存储在一个变量中。
-
打印结果矩阵。
示例
以下程序使用Numpy mat()函数创建矩阵并返回它:
# 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()函数接受以下参数:
- data - 输入的数据或类似数组的对象。
-
dtype - 输出矩阵的数据类型。
返回值:
数据的矩阵表示
步骤
以下是执行所需任务的算法/步骤:
- 使用导入关键字导入带有别名的NumPy模块。
-
将嵌套列表(列表的列表)作为参数传递给numpy.matrix()函数(从数据字符串或类似数组的对象中,该类返回一个矩阵。生成的矩阵是一个特殊的2D数组),并将此矩阵存储在变量中。
-
打印结果矩阵。
示例
以下程序使用Numpy matrix()函数创建矩阵并返回它:
# 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中生成矩阵的方法,以及如何创建垂直和水平向量。