current position:Home>Numpy core syntax and code sorting summary!
Numpy core syntax and code sorting summary!
2022-05-15 07:44:28【Xiaobai learns vision】
Click on the above “ Xiaobai studies vision ”, Optional plus " Star standard " or “ Roof placement ”
Heavy dry goods , First time delivery
Numpy Summary
Numpy It's a use python Implementation of scientific computing extension library , Include :
A powerful N Dimensional array object Array;
More mature ( radio broadcast ) function library ;
For consolidation C/C++ and Fortran Code toolkit ;
Practical linear algebra 、 Fourier transform and random number generation function .numpy And sparse matrix operation package scipy With the use of more convenient .
NumPy(Numeric Python) Many advanced numerical programming tools are provided , Such as : Matrix data type 、 Vector processing , And a sophisticated computing library . Produced for rigorous digital processing . Used by many large financial companies , And the core scientific computing organization :Lawrence Livermore,NASA Use it to deal with some of the original use C++,Fortran or Matlab And so on .
This paper sorts out a Numpy A little meter reading , Sum up Numpy Common operations of , You can collect it. Take your time .
( You can click the big picture to view the picture ~)
1、 install Numpy
Can pass Pip perhaps Anaconda install Numpy:
$ pip install numpy
or
$ conda install numpy
2、 Basics
NumPy One of the most common features is NumPy Array : List and NumPy The main difference between arrays is functionality and speed .
Lists provide basic operations , but NumPy Added FTTs、 Convolution 、 Quick search 、 Basic statistics 、 linear algebra 、 Histogram, etc .
The most important difference between the two data science is Able to use NumPy Array for element level computation .
axis 0: Usually refers to the line
axis 1: Usually refers to the column
1. Place holder
give an example :
import numpy as np
# 1 dimensional
x = np.array([1,2,3])
# 2 dimensional
y = np.array([(1,2,3),(4,5,6)])
x = np.arange(3)
>>> array([0, 1, 2])
y = np.arange(3.0)
>>> array([ 0., 1., 2.])
x = np.arange(3,7)
>>> array([3, 4, 5, 6])
y = np.arange(3,7,2)
>>> array([3, 5])
2. Array attribute
3. Copy / Sort
give an example :
import numpy as np
# Sort sorts in ascending order
y = np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
y.sort()
print(y)
>>> [ 1 2 3 4 5 6 7 8 9 10]
4. Array manipulation routines
Add or subtract elements
give an example :
import numpy as np
# Append items to array
a = np.array([(1, 2, 3),(4, 5, 6)])
b = np.append(a, [(7, 8, 9)])
print(b)
>>> [1 2 3 4 5 6 7 8 9]
# Remove index 2 from previous array
print(np.delete(b, 2))
>>> [1 2 4 5 6 7 8 9]
Combining arrays
give an example :
import numpy as np
a = np.array([1, 3, 5])
b = np.array([2, 4, 6])
# Stack two arrays row-wise
print(np.vstack((a,b)))
>>> [[1 3 5]
[2 4 6]]
# Stack two arrays column-wise
print(np.hstack((a,b)))
>>> [1 3 5 2 4 6]
Split array
give an example :
# Split array into groups of ~3
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(np.array_split(a, 3))
>>> [array([1, 2, 3]), array([4, 5, 6]), array([7, 8])]
Array shape changes
operation
other
give an example :
# Find inverse of a given matrix
>>> np.linalg.inv([[3,1],[2,4]])
array([[ 0.4, -0.1],
[-0.2, 0.3]])
5. Mathematical calculation
operation
give an example :
# If a 1d array is added to a 2d array (or the other way), NumPy
# chooses the array with smaller dimension and adds it to the one
# with bigger dimension
a = np.array([1, 2, 3])
b = np.array([(1, 2, 3), (4, 5, 6)])
print(np.add(a, b))
>>> [[2 4 6]
[5 7 9]]
# Example of np.roots
# Consider a polynomial function (x-1)^2 = x^2 - 2*x + 1
# Whose roots are 1,1
>>> np.roots([1,-2,1])
array([1., 1.])
# Similarly x^2 - 4 = 0 has roots as x=±2
>>> np.roots([1,0,-4])
array([-2., 2.])
Compare
give an example :
# Using comparison operators will create boolean NumPy arrays
z = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
c = z < 6
print(c)
>>> [ True True True True True False False False False False]
Basic statistics
give an example :
# Statistics of an array
a = np.array([1, 1, 2, 5, 8, 10, 11, 12])
# Standard deviation
print(np.std(a))
>>> 4.2938910093294167
# Median
print(np.median(a))
>>> 6.5
more
6. Slices and subsets
give an example :
b = np.array([(1, 2, 3), (4, 5, 6)])
# The index *before* the comma refers to *rows*,
# the index *after* the comma refers to *columns*
print(b[0:1, 2])
>>> [3]
print(b[:len(b), 2])
>>> [3 6]
print(b[0, :])
>>> [1 2 3]
print(b[0, 2:])
>>> [3]
print(b[:, 0])
>>> [1 4]
c = np.array([(1, 2, 3), (4, 5, 6)])
d = c[1:2, 0:2]
print(d)
>>> [[4 5]]
Slice for example :
import numpy as np
a1 = np.arange(0, 6)
a2 = np.arange(10, 16)
a3 = np.arange(20, 26)
a4 = np.arange(30, 36)
a5 = np.arange(40, 46)
a6 = np.arange(50, 56)
a = np.vstack((a1, a2, a3, a4, a5, a6))
Generate matrix and slice diagram
7. Tips
Boolean index
# Index trick when working with two np-arrays
a = np.array([1,2,3,6,1,4,1])
b = np.array([5,6,7,8,3,1,2])
# Only saves a at index where b == 1
other_a = a[b == 1]
#Saves every spot in a except at index where b != 1
other_other_a = a[b != 1]
import numpy as np
x = np.array([4,6,8,1,2,6,9])
y = x > 5
print(x[y])
>>> [6 8 6 9]
# Even shorter
x = np.array([1, 2, 3, 4, 4, 35, 212, 5, 5, 6])
print(x[x < 5])
>>> [1 2 3 4 4]
download 1:OpenCV-Contrib Chinese version of extension module
stay 「 Xiaobai studies vision 」 Official account back office reply : Extension module Chinese course , You can download the first copy of the whole network OpenCV Extension module tutorial Chinese version , Cover expansion module installation 、SFM Algorithm 、 Stereo vision 、 Target tracking 、 Biological vision 、 Super resolution processing and other more than 20 chapters .
download 2:Python Visual combat project 52 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :Python Visual combat project , You can download, including image segmentation 、 Mask detection 、 Lane line detection 、 Vehicle count 、 Add Eyeliner 、 License plate recognition 、 Character recognition 、 Emotional tests 、 Text content extraction 、 Face recognition, etc 31 A visual combat project , Help fast school computer vision .
download 3:OpenCV Actual project 20 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :OpenCV Actual project 20 speak , You can download the 20 Based on OpenCV Realization 20 A real project , Realization OpenCV Learn advanced .
Communication group
Welcome to join the official account reader group to communicate with your colleagues , There are SLAM、 3 d visual 、 sensor 、 Autopilot 、 Computational photography 、 testing 、 Division 、 distinguish 、 Medical imaging 、GAN、 Wechat groups such as algorithm competition ( It will be subdivided gradually in the future ), Please scan the following micro signal clustering , remarks :” nickname + School / company + Research direction “, for example :” Zhang San + Shanghai Jiaotong University + Vision SLAM“. Please note... According to the format , Otherwise, it will not pass . After successful addition, they will be invited to relevant wechat groups according to the research direction . Please do not send ads in the group , Or you'll be invited out , Thanks for your understanding ~
copyright notice
author[Xiaobai learns vision],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102123000496.html
The sidebar is recommended
- C - no AC this summer
- Thread control - thread waiting, thread termination, thread separation
- Key points of acupuncture and moxibustion
- Module product and problem solution of Luogu p2260 [Tsinghua training 2012]
- Review points of Geodesy
- Summary of review points of Geodesy
- Arrangement of geodetic knowledge points
- Review key points of basic geodesy
- Luogu p2522 [haoi2011] problem B solution
- [app test] summary of test points
guess what you like
Version management tool - SVN
JDBC ~ resultset, use of resultsetmetadata, ORM idea, arbitrary field query of any table (JDBC Implementation)
This article takes you to understand can bus
Gear monthly update April
Gear monthly update April
Convert timestamp to formatted date JS
The time stamp shows how many minutes ago and how many days ago the JS was processed
[untitled]
Luogu p2216 [haoi2007] ideal square problem solution
Miscellaneous questions [2]
Random recommended
- Which securities company does qiniu school recommend? Is it safe to open an account
- Hyperstyle: complete face inversion using hypernetwork
- What activities are supported by the metauniverse to access reality at this stage?
- P2P swap OTC trading on qredo
- Google | coca: the contrast caption generator is the basic image text model
- SIGIR 2022 | Huawei reloop: self correcting training recommendation system
- Whether you want "melon seed face" or "national character face", the "face changing" technology of Zhejiang University video can be done with one click!
- Sorting of naacl2022 prompt related papers
- Servlet create project
- "Chinese version" Musk was overturned by the original: "if it's true, I want to see him"
- [network security] web security trends and core defense mechanisms
- [intensive reading] object detection series (10) FPN: introducing multi-scale with feature pyramid
- 007. ISCSI server chap bidirectional authentication configuration
- 2021-03-09
- plot_ Importance multi classification, sorting mismatch, image value not displayed
- [intensive reading] object detection series (XI) retinanet: the pinnacle of one stage detector
- How to install MFS environment for ECS
- [intensive reading] the beginning of object detection series (XII) cornernet: anchor free
- Open source sharing -- a record of students passing through time
- MOT:A Higher Order Metric for Evaluating Multi-object Tracking
- How to develop a distributed memory database (1)
- Reverse engineers reverse restore app and code, and localization is like this
- One line command teaches you how to export all the libraries in anaconda
- Bi tools are relatively big. Let's see which one is most suitable for you
- Read the history of database development
- Self cultivation of coder - batterymanager design
- Technology application of swift phantom type phantom in Apple source code learning
- Swiftui advanced skills: what is the use of the technology of swift phantom type phantom
- Swiftui advanced animation Encyclopedia of complex deformation animation is based on accelerate and vector arithmetic (tutorial includes source code)
- What problems remain unsolved in swiftui in 2022
- I'll set the route for fluent
- Flutter drawing process analysis and code practice
- Emoji language commonly used icon collection (interesting Emoji)
- 5.14 comprehensive case 2.0 - automatic induction door
- How to deploy redis service on k8s top?
- Importance of data warehouse specification
- Idea automatically generates serialization ID
- Why is it recommended not to use select * in MySQL?
- Let's talk about why redis needs to store two data structures for the same data type?
- Domain lateral move RDP delivery