One article will teach you how to use Python numerical calculation Numpy package proficiently

For friends who use Python, there is an open source package that will definitely be used in ordinary numerical calculations. Yes, it is the famous Numpy package, called Numberical Python for its full name, which is an open source numerical calculation extension of Python. A large number of mathematical function libraries are integrated in the Numpy package, and you can use them at will, which is extremely convenient!

Okay, let's talk about this Numpy package!

In Python, if the use of a third-party package is involved, the first thing you need to do is of course to import this package! Let's take a look at how the Numpy package is imported when it is used!

How to import Numpy packages

In the same way as importing other packages or modules in Python, the Numpy package also uses the import statement to import. Its import format is as follows:

import numpy as np

The following as means to give numpy an alias for the convenience of writing the program after importing, so writing np directly in the program refers to numpy!

Okay, let's talk about some basic methods commonly used in the numpy package!

First, Numpy can convert a list to an array

The program we wrote above is to call the numpy package to convert the list into an array. Okay, let's run it and see what the result is:

Next, let's take a look at the usage of several basic functions in the numpy package!

Usage of arange() function in numpy package

First look at the syntax format of the arange() function:

numpy.arange(start, stop, step)

Python learning exchange group , welcome all friends to exchange and learn.

Have you seen it? The arange() function has three parameters. Let’s talk about them separately. First, the parameter start represents the starting value of the array. The stop parameter, as the name implies, is the ending value of the array, but remember that it does not include the stop value. By itself, the parameter step is the interval value between the arrays!

In other words, the arange() function is to return an array containing the start value and not the end value with an interval of step! Of course, not all of the three parameters in the arange() function are required, and can have default values!

Well, let's take a "chestnut" to understand:

After running the above program, the output is as follows:

Usage of linspace() function in numpy package

Also first take a look at the syntax format of the linspace() function:

matplotlib.linspace(start, stop, num=10, endpoint=True)

The function of the linspace() function is to obtain an array that contains both the starting value and the ending value, and the length between these data is the same. The parameter num represents the length of the array, and if the endpoint parameter is assigned a value of True, it represents the default Include the termination value in the array!

Okay, let's give a "chestnut":

Run the above program to see the result:

Usage of built-in functions in numpy package

There are many built-in functions in the numpy package, such as sine sin(), cosine cos(), exponent exp(), etc. Below, let’s pick a few to see the usage. The usage of other built-in functions is the same. Okay, let's give a "chestnut" below:

Okay, run it to see the result:

Finally, let's take a look at the usage of random functions in the numpy package!

Usage of random package in numpy package

First of all, the random package is used to generate various types of data samples, and it is very convenient to use. Let's take a look at how to use the random package:

First of all, if you want to use the random package, you need to import it first, because the random package is built into the numpy package. Therefore, after importing numpy, you can call the random package, as shown below:

np.random.rand()

np.random.randn()

At this point, some friends may ask, what is the difference between the above rand() and randn() functions? Well, let’s talk about it first. The rand() function is used to generate an array. For example, rand(10) means to generate an array of length 10, and the elements of this array are all 0~1 Between; and the randn() function generates a standard normal distribution array. For example, randn(10) means to generate an array of 10 elements, and this array conforms to the normal distribution!

Well, in order to impress everyone, let's give a "chestnut":

Run it to see the result:

Well, we have already introduced the use of several types of functions in the numpy package above. In fact, the numpy package is very, very powerful, and contains many, many, many functions. It is impossible for us to talk about them all. The usage of the several types of functions discussed will be commonly used in Python data visualization. This is why we chose to introduce these functions. Of course, as long as you have mastered the usage of these functions, the use of functions in other numpy packages Of course it is similar, just look at the syntax format of a specific function and you can use it correctly!

Well, the several numpy functions we introduced this time are also to answer some of the questions of some of the functions in the numpy package that some friends call during matplotlib library data visualization. OK, let’s talk about it today, see you next time Oh! [Goodbye]

Guess you like

Origin blog.csdn.net/Python_xiaobang/article/details/112365569