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

What is the difference between different indexing methods of numpy array

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

Share

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

Most people do not understand the knowledge points of this article, "what are the differences in the different indexing methods of the numpy array?" so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what is the difference between the different indexing methods of the numpy array?"

Numpy arrays are generally indexed in three ways:

Basic index and slicing

Boolean index

Magic index

All three methods can intercept part of the data in the original array, but they are essentially different when they are used, especially when dealing with a large amount of data, not understanding the nature of the index will lead to inaccurate final prediction results.

Basic index and slicing

Code first:

Import numpy as np

Arr = np.arange (25) .reshape (5jue 5)

Arr

Arr1 = arr [: 3]

Arr1

Arr1 [1] =-1

Arr

You can see from the above results that the first three lines of arr are assigned to arr1, then the value of the second line of arr1 is changed to-1, and finally arr changes. The reason is that in the underlying index, only the view of the original array is returned, not a copy of the original array, and any modification to the view will affect the original array.

The solution is to copy the original array using the ndarray built-in method copy ().

Arr = np.arange (25) .reshape (5jue 5)

Arr

Arr1 = arr.copy ()

Arr1 [1] =-1

Arr

Boolean index

Code first:

Bool_ = [True,False,True,False,False]

Arr1 = arr [bool_]

Arr1

Arr1 [1] =-1

Arr

You can find that there is no similar problem with the Boolean index, because when using a Boolean index, a copy of the data is always generated, so changing the selected data has no effect on the original array.

Magic index

Index = [3, 4, 1, 2]

Arr1 = arr [index]

Arr1

Arr1 [1] =-1

Arr

Magic indexes are the same as Boolean indexes, where changes to the selected data do not affect the original array.

The above is about the content of this article on "what are the differences between different indexing methods of the numpy array". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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