NumPy Mask Array Detailed

Source: Internet
Author: User
Below for you to share a numpy mask array in detail, with a good reference value, I hope to help you. Come and see it together.

The data is cluttered in large situations and contains blank or unhandled characters, and the masked array can be very good at ignoring incomplete or invalid data points. The masked array consists of a normal array and a Boolean array, which, if ture in the Boolean array, indicates that the corresponding underlying value in the normal array is invalid, whereas false indicates that the value corresponding to the normal array is valid.

The creation method is to create a Boolean array first, and then create a masked array with the functions provided by the NUMPY.MA child package, which provides a variety of required functions.

Create an instance as follows:

Import NumPy as Nporigin = Np.arange (+). Reshape (+)  #生成一个4 x4 matrix Np.random.shuffle (origin)     #随机打乱矩阵元素random_ Mask = Np.random.randint (0,2,size=origin.shape) #生成随机 [0,2) of the whole number of the 4x4 matrix Mask_array = Np.ma.array (origin,mask=random_mask ) #生成掩码式矩阵print (Mask_array)

The results are as follows:

[8 9--] [------3] [--5 6--]]

For:

1. logarithm of negative numbers

Import NumPy as Nptriples = Np.arange (0,10,3) #每隔3取0到10中的整数, (0,3,6,9) signs = Np.ones (Ten) # (1,1,1,1,1,1,1,1,1) signs[ Triples] = -1# ( -1,1,1,-1,1,1,-1,1,1,-1) values = signs * 77# ( -77,77,77,-77,77,77,-77,77,77,-77) Ma_log = Np.ma.log ( Values) #掩码式取对数print (Ma_log)

The result is:

[--4.343805421853684 4.343805421853684--4.343805421853684 4.343805421853684--4.343805421853684 4.343805421853684- -]

2. Ignoring extrema

Import NumPy as Npinside = Np.ma.masked_outside (Array,min,max)

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.