Numpy usage tips: array filtering instance code, numpy usage tips

Source: Internet
Author: User

Numpy usage tips: array filtering instance code, numpy usage tips

This article focuses on the array filtering related to numpy usage techniques, as detailed below.

When Boolean array B is used as the subscript to access elements in array x, all elements in array x corresponding to the subscript True in array B are collected. The array obtained using a Boolean array as the subscript does not share data space with the original array. Note that this method only corresponds to a Boolean array (array) and does not support a Boolean list (list ).

>>> X = np. arange (5, 0,-1) >>> xarray ([5, 4, 3, 2, 1]) >>> x [np. array ([True, False, True, False, False])] >>> # obtain the subscript True. The element 0, 2 in the Boolean array is True, therefore, we can obtain the array ([5, 3])> x [[True, False, True, False, False] element with the lower mark 0, 2 in x. # Error, this is not the expected result >>> # if it is a Boolean list, True is treated as 1, False is treated as 0, and the element array ([4, 5, 5, 4, 5, 5]) >>> x [np. array ([True, False, True, True])] >>> # When the length of a Boolean array is insufficient, all the insufficient parts are treated as Falsearray ([5, 3, 2]) >>> x [np. array ([True, False, True, True])] =-1,-2, -3 # modify only the elements whose subscript is True >># the Boolean array subscript can also be used to modify the element >>> xarray ([-1, 4,-2,-3, 1])

Note: Boolean arrays are generally not produced manually. We usually use a Boolean expression, for example:

>>> X = np. random. rand (10) # generate an array of random numbers with a length of 10 and an element value of 0-1> xarray ([0.72223939, 0.921226, 0.7770805, 0.2055047, 0.17567449, 0.95799412, 0.12015178, 0.7627083, 0.43260184, 0.91379859]) >>>> x> 0.5 >>># compare the size of each element in array x with 0.5 to obtain a Boolean array, true indicates that the value of x is greater than 0.5 array ([True, False, False, True, False], dtype = bool) >>> x [x> 0.5] # x> 0.5 is a Boolean array >>## use the Boolean array returned by x> 0.5 to collect elements in x, the result is an array of all elements greater than 0.5 in array x ([0.72223939, 0.921226, 0.7770805, 0.95799412, 0.7627083, 0.91379859]).
Summary

The above is all the content of the array filtering instance code about numpy usage tips in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.