python矩阵_Python矩阵

python矩阵

In this tutorial we will learn about Python Matrix. In our previous tutorial we learnt about Python JSON operations.

在本教程中,我们将学习Python矩阵。 在上一教程中,我们了解了Python JSON操作。

Python矩阵 (Python Matrix)

To work with Python Matrix, we need to import Python numpy module. If you do not have any idea about numpy module you can read python numpy tutorial. Python matrix is used to do operations regarding matrix, which may be used for scientific purpose, image processing etc.

要使用Python Matrix,我们需要导入Python numpy模块。 如果您对numpy模块不了解,可以阅读python numpy教程 。 Python矩阵用于执行有关矩阵的操作,可用于科学目的,图像处理等。

创建矩阵Python (Create Matrix Python)

In this section we will learn how to create a matrix in python.

在本节中,我们将学习如何在python中创建矩阵。

According to wikipedia, a matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. So, in the following code we will be initializing various types of matrices.

根据维基百科,矩阵是数字,符号或表达式的矩形阵列,排列成行和列。 因此,在下面的代码中,我们将初始化各种类型的矩阵。

Generally a matrix is created using numpy.matix() function. We can use numpy.shape to know the dimension of the matrix. See the following python matrix example code.

通常,使用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)

You will get output like the following image.

您将获得如下图所示的输出。

Python矩阵加法 (Python Matrix Addition)

The manual code for matrix addition is complex enough to write! Thanks to numpy module, we can simply use + operator to for matrix addition. So, in the following example code we will see both to write the addition code manually and also by using plus operator.

矩阵加法的手动代码非常复杂,难以编写! 多亏了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)

The output of the python matrix addition code is following.

以下是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 Multiplication, Inverse Matrix, Matrix Transpose)

In the previous section we have discussed about the benefit of Python Matrix that it just makes the task simple for us. Like that, we can simply Multiply two matrix, get the inverse and transposition of a matrix.

在上一节中,我们讨论了Python Matrix的好处,即它使我们的任务变得简单。 这样,我们可以简单地将两个矩阵相乘,得到矩阵的逆和转置。

As we have seen before that + operator adds two matrix, here we can simply use * operator to multiply matrices.

正如我们之前看到的, +运算符将两个矩阵相加,这里我们可以简单地使用*运算符将矩阵相乘。

For matrix multiplication, number of columns in first matrix should be equal to number of rows in second matrix.

对于矩阵乘法,第一个矩阵中的列数应等于第二个矩阵中的行数。

We can get the inverse of a matrix by using getI() function. We can use getT() to get the transpose of matrix. Let’s have a look at matrix multiplication example.

我们可以使用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)

As we have used random values. So the elements of the matrix will vary. However the output of the above code is given below for a sample run in my computer.

由于我们使用了随机值。 因此,矩阵的元素将有所不同。 但是,下面的代码输出是在我的计算机上运行的示例。

The first matrix is :
 [[ 0.88847844  0.01832413]
 [ 0.08538396  0.20208474]
 [ 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]]

So, that’s all for python matrix operations. To know more about python matrix, you may read the official documentation.

因此,这就是python矩阵运算的全部。 要了解有关python矩阵的更多信息,您可以阅读官方文档

翻译自: https://www.journaldev.com/16025/python-matrix

python矩阵

  • 5
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值