Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ndarray in Python numpy

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to understand ndarray in Python numpy. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Numpy module knowledge:

The numpy module is an open source third-party Python library, which is often used in the fields of science and engineering and is the core of the scientific Python and PyData ecosystem.

The numpy module is easy to learn and use, basically covering all groups from beginners to advanced scientific researchers.

Numpy module is not only used in matplotlib, but also in Pandas, Scipy, scikit-learn and other scientific Python.

The numpy module also contains multi-dimensional arrays and matrix data structures, which specifically provides ndarray with n-dimensional array objects and methods to manipulate them effectively.

The numpy module can also be used to perform various mathematical operations on arrays, to provide a library of advanced mathematical functions, and to operate arrays and matrices.

As we all know, array objects in the numpy module are its core function, so let's focus on learning about numpy arrays.

1. What is ndarray? Ndarray concept

Array is the core data structure of numpy module. An array is a network of values that contains information about the raw data, how to define the element, and how to interpret the element. We can index the element grid in a variety of ways.

Ndarray is the n-dimensional array type defined in the numpy module. Ndarray can describe a collection of project of the same type.

Same data type: all elements in the numpy array are of the same type, such as int32,float64, etc.

Same size memory space: each project occupies the same size memory block

Interpreted in the same way: 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

We can extract the python object representation from the array. For example, if the index is one of the numpy built-in array scalar types, we can easily manipulate complex data arrangements by using array scalars.

From the above internal structure of ndarray, we can see that ndarray is mainly composed of dtype, shape and stride.

Ndarray pointer to memory-mapped address-data object

Ndarray element interpretation image-dtype object

Ndarray the interval between elements of each dimension-strides object (tuple)

Ndarray's description of the number and size of each dimension-shape object (tuple)

The above four python objects can find the data at the specified location in ndarray by indexing.

At the same time, we also call np.array (). Flags to get byte order, read and write permissions and other information, we can know that the underlying ndarray is implemented by C and Fortran.

C_CONTIGUOUS: True F_CONTIGUOUS: False OWNDATA: True WRITEABLE: True ALIGNED: True WRITEBACKIFCOPY: False UPDATEIFCOPY: False2. Ndarray memory structure ndarray memory structure

We create a 2-dimensional array through the numpy.array method

Numpy.array (object, dtype = None, copy = True, order = None, subok = False, ndmin = 0) import numpy as npa = np.array ([1mai 2], [4jue 5], [7je 8]]) print ("dim:", a.ndim) print ("strides:", a.strides) print ("dtype:", a.dtype) print ("data:", a.data) print ("shape:", a.shape) print (a)

By calling the ndarray scalar object through the array object, you can know the ndarray dimension size, element type, interval and other information.

From the above figure, we can see that ndarray memory is mainly divided into two parts:

Raw data: a continuous block of a computer, stored in an array in C or Fortran.

Metdata: information about raw array data

3. Characteristics of ndarray vs listndarray

Ndarray requires all data to be of the same type.

Each data occupies the same space.

The data stored in the array is a continuous space

Characteristics of list

Can accommodate different data types

Only references to objects are stored in list, and specific objects are found through references.

The physical address of an object is not contiguous

Therefore, to sum up, the running efficiency of ndarray lookup data is faster than that of list, and the data stored by ndarray is a continuous section of space. Compared with the scattered physical addresses of list objects, ndarray saves more space than list.

This is how the ndarray in the Python numpy shared by the editor is understood. If you happen to have similar doubts, you might as well refer to the above analysis. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report