1

if I have an array like

z = np.random.random((41,61,106))
y,x=np.mgrid[slice(0,61, 1),slice(0,106, 1)] 
z=z[_num,x,y]]

Who I can capture the 10 maximum and 10 minimum and mask the rest of the values in a array z.

And, it is posible take the midle of the max and the min values an put another 10 values.

 middle=sorted[(len(sorted)/2)-5:(len(sorted)/2)+5]
 print middle
 mask = ma.masked_inside(z,sorted[10],sorted[-10],middle)
 print mask

2 Answers 2

1

I do not quite understand the code snippet. To answer your first question:

import numpy as np
import numpy.ma as ma

z = np.random.random((10,10))
sorted = np.sort(z,axis=None)
mask = ma.masked_inside(z,sorted[10],sorted[-10])

concerning your second question you might also consider concatenating the conditions

ma.masked_where( ((z<sorted[sorted.size/2-5]) |
                  (z>sorted[sorted.size/2+4])) &
                 ((z>sorted[10]) & (z<sorted[-10])),z)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you!! works. I edit the answer and include one more petition , if you dont mind that take 10 values between max and min, in the middle take 10 most near.
My code that Take 10 values in the middle not works. yours yes.
And how I can retrieve the position in the 2 dimensional array? of the elements masked
the last code yours give me _f_data= ma.masked_where( ((z<sorted[sorted.size/2-5]) | (z>sorted[sorted.size/2+4])) & ((z>sorted[10]) & (z<sorted[-10])),z) TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
when Apply sort to a array with a mask, the give me in _sorted[-10] elements "--"
|
1

If you only need a few of the items in an array to be sorted, with numpy >= 1.8 it is more efficient to use np.partition than np.sort:

In [6]: z = np.random.rand(61, 106)

In [7]: %timeit np.sort(z, axis=None)
1000 loops, best of 3: 413 µs per loop

In [8]: %%timeit
    ...: n = z.size
    ...: y = np.partition(z, (10, n//2 - 5, n//2 + 5, -10), axis=None)
    ...: y[:10].sort()
    ...: y[n//2 - 5:n//2 + 5].sort()
    ...: y[-10:].sort()
    ...: 
10000 loops, best of 3: 143 µs per loop

1 Comment

hello, thank you, . How I return the value,index of the elements masked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.