In this chapter, I will talk about how to learn python and teach you how to do a simple small project.
Linux founder Linus once said:
Read The Fucking Source Code!
What is learning?
We sometimes feel that artificial intelligence is very mentally retarded. Why do we feel that way? Because the artificial intelligence which you use can't learn.
Learning is to acquire knowledge from the world, acquire knowledge autonomously and spontaneously.
If you have a question today, it is:
>>>import numpyTraceback (most recent call last): File "<pyshell>", line 1,in<module>import numpyModuleNotFoundError: No module named 'numpy'
How would you solve it? Here are a few options for you:
python is really hard, I can't learn
what the fxxk is this?
Google search: ModuleNotFoundError: No module named'numpy'
If you choose the last one, congratulations, you know how to learn. The characteristic of learning is to continuously acquire knowledge, and the key to learning is to know how to effectively acquire knowledge.
Here is a website for everyone: pypi, through this website you can get information and installation methods of various libraries in python.
You will find that through this command: pip install numpy, we can install numpy.
So how do we use this command? Open your windows and search for cmd in the lower left corner.
Then you will open an interface like this:
Then enter pip install numpy in it. After that, you will find that the progress bar keeps moving, and then the installation is complete.
pipinstallnumpy
Now you try the previous commands again, and you are all set.
>>>import numpy>>>
In order to run today's program, we need to install another library: matplotlib. This library is modeled after matlab's api. It can easily draw function images. We enter the following commands into CMD:
pipinstallmatplotlib
Try to plot in python
The program to be tried today is a program for drawing a three-dimensional quadratic function. The code is as follows. You can create a new python script called 3D drawing.py, and then paste the following code and run it to see how it works:
from matplotlib import pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax =Axes3D( fig, auto_add_to_figure =False )fig.add_axes(ax)x = np.arange( -4, 4, 0.1 )y = np.arange( -4, 4, 0.1 )x, y = np.meshgrid( x, y )z = x**2+ y**2ax.plot_surface( x, y, z, rstride =1, cstride =1, cmap ='rainbow', )plt.show()
Let's take a look at what is being done at each step of this program:
# Introduce necessary packages (library functions)from matplotlib import pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D# Create a 3D canvasfig = plt.figure()ax =Axes3D( fig, auto_add_to_figure =False )fig.add_axes(ax)# Create xy axisx = np.arange( -4, 4, 0.1 )y = np.arange( -4, 4, 0.1 )# Create xy planex_2d, y_2d = np.meshgrid( x, y )# Create the data for the z coordinatez = x_2d**2+ y_2d**2# Draw three-dimensional graphicsax.plot_surface( x_2d, y_2d, z, rstride =1, cstride =1, cmap ='rainbow', )# Project the graphics in the internal memory and video memory onto the screen.plt.show()
If we want to see what the following sentence accomplish, then what should we do?
x = np.arange( -4, 4, 0.1 )
We close the graphical interface that pops up before, so that the interpreter script can input content. At this time, the interpreter script should output the following content:
>>>
Enter in:
>>> x
Then press the Enter key, the interface should output the following content:
In the previous program, the step size I set was too small, so you will see a dense pile of content. In order to avoid flashing the screen, I changed the previous program. You need to copy and paste the following program into the script file (delete all previous program codes first), then run (or press F5 shortcut key to run):
# Introduce necessary packages (library functions)from matplotlib import pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D# Create a 3D canvasfig = plt.figure()ax =Axes3D( fig, auto_add_to_figure =False )fig.add_axes(ax)# Create xy axisx = np.arange( -4, 4, 1 )y = np.arange( -4, 4, 1 )# Create xy planex_2d, y_2d = np.meshgrid( x, y )# Create the data for the z coordinatez = x_2d**2+ y_2d**2# Draw three-dimensional graphicsax.plot_surface( x_2d, y_2d, z, rstride =1, cstride =1, cmap ='rainbow', )# Project the graphics in the internal memory and video memory onto the screen.plt.show()
Then close this interface, and then enter the variables you want to observe in the interpreter window:
>>> xarray([-4, -3, -2, -1, 0, 1, 2, 3])>>>
You will find: x = np.arange( -4, 4, 1) means to create an arithmetic sequence from -4 to 4 (not including 4), with a step length of 1.
We can also observe y:
>>> yarray([-4, -3, -2, -1, 0, 1, 2, 3])>>>
So what does x_2d, y_2d = np.meshgrid( x, y) mean? We are using the same method to interactively observe:
If we put the previous two figures together, you will find how python's matplotlib package draws three-dimensional function images:
# Introduce necessary packages (library functions)from matplotlib import pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D# Create a 3D canvasfig = plt.figure()ax =Axes3D( fig, auto_add_to_figure =False )fig.add_axes(ax)# Create xy axisx = np.arange( -4, 4, 1 )y = np.arange( -4, 4, 1 )# Create xy planex_2d, y_2d = np.meshgrid( x, y )# Create the data for the z coordinatez = x_2d**2+ y_2d**2# Draw the lattice planeax.scatter(x_2d, y_2d, np.zeros( (len(x_2d), len(x_2d)) ))# Draw three-dimensional graphicsax.plot_surface( x_2d, y_2d, z, rstride =1, cstride =1, cmap ='rainbow', )# Project the graphics in the internal memory and video memory onto the screen.plt.show()
# Introduce necessary packages (library functions)from matplotlib import pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D# Create a 3D canvasfig = plt.figure()ax =Axes3D( fig, auto_add_to_figure =False )fig.add_axes(ax)# Create xy axisx = np.arange( -4, 4, 0.5 )y = np.arange( -4, 4, 0.5 )# Create xy planex_2d, y_2d = np.meshgrid( x, y )# Create the data for the z coordinatez = x_2d**2+ y_2d**2# Draw the lattice planeax.scatter(x_2d, y_2d, np.zeros( (len(x_2d), len(x_2d)) ))# Draw three-dimensional graphicsax.plot_surface( x_2d, y_2d, z, rstride =1, cstride =1, cmap ='rainbow', )# Project the graphics in the internal memory and video memory onto the screen.plt.show()
So the way to draw points in matplotlib is to enumerate all possible points, and then calculate and draw them one by one. Is that mysterious? Absolutely not!
Python program debugging method
What should you do when your python is wrong or you encounter a python program that you can't understand?
It's very simple, just like we did step by step debugging, and soon we will be able to figure out the meaning of each variable.
When we encounter a huge array, we try to reduce it, and when we encounter a mathematical problem that is difficult to understand, we try to visualize it. If we encounter a function, we try to disassemble it, and the problem will be solved!