“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Jym: Hello everyone, we meet again. As the Spring Festival is approaching, we will continue to study with the strong feeling of returning home.

I wish you all success in the New Year

preface

We then continued our month-long study of the Matplotlib module, Python’s third party library for graphing.

How do you get and create the data you need to experiment with before drawing a chart?

During motplotlib learning, the numpy module was used the most.

The NUMpy module is called the Matplotlib module for plotting a chart companion.

XDM, Let’s go~ for matplotlib companion – Numpy module

1. Numpy profile

The Numpy module is a third party library in Python that supports scientific calculations on large arrays.

The NUMPY module provides many functions for fast manipulation of multi-dimensional array objects and matrices.

  • Mathematics, logic, shape manipulation
  • Sorting, selection, I/O
  • Basic linear algebra, basic statistical operations, random simulation
  • Broadcast, byte exchange

Numpy module for open source code, the official website has been updated to version 1.22.0.

Github source link, interested in its source brothers can kangkangha ~

  • Numpy application scenarios

    The NUMpy module is usually used in conjunction with matplotlib (drawing library).

    Data is computed before matplotlib draws diagrams.

  • Numpy module installed

    Since the Numpy module is the third library Python provides for data processing, you need to install it using PIP Install

    pip install numpy
    Copy the code
  • Numpy module used

    In the code, we need to use import to do the import

    import numpy as np
    Copy the code

2. Numpy characteristics

At the heart of the Numpy template is the Ndarray object, which encapsulates n-dimensional arrays of the same type and improves performance by leaving many operations to be performed in compiled code.

Numpy Array distinguishes itself from Python’s built-in list

  • Numpy arrays are created with a fixed size, and Python List lists are dynamically sized. If you want to change the size of ndarray, delete the original array and create a new one
  • The elements in a NUMpy array must be of the same data type and therefore of the same size in memory. Python Lists allow different types of elements to exist.
  • Numpy arrays perform advanced mathematical operations on large amounts of data more efficiently than Python lists
  • Numpy array is widely used in scientific calculation, and the mainstream scientific and mathematical software is based on this module.

3. Numpy common methods

Numpy module is good at scientific calculation of large amounts of data, so it not only provides multi-dimensional array objects, but also general function methods.

  • For now, this article will introduce the common methods in numpy module

    methods role
    numpy.array(item) Create an array object (can create an N-dimensional array)
    numpy.arange(num) Create an arithmetic array
    Numpy. Random. Randn (d0, d1,… ,dn) Create a random normal distribution
    numpy.dtype() Create custom data types
    Numpy. Random. Rand (d0, d1,… ,dn) Create data 0 to 1
    numpy.random.randint(low,high) Creates a random integer for the given range
    numpy.random.random_integers(low,high) Returns a random integer in the given range
    numpy.random.random(size) Randomly generate floating point numbers between 0 and 1
    numpy.random.choice(a,size) Generates a random number from a given one-dimensional array
    numpy.random.seed() Know the random data in advance
    numpy.linespcae() Creating a linear array
    numpy.sort() Sort array data

4. Test the cat

In this installment, we’ll use Numpy to create arrays, slicing, indexing, broadcasting, and more

Create a 3d array
arr = np.array([[0.0.0], [2.3.9], [1.2.3]])

# numpy is accessed by index or slice, arr[start:stop:step]

print(Slice a [1] : \ "n",arr[1:)# Broadcast function, two arrays for ➕

b = np.array([2.5.7])
print("arr+b:\n",arr+b)
Copy the code

conclusion

In this issue, we introduce the matplotlib mapping module companion – Numpy module. The array objects provided by the NUMpy module make multidimensional data math more efficient than Python’s built-in methods.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time