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)