“This is the second day of my participation in the First Challenge 2022.
Review and
The NUMpy module is often referred to as the matplotlib module companion. Numpy can easily and quickly perform scientific calculations on a large amount of data and provide data for Matplotlib to draw diagrams.
The python Numpy module provides an overview of how to install, use, and use numpy.
-
The Numpy module is an open source third-party Python library commonly used in science and engineering, and is at the heart of the Scientific Python and PyData ecosystem.
-
Numpy module is easy to learn and easy to use, basically covering all groups from beginners to advanced scientific researchers.
-
In addition to the use of the numpy module in matplotlib, it is also used in scientific Python, such as Pandas, Scipy, and Scikit-learn.
-
The NUMpy module also contains multidimensional array and matrix data structures, which specifically provide NDARray with n-dimensional array objects, and methods for efficiently manipulating them.
-
The NUMpy module can also be used to perform various mathematical operations on arrays, providing a library of advanced mathematical functions, and manipulation of arrays and matrices.
As we all know, numpy module array object is its core function, we focus on numpy array content, Let’s go~~
What is NDARray?
-
Ndarray concept
Array is the core data structure of NUMPY module. Arrays are networks of values that contain raw data, information about how elements are defined, and how they are interpreted. We can index the element grid in various ways.
Ndarray is an n-dimensional array type defined in the NUMpy module. Ndarray can describe a collection of projects of the same type.
- Same data type: all elements in a numpy array are of the same type, such as INT32, float64, etc
- Same size memory space: Each project occupies the same size memory block
- Each project is specified by the object data type, one of which is associated with each array
- Project can be indexed by N integers
-
Ndarray internal relations
From arrays we can extract representations of Python objects such as indexes, which are one of numpy’s built-in array scalar types. Array scalars can be used to easily manipulate complex arrays of data.
From the above internal structure of NDARray, we can see that NDARray is mainly composed of DType, shape and stride
- Ndarray pointer -data object to memory mapped address
- The ndarray element interprets the image-dtype object
- Ndarray – Strides for the interval between elements for each dimension
- Ndarray description of the number and size of each dimension -shape object (tuple)
These four Python objects can be indexed in NDARRay to find data at a specified location.
Ndarray is implemented in C and Fortran by calling np.array(). Flags to obtain the byte order and read/write permissions.
C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False Copy the code
2. Ndarray memory structure
-
Ndarray memory structure
We create a 2-dimensional array using the numpy.array method
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0) Copy the code
import numpy as np a = np.array([[1.2], [4.5], [7.8]]) print("dim:",a.ndim) print("strides:",a.strides) print("dtype:",a.dtype) print("data:",a.data) print("shape:",a.shape) print(a) Copy the code
An Array object calls an Ndarray scalar object to get information about ndarray dimensions, element types, and intervals
From the above figure, we can know that Ndarray memory is mainly divided into two parts:
- Raw data: AN array of contiguous blocks stored in C or Fortran on a computer
- Metdata: Information about raw array data
4. ndarray vs list
-
Ndarray characteristics
- Ndarray requires that all data be of the same type
- Each data takes the same amount of space
- The data stored in an array is a contiguous space
-
Characteristics of the list
- Can accommodate different data types
- The list stores only references to objects, which are then used to find specific objects
- The physical addresses of objects are not contiguous
Therefore, to sum up, ndarray searches data faster than List, and nDARray stores data in a continuous segment of space. Compared with list, ndarray saves more space than List because the physical addresses of list objects are scattered.
conclusion
In this issue, we will learn the memory principle of nDARray array object, which is one of the core of NUMpy module, and compare it with Python List.
All project elements in NDARray are of the same type and have contiguous storage space.
It is much better than Python lists for finding data, storing data, calculating data, etc.
That’s the content of this episode, welcome you to like and comment, we will see you next time ~ ~