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 Python numpy view and copy

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the Python numpy view and copy how to understand, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, do not say much, follow the editor to have a look.

Foreword:

The memory structure of ndarray is mainly divided into two parts:

Metdata: stores array type dtype, array dimension ndim, number of dimensions shape, dimension spacing strides, etc.

Raw bata: data for storing raw data

Metdata contains information about arrays, which can help us quickly index and interpret specified data in array ndarray.

In addition to indexing the array, the original data of the array is similar to the previous "copy" operation.

As we all know, in Python, we should have a certain impression of deep and shallow copy, but in numpy, it has been replaced by the concept of "view" and "copy".

I believe you, like me, have doubts about this. Why do 100, 000 questions come to mind, "what is the view?" "what is a copy?"

So, let's learn about the relatively novel concept view and copy in the numpy module, Let's go~.

1. Brief explanation

When we were learning about Python assignments and deep and shallow copies, we compared the address id () of the two objects in code addition to see if they were consistent.

In the same way, you can compare two array addresses in numpy to see if they are the same.

At the same time, many fields are provided in the numpy array object ndarray, which makes it easy for us to further view the differences within the array.

Ndarray.flags: view array storage policies, read and write permissions, objects, etc.

C_CONTIGUOUS (C) Line priority Storage

F_CONTIGUOUS column priority storage

OWNDATA data owner

WRITEABLE authoring permission

ALIGNED data elements are aligned with hardware pointers

The WRITEBACKIFCOPY array is a copy of other arrays

UPDATEIFCOPY has been deprecated

Note: flags-related attribute names can be called separately, such as flags.writeable

Ndarray.base: check to see if the elements in the array come from other arrays

Ndarray.nbytes: view the number of bytes occupied by the data in the array

Getsizeof (item): view the memory space occupied by the array

After introducing the above indicators, let's try it out:

> import numpy as np > a = np.array ([1Jing 2 Mei 3 Ji 4]) > print (a [1:3]) [2 3] > print (a [[1 Mei 2]]) [2 3] >

Look at the memory addresses of a [1:3] and a [[1jue 2]], they are in different locations, a [[1d2]] means a deep copy (copy), and a [1:3] is the original array a reference (view).

> print (id (a [1:3])) 2247482965008 > print (id (a [[1mem2]])) 2247482964928

Looking at the ndarray.owndata property, it is found that the a [1:3] data comes from the an array, and a [[1pr 2]] belongs to its own data.

> print (a.flags.owndata) True > print (a [1:3] .recips.owndata) False > print (a [[1mer2]] .consums.owndata) True

We are looking at the ndarray.base property, and if we do confirm the result of using the flags.owndata query, a [1:3] is not the data owner, but the data source array a

A [[1J2]] is the data owner, the data source itself (None)

> print (a [[1d2]] .base) None > print (a [1:3] .base) [1234] 2. View View concept

From the simple example above, we can see that a [1:3] is not the owner of the data, but that the data comes from a reference to array a (shallow copy).

Therefore, we should have a basic understanding of the view and see how the official describes the view.

No copy at All . Simple assignments make no copy of objects or their data.

The view is a reference copy of the original array and shares the data of the original array.

View application

Views are widely used in numpy, and they generally produce two kinds of scenarios:

When referencing the original array

When you have no data and share data with the original array

> > import numpy as np > a = np.array > > b = a > > b is aTrue > id (a) 2247207679680 > id (b) 2247207679680 >

We can see that an and b share the same data space.

Return view results in numpy modules such as indexing, slicing, functions view (), reshape (), etc.

> arr = np.arange (10) > arr_view = arr.view () > arr.shape = (2mem5) > arr_reshape = arr.reshape (5meme 2) # ndarray.base attribute > print (arr.base) None > print (arr_view.base) [[01234] [56789]] > print (arr_reshape.base) [[01234] [56789]] # ndarray.flags.owndata attribute > > print (arr.flags.owndata) True > > print (arr_view.flags.owndata) False > print (arr_reshape.flags.owndata) False >

View advantage

Objects that can be created by views in numpy save memory space and do not need to be copied, thus improving query speed

In the view, if the created object modifies the data, the original data is also modified.

3. Replica concept

The copy is a complete copy of the original array (the data address will also be copied), is completely independent of the original array, and does not share data with the original array relative to the "deep copy".

Also intercept the official website and describe the copy:

Deep Copy The copy method makes a complete copy of the array and its data

When you change the data element value of the copy, although the copy is independent of the original array, the element value in the original array does not change.

Copy application

When slicing operation

When you need to be independent of the original array data

For the implementation of the copy, we can directly use the ndarray.copy () method to make a deep copy of the original array

B = np.array (] c = b.copy () c [1] = 8print ("b:", b) print ("c:", c) print ("c is b:", c is b) # View ndarray.base attribute å print ("b.base:", b.base) print ("c.base:", c.base) # View ndarray.flags.owndataprint ("b.flags.owndata:", b.flags.owndata) print ("c.flags.owndata:", c.flags.owndata)

We looked at the important conceptual views and copies in the numpy module.

View, which is equivalent to a shallow copy, shares data with the original array.

Copy, which is equivalent to a deep copy, independent of the original array data

We can further analyze the difference through the memory address id () method and with the help of ndarray.base and ndarray.flags.

The above is how to understand the Python numpy view and copy. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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