简体   繁体   English

最大值和最小值numpy Array

[英]Maximum and minimum values numpy Array

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. 我可以捕获10个最大值和10个最小值并掩盖数组z中其余值的对象。

And, it is posible take the midle of the max and the min values an put another 10 values. 并且,可以取最大值和最小值的中间值再加上10个值。

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

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)

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 : 如果您只需要对数组中的一些项目进行排序,并且numpy> = 1.8,则使用np.partitionnp.sort效率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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM