current position:Home>[Python] numpy notes
[Python] numpy notes
2022-02-01 18:23:27 【cout0】
「 This is my participation 11 The fourth of the yuegengwen challenge 29 God , Check out the activity details :2021 One last more challenge 」.
numpy brief introduction
What is? numpy
NumPy yes Python The basic package of scientific computing in . It's a Python library , Provide multidimensional array objects , Various derived objects ( Such as mask array and matrix ), And various methods for fast array operation API, It includes == mathematics 、 Logic 、 Shape operation 、 Sort 、 choice 、 Input and output 、 Discrete Fourier transform 、 Basic linear algebra , Basic statistical operation and random simulation, etc ==.
numpy Application
NumPy Usually with SciPy(Scientific Python) and Matplotlib( Drawing library ) Use it together , This combination is widely used in == replace MatLab==, It's a powerful scientific computing environment , It helps us to get through Python Study == Data science or machine learning ==.
ndarray
numpy Of ndarray object , Alias called array. It should be noted that ,==numpy.array≠array.array==, The latter is python Objects of the standard library , It can only handle one-dimensional array objects and has less functions .
- ndarray Properties of
Property name | describe |
---|---|
ndarray.ndim | n dimension, namely array The number of dimensions of , Also known as rank |
ndarray.shape | That is, the dimensions of an array , Returns an integer tuple , Such as two-dimensional array (m,n) |
ndarray.size | Number of array elements , amount to shape Element grades |
ndarray.dtype | An object that describes the type of element in an array |
- Example
>>> import numpy as np
>>> num=np.array([[1,2,3],[4,5,6]])
>>> num.ndim
2
>>> num.shape
(2, 3)
>>> num.size
6
Copy code
zeros
Used to create a 0 matrix .
>>> s=np.zeros((5,2))
>>> s
array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])
Copy code
ones
Be similar to zeros, Created is full 1 matrix .
arrange
Used to create a one-dimensional array
- ndarray.arrange(first,end,len), Interval is [first,end], step len.
- ndarray.arrange(num), The array created is 0,1,2……num-1.
>>> np.arange(0,5,2)
array([0, 2, 4])
>>> np.arange(5)
array([0, 1, 2, 3, 4])
Copy code
reshape
Used to change the dimension of the matrix .
>>> num=num.reshape(2,3)
>>> num
array([[1, 2, 3],
[4, 5, 6]])
>>> num=num.reshape(6)
>>> num
array([1, 2, 3, 4, 5, 6])
Copy code
max,min and sum
>>> print(a.dot(b))
[[10 13]
[28 40]]
>>> print(a)
[[0 1 2]
[3 4 5]]
>>> a.sum()
15
>>> a.min()
0
>>> a.max()
5
Copy code
Matrix multiplication
array in ,==* Represents an operation within an element , Not matrix multiplication , For multiplication @ or dot Function substitution ==
>>> a=np.arange(6).reshape(2,3)
>>> b=np.arange(6).reshape(3,2)
>>> print(a)
[[0 1 2]
[3 4 5]]
>>> print(2*a)
[[ 0 2 4]
[ 6 8 10]]
>>> print([email protected])
[[10 13]
[28 40]]
>>> print(a.dot(b))
[[10 13]
[28 40]]
Copy code
axis Parameters
Specify the axis of the operation
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>>
>>> b.sum(axis=0) # sum of each column
array([12, 15, 18, 21])
>>>
>>> b.min(axis=1) # min of each row
array([0, 4, 8])
Copy code
Transpose matrix
The common method is to access array Of T attribute .
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> a.T
array([[0, 3],
[1, 4],
[2, 5]])
Copy code
Unit matrix
- It can be used identity or eye
>>> np.identity(2)
array([[1., 0.],
[0., 1.]])
>>> np.eye(2)
array([[1., 0.],
[0., 1.]])
Copy code
- You can also use == Diagonal matrix == Of diag Generative method
>>> np.diag([1]*2)
array([[1, 0],
[0, 1]])
Copy code
Matrix stacking
- hstack Lateral stacking
- vstack Vertical stacking
>>> a=np.array([0,1,2])
>>> b=np.array([3,4,5])
>>> np.hstack((a,b))
array([0, 1, 2, 3, 4, 5])
>>> np.vstack((a,b))
array([[0, 1, 2],
[3, 4, 5]])
Copy code
Matrix partition
Divide the matrix equally
- hsplit Horizontal segmentation
- vsplit Vertical segmentation
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> np.hsplit(a,3)
[array([[0],
[3]]), array([[1],
[4]]), array([[2],
[5]])]
>>> np.vsplit(a,2)
[array([[0, 1, 2]]), array([[3, 4, 5]])]
Copy code
linalg
linalg.det
Calculate the determinant of a matrix :numpy.linalg.det(a)
solve
numpy.linalg.solve(a,b) For solving linear matrix equations
>>> b=np.array([4]).reshape(1,1)
>>> a=np.array([2]).reshape(1,1)
>>> np.linalg.solve(a,b)
array([[2.]])
Copy code
inv
numpy.linalg.inv(a) Find the inverse of a matrix
>>> a=([[1,2],[2,1]])
>>> b=np.linalg.inv(a)
>>> [email protected]
array([[1., 0.],
[0., 1.]])
Copy code
copyright notice
author[cout0],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011823250084.html
The sidebar is recommended
- Python learning notes - the fifth bullet * class & object oriented
- Python learning notes - the fourth bullet IO operation
- Python crawler actual combat: crawl all the pictures in the answer
- Quick reference manual of common regular expressions, necessary for Python text processing
- [Python] the characteristics of dictionaries and collections and the hash table behind them
- Python crawler - fund information storage
- Python crawler actual combat, pyteseract module, python realizes the visualization of boos direct employment & hook post data
- Pit filling summary: Python memory leak troubleshooting tips
- Python code reading (Chapter 61): delaying function calls
- Through the for loop, compare the differences between Python and Ruby Programming ideas
guess what you like
-
leetcode 1606. Find Servers That Handled Most Number of Requests(python)
-
leetcode 1611. Minimum One Bit Operations to Make Integers Zero(python)
-
06python learning notes - reading external text data
-
[Python] functions, higher-order functions, anonymous functions and function attributes
-
Python Networkx practice social network visualization
-
Data analysis starts from scratch, and pandas reads and writes CSV data
-
Python review (format string)
-
[pandas learning notes 01] powerful tool set for analyzing structured data
-
leetcode 147. Insertion Sort List(python)
-
apache2. 4 + windows deployment Django (multi site)
Random recommended
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
- Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
- Python series -- web crawler
- Plotly + pandas + sklearn: shoot the first shot of kaggle
- How to learn Python systematically?
- Analysis on several implementations of Python crawler data De duplication
- leetcode 1616. Split Two Strings to Make Palindrome (python)
- Python Matplotlib drawing violin diagram
- Python crawls a large number of beautiful pictures with 10 lines of code
- [tool] integrated use of firebase push function in Python project
- How to use Python to statistically analyze access logs?
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
- Python: simple single player strange game (text)
- Daily python, chapter 27, Django template
- TCP / UDP communication based on Python socket
- Use of pandas timestamp index
- leetcode 148. Sort List(python)
- Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
- [HTB] cap (datagram analysis, setuid capability: Python)
- How IOS developers learn Python Programming 23 - Supplement 2
- How to automatically identify n + 1 queries in Django applications (2)?
- Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis