python set函數,python矩陣_Python矩陣

 2023-11-19 阅读 31 评论 0

摘要:python矩陣In this tutorial we will learn about Python Matrix. In our previous tutorial we learnt about Python JSON operations.python set函數? 在本教程中,我們將學習Python矩陣。 在上一教程中,我們了解了Python JSON操作。 Python矩陣 (Python Matr

python矩陣

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

python set函數? 在本教程中,我們將學習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)

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.

python矩陣計算, 根據維基百科,矩陣是數字,符號或表達式的矩形陣列,排列成行和列。 因此,在下面的代碼中,我們將初始化各種類型的矩陣。

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)

python sort函數,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.

python for循環、 矩陣加法的手動代碼非常復雜,難以編寫! 多虧了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 + operatorresultB = 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矩陣

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/183184.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息