<>python numpy Module detailed explanation and application cases

Introduction to blog

*
This blog introduces 3 It's a common one numpy method (array,linspace,arange), At the same time, each method will have an application example , Through the application of examples to better understand and use these methods
* of course numpy There are also some mathematical operations , This is the most familiar one :
sin(), cos(), log(),tan(),exp(),arctan(),arcsin(),arccos(),cot() wait
If you know mathematics, you must know those methods , So I will not introduce it here
<> one , install numpy

windows system :
1.windows + R Open the console ;
2.pip install numpy.
The results are as follows :

<> two , The most common method

<>1,array method

1.1 array(numpy.array) function
Generate array ( It can be a one-dimensional array , Two dimensional array , Arrays of even higher dimensions )
The difference between array and list :
Arrays must be of the same data type ;
The list can be of different data types .
In other ways, the two are similar .
give an example :
import numpy as np """ The most common operation in the industry , Declare an alias np """ array_0 = np.array([0, 1, 2, 3, 4]
) # One dimensional array array_1 = np.array([[0, 1, 2, 3],[4, 5, 6, 7]]) # Two dimensional array array_2 = np.
array([[[0, 1, 2],[3, 4, 5]],[[6, 7, 8],[9, 10, 11]]]) # 3D array # More dimensions are rarely used
""" Notice here : Arrays must be made up of elements of the same data type , therefore , When the data types of elements are different , There will be forced replacement !! The priority of quasi exchange is : character string > Floating point number > integer
For example, the following example : """ array_3 = np.array([1, 2.23, 4, 5]) # Is cast to floating point array_4 = np.array(
[1, 2.23, 4, 5, 'hello world !']) # Is cast to a string # Print results print(array_0) print(
array_1) print(array_2) print(array_3) print(array_4)
The results of the operation are as follows (output):
[0 1 2 3 4] # array_0 [[0 1 2 3] [4 5 6 7]] # array_1 [[[ 0 1 2] [ 3 4 5]] [[ 6
7 8] [ 9 10 11]]] # array_2 [1. 2.23 4. 5. ] # array_3 ['1' '2.23' '4' '5'
'hello world !'] # array_4
In short, that is to say ,numpy.array() Is to generate an array , Its type is :<class ‘numpy.ndarray’>
( Can pass type() Method to view ).
in addition , Subtract a number from an array , for example :
array_0 = array_0 - 100 # One hundred for each element
It means that every element in the array is subtracted 100!
1.2 array Application examples of the method ( Can handle images )
import numpy as np import matplotlib.pyplot as plt #
This is a special drawing module , The introduction of this module will be described in another blog post """ It is also a commonly used nickname in the industry """ # Convert the picture to an array ( 3D array ) array_img =
plt.imread('./ Picture name ') """ for example : array_img =
plt.imread('./0065ErDtgy1geehlwnfb6j30p018g435.jpg') """ # It is equivalent to entering the location of an image file #
The result is a three-dimensional array # adopt print see print(array_img) # print(type(array_img)) # Use the drawing module to show it
plt.imshow(array_img) # Implementation of an array minus a number : plt.imshow(array_img - 100) # It's OK to add a number plt
.imshow(array_img + 200)
The results of the operation are as follows (output):
I have already used a picture here , The original picture is as follows , For the effect of running, please see below :

Picture here :
1, Corresponding to array_img : ( No change )

