NumPy Ndarray filter arrays by criteria, associating filters

Source: Internet
Author: User

Turn: Http://http://blog.csdn.net/blackyuanc y Ogawa 77948703

Most recent projects involve a lot of preprocessing of data, and the use of Ndarray is very frequent. Ndarray how to carry on the numerical screening, summed up several methods.

1. Filter by certain fixed values
As the following code from, Ndarray can filter out a number equal to 3 of the sub-array and its index position in the original array.

Import NumPyAs Nparr = Np.array ([1,1, 1, 134, 45, 3, 46, 45, 65, 3, 23424, 234, 12, 12, 3, 546, 1, 2]) print (Np.where (arr==3)) Print (arr[np.where (arr = 3)]) Output: (Array ([5, 9, 14", Dtype=int32),) [3 3 3]          

2. Filter by multiple fixed values
It is also possible to filter multiple fixed values as described above, and to enclose the different conditions in parentheses to play | .

Import NumPyAs Nparr = Np.array ([1,1,1,134,45,3,46,45,65,3,23424,234,3,   546, 1, 2]) print (np.where (arr = 3) | (arr = = 1))) Print (arr[np.where (arr = 3) | (arr = = 1))]) Output: (Array ([ 0, 1, 2, 5, 9, + , + ], dtype=int32),) [1 1 1 3  3 3 1]               

3. Filter by Range
In addition to fixed values, we can filter by a certain range

Import NumPyAs Nparr = Np.array ([1,1,1,134,45,3,46,45,65,3,23424,234,12,12,3,546, 1, 2]) print (Np.where (arr > Span class= "Hljs-number" >3)) print (Arr[np.where (arr > 3)]) Output: (Array ([3, 4, 6, 7, 8, 10, 11, 12, 13, 15], Dtype=int32),) [134 45 46 45 65 23424 234 12 12 546]             

If you don't need index, you can have a faster way

Import NumPyAs Nparr = Np.array ([1,1,1,134, 45, 3, 46, 45, 65, 3, 23424, 234, 12, 12, 3, 546, 1, 2]) print (Arr[arr > 3]) output:[134 45 46 45 65  23424 234 12 12 546]              

Then why do you need to use Np.where? Because the index can satisfy different arrays of filtering, such as a, b two arrays we need to filter out all the A = = 0 B When you can use the Np.where function.

Import NumPyAs NPA = Np.array ([0,0,1,1,0,1,1, 0, 1]) b = Np.arange (Len (a)) print ( A) print (b) Print (b[np.where (a = = 0)]) output:[0 0 1 1 0 1 1 0 1][0 1 2 3 4 5 6 7 8][0 1 4 7]              

NumPy Ndarray filter arrays by criteria, associating filtering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.