Use nested lists and NumPy Bag Python matrix . Matrix is a two-dimensional data structure , The numbers are arranged in rows and columns .
Python There is no built-in type of matrix . however , You can think of a list as a matrix .
example :
A = [[1, 4, 5], [-5, 8, 9]]
You can think of this list as having 2 That's ok 3 Columns of the matrix .
Pictured :
A = [[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]] print("A =", A) print("A[1] =", A[1]) # The second line print("A[1][2] =", A[1][2]) # The third element of the second line print("A[0][-1] =", A[0][-1]) # The last element of the first line column = []; # empty listfor row in A: column.append(row[2]) print("3rd column =", column)
When you run a program , Output is :
NumPy Is a software package for Scientific Computing , It supports powerful N Dimensional array object .
In the use of NumPy Before , It needs to be installed first .
If you use Windows, Use PyCharm install NumPy,NumPy It comes with some other software packages related to data science and machine learning .
Successfully installed NumPy, You can import and use it .
NumPy Provides a multidimensional array of numbers ( It's actually an object ).
example :
import numpy as npa = np.array([1, 2, 3])print(a) # Output : [1, 2, 3]print(type(a)) # Output : <class 'numpy.ndarray'>
NumPy The array class of is called ndarray.
notes :
NumPy The array class of is called ndarray.
There are several ways to create NumPy Array method .
import numpy as np A = np.array([[1, 2, 3], [3, 4, 5]])print(A) A = np.array([[1.1, 2, 3], [3, 4, 5]]) # Floating point array print(A) A = np.array([[1, 2, 3], [3, 4, 5]], dtype = complex) # A complex array print(A)
Running effect :
import numpy as np zeors_array = np.zeros( (2, 3) )print(zeors_array)ones_array = np.ones( (1, 5), dtype=np.int32 ) // dtypeprint(ones_array) # Output : [[1 1 1 1 1]]
ad locum , Appoint dtype 了 32 position (4 byte ). therefore , The array can take values from to .-2-312-31-1
3. Use arange() and shape()
import numpy as np A = np.arange(4) print('A =', A) B = np.arange(12).reshape(2, 6) print('B =', B)
Add two matrices , Multiplication of two matrices and transposition of a matrix . Before writing these programs , Nested lists are used . Let's see how to use NumPy Arrays do the same thing .
The addition of two matrices
Use + Operator takes two NumPy The corresponding elements of the matrix are added .
import numpy as np A = np.array([[2, 4], [5, -6]])B = np.array([[9, -3], [3, 6]])C = A + B # Clever addition of elements print(C)
Multiply two matrices
To multiply two matrices , Use dot() Method .
Be careful : For array multiplication ( Multiplication of the corresponding elements of two arrays ), Not matrix multiplication .
import numpy as np A = np.array([[3, 6, 7], [5, -3, 0]])B = np.array([[1, 1], [2, 1], [3, -3]])C = A.dot(B)print(C)
Matrix transposition
Use numpy.transpose Calculate the transpose of the matrix .
import numpy as np A = np.array([[1, 1], [2, 1], [3, -3]])print(A.transpose())
notes :
NumPy Make your task easier .
Like a list , You can use indexes to access matrix elements . Let's start from one dimension NumPy Array start .
import numpy as npA = np.array([2, 4, 6, 8, 10]) print("A[0] =", A[0]) # First element print("A[2] =", A[2]) # Third element print("A[-1] =", A[-1]) # Last element
When running the program , Output is :
Now? , Let's see how to access a two-dimensional array ( It's basically a matrix ) The elements of .
import numpy as np A = np.array([[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]]) # First element of first rowprint("A[0][0] =", A[0][0]) # Third element of second rowprint("A[1][2] =", A[1][2]) # Last element of last rowprint("A[-1][-1] =", A[-1][-1])
When you run a program , The output will be :
import numpy as np A = np.array([[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]]) print("A[0] =", A[0]) # First Rowprint("A[2] =", A[2]) # Third Rowprint("A[-1] =", A[-1]) # Last Row (3rd row in this case)
When you run a program , The output will be :
import numpy as np A = np.array([[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]]) print("A[:,0] =",A[:,0]) # First Columnprint("A[:,3] =", A[:,3]) # Fourth Columnprint("A[:,-1] =", A[:,-1]) # Last Column (4th column in this case)
When you run a program , The output will be :
notes :
Use NumPy( Instead of nested lists ) You can handle matrices more easily , And it doesn't even involve basic knowledge . It is suggested to study in detail NumPy software package , Especially when trying to Python For Data Science / When analyzing .
This article is based on Python Basics , Matrix and NumPy Array , highlighted NumPy Array , How to install NumPy modular , How to create a NumPy Two ways of arrays .
Through case analysis , Demonstration of code , Display of operation effect diagram , Use Python Language , Can let the reader better understand .
According to the content of the article , Realize it by yourself . Sometimes it's easy to see someone else do it , But when it comes to doing it yourself , There will always be all kinds of problems , Don't hold your eyes high or your hands low , Do it frequently , Can understand more deeply .
The code is simple , I hope it will help you with your study .
------------------- Send the book -------------------
advertisement
author : Zhang Ying
Dangdang
Buy
Activity rules
Participate in the way : Reply in the official account background “ Send the book ” keyword , Remember it was “ Send the book ” Two words ha , You can participate in this book delivery activity .
Publication time :2021 year 5 month 26 Number ( Wednesday ) evening 20 spot
Collection matters : Please add wechat as a little assistant to your friends : pycharm1314, Or scan the code to add friends . Everyone who adds a little assistant can get one Python Learning materials , More importantly, it's easy to get in touch with .
matters needing attention : Be sure to pay attention to wechat , If you are lucky, fill in the receiving address in the small program as soon as possible 、 Book information . Didn't fill in the receiving information within one day , The book delivery quota will be transferred to other people , Welcome to participate
------------------- End -------------------
Excellent articles in the past are recommended :