2, Corresponding to array_img - 100 :( I made my lovely little sister ugly , It's my fault , Wuwuwu ~~~)

3, Corresponding to array_img + 200 :( I made my little sister ugly again , Wuwuwu ~~~, I'm sorry first ~~, But it seems better than the previous one ~)

So , In fact, those software is to take pictures after the transformation into an array , Then through a specific algorithm to achieve beauty , Slimming and other functions .
such as :
For the commonly used thin face function , We can :
1, photograph ;
2, Face recognition ;
3, Turn the whole picture into an array ;
4, A specific algorithm is used to deal with the array region corresponding to the face ;
5, last , Display the processed image ( Namely , Picture after face thinning ).
( In this case, we don't need to implement this kind of complex function , Those who are interested can explore it by themselves ~)

<>2,linspace method

2.1 linspace Function of method
linspace Method is used to generate a one-dimensional array , The concrete implementation is the interval from the start position to the end position , Produce a specified number of numbers evenly , And these numbers form a one-dimensional array .
import numpy as np array_0 = np.linspace(-10, 20, 30) # The first parameter : Starting position # The second parameter : It's over
# The third parameter : The size of the resulting one-dimensional array , That is, the number of digits specified print(array_0)
Output results :( Equivalent to arithmetic sequence !!)
[-10. -8.96551724 -7.93103448 -6.89655172 -5.86206897 -4.82758621 -3.79310345 -
2.75862069 -1.72413793 -0.68965517 0.34482759 1.37931034 2.4137931 3.44827586
4.48275862 5.51724138 6.55172414 7.5862069 8.62068966 9.65517241 10.68965517
11.72413793 12.75862069 13.79310345 14.82758621 15.86206897 16.89655172
17.93103448 18.96551724 20. ]
( share 30 A number )
2.2 linspace Application examples of the method
Here is an example of drawing an image , Draw a three-dimensional curve
""" Draw a three-dimensional curve The curve drawn is an equidistant helix Isometric spiral drawing code and the results are as follows : """ import numpy as np import
matplotlib.pyplot as plt # Import library functions fig = plt.figure() # Establish a three-dimensional coordinate system ax1 = plt.axes(
projection='3d') # 3D drawing z = np.linspace(-5, 5, 50) # use linspace Method definition z coordinate """
from -5 reach 5 The interval of , Equally divided into 50 Point drawing is carried out for each copy Point drawing !! """ x = 5 * np.sin(z) y = 5 * np.cos(z) # definition x And y Coordinates of
ax1.plot3D(x, y, z, 'gray') # Realize the drawing of three-dimensional drawing plt.show() # Displays the result of the drawn image
The results are shown below :

That's all linspace A brief introduction of the method .

<>3, arange method

3.1 arange Function of method
Generate a starting position , End at end position , An array with a specific step size as the difference value ( It is also equivalent to arithmetic sequence ):
import numpy as np array_0 = np.arange(-10, 10, 2) # use arange method # The first parameter : Starting position #
The second parameter : Termination position # The third parameter : step # Printing print(array_0)
The operation results are as follows :
[-10 -8 -6 -4 -2 0 2 4 6 8]
3.2 arange Application examples of the method
Here we draw an image of a surface in three dimensions , The specific code implementation is as follows :
""" The first four lines are the same as the previous example !! """ import numpy as np import matplotlib.pyplot as plt fig
= plt.figure() ax3 = plt.axes(projection='3d') # definition x coordinate ---> Generate array x = np.arange(-5,
5, 0.1) # definition y coordinate ---> Generate array y = np.arange(-5, 5, 0.1) # ad locum , Generate a two-dimensional array to store coordinates ( Storage of two dimensional coordinates
!!) # (meshgrid It's another way , I won't introduce it here , Interested can refer to the relevant information to learn .) X, Y = np.meshgrid(x, y) # Z
The function relation of Z = 10 * np.log(1000 - X ** 2 - Y ** 2) #
Display image ,cmap Parameters are used to style a drawing ,rainbow It's like a rainbow ax3.plot_surface(X, Y, Z, cmap='rainbow') #
Don't forget plt.show()!! plt.show()
The running results are shown as follows :

<> summary

in summary :
numpy(np) modular That's it ~~~

Of course, the above introduction is not comprehensive enough , Want to really master these modules , Or should we practice more by ourselves , Multi practice , In order to enhance their skills , This article is for your reference only ~
If you like, you can like it ( kiss you ), Of course, if you don't like it, I hope you don't step on it ~

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element