DAY5 |
您所在的位置:网站首页 › numpy实现矩阵转置 › DAY5 |
什么是Python矩阵?
Python矩阵是存储在行和列中的专用二维数据矩形数组。 矩阵中的数据可以是数字,字符串,表达式,符号等。矩阵是可用于数学和科学计算的重要数据结构之一。 Python矩阵如何运作?二维数组中矩阵格式的数据如下: 第1步) 它显示了一个2×2矩阵。它有两行两列。矩阵内的数据是数字。 row1的值为2,3,row2的值为4,5。列即col1的值为2,4,而col2的值为3,5。 第2步) 它显示了一个2×3矩阵。它有两行三列。第一行内的数据,即row1,具有值2,3,4,而row2具有值5,6,7。列col1的值为2,5,col2的值为3,6,col3的值为4,7。 因此,类似地,您可以将数据存储在Python中的nxn矩阵内。可以对类似矩阵的加法,减法,乘法等进行很多操作。 Python没有实现矩阵数据类型的直接方法。 python矩阵利用数组,并且可以实现相同的数组。 使用嵌套列表数据类型创建Python矩阵使用Python Numpy包中的数组创建Python矩阵 使用嵌套列表数据类型创建Python Matrix在Python中,使用列表数据类型表示数组。因此,现在将利用该列表创建一个python矩阵。 我们将创建一个3×3矩阵,如下所示: 矩阵有3行3列。列表格式的第一行如下:[8,14,-6]列表中的第二行将是:[12,7,4]列表中的第三行将是:[-11,3,21]包含所有行和列的列表内的矩阵如下所示: List = [[Row1], [Row2], [Row3] ... [RowN]]因此,根据上面列出的矩阵,具有矩阵数据的列表类型如下: M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] 使用列表读取Python Matrix中的数据。我们将利用上面定义的矩阵。 该示例将读取数据,打印矩阵,显示每行的最后一个元素。 示例:打印矩阵 M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] #To print the matrix print(M1)Output: The Matrix M1 = [[8, 14, -6], [12, 7, 4], [-11, 3, 21]]示例2:读取每一行的最后一个元素。 M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] matrix_length = len(M1) #To read the last element from each row. for i in range(matrix_length): print(M1[i][-1])Output: -6 4 21示例3:打印矩阵中的行 M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] matrix_length = len(M1) #To print the rows in the Matrix for i in range(matrix_length): print(M1[i])Output: [8, 14, -6] [12, 7, 4] [-11, 3, 21] 使用嵌套列表添加矩阵我们可以轻松地添加两个给定的矩阵。 此处的矩阵将以列表形式显示。 让我们来研究一个示例,该示例将注意添加给定的矩阵。 Matrix 1: M1 = [[8, 14, -6], [12,7,4], [-11,3,21]]Matrix 2 : M2 = [[3, 16, -6], [9,7,-4], [-1,3,13]]最后将初始化一个矩阵,该矩阵将存储M1 + M2的结果。 Matrix 3 : M3 = [[0,0,0], [0,0,0], [0,0,0]]示例:添加矩阵 作为补充,矩阵将使用for循环,该循环将遍历给定的两个矩阵。 M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] M2 = [[3, 16, -6], [9,7,-4], [-1,3,13]] M3 = [[0,0,0], [0,0,0], [0,0,0]] matrix_length = len(M1) #To Add M1 and M2 matrices for i in range(len(M1)): for k in range(len(M2)): M3[i][k] = M1[i][k] + M2[i][k] #To Print the matrix print("The sum of Matrix M1 and M2 = ", M3)Output: The sum of Matrix M1 and M2 = [[11, 30, -12], [21, 14, 0], [-12, 6, 34]] 使用嵌套列表的矩阵乘法要相乘矩阵,我们可以在两个矩阵上使用for循环,如下面的代码所示: M1 = [[8, 14, -6], [12,7,4], [-11,3,21]] M2 = [[3, 16, -6], [9,7,-4], [-1,3,13]] M3 = [[0,0,0], [0,0,0], [0,0,0]] matrix_length = len(M1) #To Multiply M1 and M2 matrices for i in range(len(M1)): for k in range(len(M2)): M3[i][k] = M1[i][k] * M2[i][k] #To Print the matrix print("The multiplication of Matrix M1 and M2 = ", M3)Output: The multiplication of Matrix M1 and M2 = [[24, 224, 36], [108, 49, -16], [11, 9, 273]] 使用Python Numpy包中的数组创建Python矩阵python库Numpy帮助处理数组。 与列表相比,Numpy处理数组要快一些。 要使用Numpy,您需要先安装它。 请按照下面给出的步骤安装Numpy。 第1步) 安装Numpy的命令是: pip install NumPy第2步) 要在代码中使用Numpy,必须将其导入。 import NumPy步骤3) 您还可以使用别名导入Numpy,如下所示: import NumPy as np我们将利用Numpy的array()方法创建一个python矩阵。 示例:Numpy中的数组以创建Python矩阵 import numpy as np M1 = np.array([[5, -10, 15], [3, -6, 9], [-4, 8, 12]]) print(M1)Output: [[ 5 -10 15] [ 3 -6 9] [ -4 8 12]] 使用Numpy.Array()进行矩阵运算可以完成的矩阵运算是加法,减法,乘法,转置,读取矩阵的行,列,对矩阵进行切片等。在所有示例中,我们将使用array()方法。 矩阵加法 为了对矩阵执行加法运算,我们将使用numpy.array()创建两个矩阵,并使用(+)运算符将它们相加。 Example: import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, 14, 21]]) M2 = np.array([[9, -18, 27], [11, 22, 33], [13, -26, 39]]) M3 = M1 + M2 print(M3)Output: [[ 12 -12 36] [ 16 12 48] [ 6 -12 60]]矩阵减法 为了对矩阵进行减法,我们将使用numpy.array()创建两个矩阵,并使用(-)运算符将它们相减。 Example: import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, 14, 21]]) M2 = np.array([[9, -18, 27], [11, 22, 33], [13, -26, 39]]) M3 = M1 - M2 print(M3)Output: [[ -6 24 -18] [ -6 -32 -18] [-20 40 -18]]矩阵乘法 首先将使用numpy.arary()创建两个矩阵。 若要将它们相乘,可以使用numpy dot()方法。 Numpy.dot()是矩阵M1和M2的点积。 Numpy.dot()处理2D数组并执行矩阵乘法。 Example: import numpy as np M1 = np.array([[3, 6], [5, -10]]) M2 = np.array([[9, -18], [11, 22]]) M3 = M1.dot(M2) print(M3)Output: [[ 93 78] [ -65 -310]]矩阵转置 通过将行更改为列,将列更改为行来计算矩阵的转置。 Numpy的transpose()函数可用于计算矩阵的转置。 Example: import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [4,8,12]]) M2 = M1.transpose() print(M2)Output: [[ 3 5 4] [ 6 -10 8] [ 9 15 12]]矩阵切片(Slicing) 切片将根据给定的开始/结束索引从矩阵中返回元素。 切片的语法是-[start:end]如果未指定起始索引,则将其视为0。例如[:5],则表示为[0:5]。如果未通过结尾,则将其作为数组的长度。如果开始/结尾具有负值,它将从数组的结尾开始切片。在对矩阵进行切片之前,让我们首先了解如何将切片应用于简单数组。 import numpy as np arr = np.array([2,4,6,8,10,12,14,16]) print(arr[3:6]) # will print the elements from 3 to 5 print(arr[:5]) # will print the elements from 0 to 4 print(arr[2:]) # will print the elements from 2 to length of the array. print(arr[-5:-1]) # will print from the end i.e. -5 to -2 print(arr[:-1]) # will print from end i.e. 0 to -2Output: [ 8 10 12] [ 2 4 6 8 10] [ 6 8 10 12 14 16] [ 8 10 12 14] [ 2 4 6 8 10 12 14]现在让我们在矩阵上实现切片。 对矩阵执行切片 语法将为M1 [row_start:row_end,col_start:col_end] 第一个开始/结束将针对该行,即选择矩阵的行。第二个开始/结束将用于该列,即选择矩阵的列。我们将使用的矩阵M1如下: M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]])共有4行。 索引从0到3。第0行是[2,4,6,8,10],第1行是[3,6,9,-12,-15],后跟第二和第三。 矩阵M1具有5列。 索引从0到4。第0列的值为[2,3,4,5],第1列的值为[4,6,8,-10],后跟第2,第3,第4和第5。 这是一个示例,显示了如何使用切片从矩阵获取行和列数据。 在示例中,我们要打印第一行和第二行,对于列,我们需要第一列,第二列和第三列。 为了获得该输出,我们使用了:M1 [1:3,1:4] Example: import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[1:3, 1:4]) # For 1:3, it will give first and second row. #The columns will be taken from first to third.Output: [[ 6 9 -12] [ 8 12 16]]示例:要打印所有行和第三列 import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:,3]) # This will print all rows and the third column data.Output: [ 8 -12 16 -20]示例:打印第一行和所有列 import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:1,]) # This will print first row and all columnsOutput: [[ 2 4 6 8 10]]示例:打印前三行和前2列 import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:3,:2])Output: [[2 4] [3 6] [4 8]] 访问NumPy矩阵我们已经看到了切片的工作原理。 考虑到这一点,我们将如何从矩阵中获取行和列。 打印矩阵的行 在示例中将打印矩阵的行。 Example: import numpy as np M1 = np.array([[3, 6, 9], [5, -10, 15], [4,8,12]]) print(M1[0]) #first row print(M1[1]) # the second row print(M1[-1]) # -1 will print the last rowOutput: [3 6 9] [ 5 -10 15] [ 4 8 12]要获取最后一行,可以使用索引或-1。 例如,矩阵有3行, 所以M1 [0]会给你第一行, M1 [1]将给您第二行 M1 [2]或M1 [-1]将为您提供第三行或最后一行。 打印矩阵的列 import numpy as np M1 = np.array([[2, 4, 6, 8, 10], [3, 6, 9, -12, -15], [4, 8, 12, 16, -20], [5, -10, 15, -20, 25]]) print(M1[:,0]) # Will print the first Column print(M1[:,3]) # Will print the third Column print(M1[:,-1]) # -1 will give you the last columnOutput: [2 3 4 5] [ 8 -12 16 -20] [ 10 -15 -20 25] 摘要: Python矩阵是存储在行和列中的专用二维数据矩形数组。矩阵中的数据可以是数字,字符串,表达式,符号等。矩阵是可用于数学和科学计算的重要数据结构之一。Python没有实现矩阵数据类型的直接方法。可以使用嵌套列表数据类型和numpy库创建Python矩阵。python库Numpy帮助处理数组。与列表相比,Numpy处理数组要快一些。可以完成的矩阵运算是加法,减法,乘法,转置,读取矩阵的行,列,对矩阵进行切片等。要添加两个矩阵,可以使用numpy.array()并使用(+)运算符添加它们。若要将它们相乘,可以使用numpy dot()方法。 Numpy.dot()是矩阵M1和M2的点积。 Numpy.dot()处理2D数组并执行矩阵乘法。通过将行更改为列,将列更改为行来计算矩阵的转置。 Numpy的transpose()函数可用于计算矩阵的转置。切片矩阵将根据给定的开始/结束索引返回元素。 阅读: 561 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |