


Implement ndarray array in numpy to return index method that meets specific conditions
The following is an article in numpy that implements an ndarray array to return an index that meets specific conditions. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together
In the ndarray type of numpy, there seems to be no method to directly return a specific index. I only found the where function, but the where function is very useful for finding the index corresponding to a specific value. For Returning an index of values within a certain range is not very efficient, at least I don't understand how to do it. Let’s first talk about the usage of where function.
(1) Usage scenarios of where function:
For example, now I generated an array:
import numpy as np arr=np.array([1,1,1,134,45,3,46,45,65,3,23424,234,12,12,3,546,1,2])
Now arr is an ndarray type array containing 18 elements. Let’s call it an array later. If I want to return the index position corresponding to all elements in the array with a value of 3, this can be easily accomplished through the where function.
print np.where(arr==3)
It will return a tuple containing all index positions corresponding to a value of 3, as shown below:
You can see that the corresponding element value at indexes 5, 9, and 14 is 3. This way you can easily achieve your goal. But this won't work for indexes within a certain range of changes. The following method is a compromise that I came up with. It is relatively stupid, but the master can clear it up.
(2) Solve the search for value index within a certain range through an auxiliary array
We build an array that identifies the element index, Then use it to display the index corresponding to the element that meets the conditions. It's still the same array, if I now want to return the index of the element value between 3 and 100. I can generate an array with the same size as arr, and then first filter it once to find the array corresponding to the index of the element greater than 3, and then filter it again, and finally get the desired result. The code is as follows:
b=np.arange(len(arr))#生成和arr相同长度的数组
c=b[arr>3]#c存放的就是arr中大于3的元素对应的索引 #最后通过遍历c数组,选择3到100之间的值打印出来 for i in range(len(c)): if arr[c[i]]<100: print c[i],
Let’s take a look at the execution effect:
You can see that the program prints out the index values corresponding to all elements between 3 and 100. If you want to get the index value and the corresponding element at the same time, just enter The above "print c[i]" can be replaced with "print c[i],arr[c[i]]".
Of course, this method is also suitable for selecting the index corresponding to a specific value. For example, if I want to find all positions corresponding to 3, I can use print b[arr==3] to print out all values that are 3. The index corresponding to the element. In fact, no matter how you do it, you use an array to perform relational operations to generate a Boolean array, and then display the areas that are True in the array.
Of course, you can also filter twice to filter out the index arrays corresponding to elements greater than 3 and elements less than 100, and then perform intersection processing on the two arrays. There is an intersect1d function in numpy that can do this operation, but it is still troublesome. I can only think of these methods at present. I don’t know if any expert has a better method. Everyone is welcome to share it.
Related recommendations:
Related recommendations:
How to view numpy array attributes in python3 library
How to convert numpy and array in python
The above is the detailed content of Implement ndarray array in numpy to return index method that meets specific conditions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to update the numpy version: 1. Use the "pip install --upgrade numpy" command; 2. If you are using the Python 3.x version, use the "pip3 install --upgrade numpy" command, which will download and install it, overwriting the current NumPy Version; 3. If you are using conda to manage the Python environment, use the "conda install --update numpy" command to update.

Numpy is an important mathematics library in Python. It provides efficient array operations and scientific calculation functions and is widely used in data analysis, machine learning, deep learning and other fields. When using numpy, we often need to check the version number of numpy to determine the functions supported by the current environment. This article will introduce how to quickly check the numpy version and provide specific code examples. Method 1: Use the __version__ attribute that comes with numpy. The numpy module comes with a __

Teach you step by step to install NumPy in PyCharm and make full use of its powerful functions. Preface: NumPy is one of the basic libraries for scientific computing in Python. It provides high-performance multi-dimensional array objects and various functions required to perform basic operations on arrays. function. It is an important part of most data science and machine learning projects. This article will introduce you to how to install NumPy in PyCharm, and demonstrate its powerful features through specific code examples. Step 1: Install PyCharm First, we

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

With the rapid development of fields such as data science, machine learning, and deep learning, Python has become a mainstream language for data analysis and modeling. In Python, NumPy (short for NumericalPython) is a very important library because it provides a set of efficient multi-dimensional array objects and is the basis for many other libraries such as pandas, SciPy and scikit-learn. In the process of using NumPy, you are likely to encounter compatibility issues between different versions, then

Numpy can be installed using pip, conda, source code and Anaconda. Detailed introduction: 1. pip, enter pip install numpy in the command line; 2. conda, enter conda install numpy in the command line; 3. Source code, unzip the source code package or enter the source code directory, enter in the command line python setup.py build python setup.py install.

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

Numpy installation guide: One article to solve installation problems, need specific code examples Introduction: Numpy is a powerful scientific computing library in Python. It provides efficient multi-dimensional array objects and tools for operating array data. However, for beginners, installing Numpy may cause some confusion. This article will provide you with a Numpy installation guide to help you quickly solve installation problems. 1. Install the Python environment: Before installing Numpy, you first need to make sure that Py is installed.
