Python矩阵
时间:2020-02-23 14:43:00 来源:igfitidea点击:
在本教程中,我们将学习Python矩阵。
Python矩阵
要使用Python Matrix,我们需要导入Python numpy模块。
如果您对numpy模块不了解,可以阅读python numpy教程。
Python矩阵用于执行有关矩阵的操作,可用于科学目的,图像处理等。
Python中创建矩阵
在本节中,我们将学习如何在python中创建矩阵。
根据维基百科,矩阵是数字,符号或者表达式的矩形阵列,排列成行和列。
因此,在下面的代码中,我们将初始化各种类型的矩阵。
通常,使用numpy.matix()函数创建矩阵。
我们可以使用numpy.shape来知道矩阵的尺寸。
请参阅以下python矩阵示例代码。
import numpy as np
# create 2x2 matrix
a = np.matrix([[1, 2], [3, 4]]) # using array of array
print('2x2 matrix is:\n', a)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', a.shape)
# using MatLab syntax in string
b = np.matrix('[1,2;3,4;5,6]', dtype=np.int32) # limiting the data-type to int
print('\n3x2 matrix is:\n', b)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', b.shape)
# using numpy.random.rand(row, column) to generate array of random element
c = np.matrix(np.random.rand(3, 3), dtype=np.float32) # considering the data-type as float
print('\n3x3 random element matrix is:\n', c)
# using shape attribute to get the tuple describing matrix shape
print('The dimension of the matrix is :', c.shape)
您将得到如下图所示的输出。
Python矩阵加法
矩阵加法的手动代码足够复杂!多亏了numpy模块,我们可以简单地使用+运算符进行矩阵加法。
因此,在以下示例代码中,我们将看到手动编写附加代码以及使用加号运算符。
import numpy as np
# create two 2x2 matrix
a = np.matrix([[1, 2], [3, 4]]) # using array of array
b = np.matrix([[5, 6], [7, 8]]) # using array of array
result = np.matrix(np.zeros((2,2))) # result matrix
print('A matrix :\n', a)
print('\nB matrix :\n', b)
# traditional code
for i in range(a.shape[1]):
for j in range(a.shape[0]):
result[i, j] = a[i, j] + b[i, j]
print('\nManually calculated result :\n', result)
# get the result by simply using + operator
resultB = a + b
print('\nCalculated using matrix + operator :\n', resultB)
以下是python矩阵附加代码的输出。
A matrix : [[1 2] [3 4]] B matrix : [[5 6] [7 8]] Manually calculated result : [[ 6. 8.] [ 10. 12.]] Calculated using matrix + operator : [[ 6 8] [10 12]]
Python矩阵乘法,逆矩阵,矩阵转置
在上一节中,我们讨论了Python Matrix的好处,即它使我们的任务变得简单。
这样,我们可以简单地将两个矩阵相乘,得到矩阵的逆和转置。
正如我们之前看到的,+运算符将两个矩阵相加,这里我们可以简单地使用*运算符将矩阵相乘。
对于矩阵乘法,第一个矩阵中的列数应等于第二个矩阵中的行数。
我们可以使用getI()函数获得矩阵的逆矩阵。
我们可以使用getT()获取矩阵的转置。
让我们看一下矩阵乘法的例子。
import numpy as np
# initialize a 3x2 matrix of random values
matA = np.matrix(np.random.rand(3, 2))
# print the first matrix
print('The first matrix is :\n', matA)
# initialize a 2x3 matrix of random values
matB = np.matrix(np.random.rand(2, 3))
# print the second matrix
print('\nThe second matrix is :\n', matB)
# multiply two matrix using * operator
result = matA * matB
# print the resultant matrix
print('\nMatrix multiplication result :\n', result)
# get the inverse of the first matrix
inverseMatA = matA.getI()
print('\nThe inverse of the first matrix is :\n', inverseMatA)
# get the transpose matrix of the second matrix
transposeMatB = matB.getT()
print('\nThe transpose of the second matrix is :\n', transposeMatB)
由于我们使用了随机值。
因此,矩阵的元素将有所不同。
但是,下面的代码输出是在我的计算机上运行的示例。
The first matrix is : [[ 0.88847844 0.01832413] [ 0.08538396 0.20178474] [ 0.92615527 0.8963927 ]] The second matrix is : [[ 0.03454971 0.89908281 0.08825769] [ 0.46224998 0.63173062 0.91734146]] Matrix multiplication result : [[ 0.039167 0.81039161 0.09522454] [ 0.09636365 0.20443036 0.1929165 ] [ 0.44635589 1.398969 0.90403851]] The inverse of the first matrix is : [[ 1.12771189 -0.15722127 0.01239153] [-1.13143853 0.40000541 1.04853336]] The transpose of the second matrix is : [[ 0.03454971 0.46224998] [ 0.89908281 0.63173062] [ 0.08825769 0.91734146]]

