程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python matrices and numpy arrays

編輯:Python

Hello everyone , I am a IT Sharers , It's called Pipi . Today I'd like to introduce matrix and NumPy Array .

One 、 What is a matrix ?

Use nested lists and NumPy Bag Python matrix . Matrix is a two-dimensional data structure , The numbers are arranged in rows and columns .

Two 、Python matrix

1. The list is treated as a matrix

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 :

2. How to use nested lists .

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 :

3、 ... and 、NumPy Array

1. What is? NumPy?

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 .

2. How to install NumPy?

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.

3. How to create a NumPy Array ?

There are several ways to create NumPy Array method .

3.1 Integers , An array of floating-point numbers and complex numbers

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 :

3.2 An array of zeros and ones

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)

Four 、 Matrix operations

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 .

5、 ... and 、 Case study

1. Access matrix elements

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 :

2. Access the rows of the matrix

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 :

3. Access the columns of the matrix

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 .

6、 ... and 、 summary

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

Python Web crawler framework Scrapy From entry to mastery

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 :

  • Windows Easy to build in the environment NodeJs The server
  • An article will take you to understand CSS Unit related knowledge
  • Here's what you want , Text bomber

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved