3

最低から最高に並べ替える必要がある値の 2D マスク配列があります。例えば:

import numpy as np

# Make a random masked array
>>> ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                     mask=np.random.binomial(1, .2, 20)).reshape((4,5))
>>> print(ar)
[[-- 51.9 38.3 46.8 43.3]
 [52.3 65.0 51.2 46.5 --]
 [56.7 51.1 -- 38.6 33.5]
 [45.2 56.8 74.1 58.4 56.4]]

# Sort the array from lowest to highest, with a flattened index
>>> sorted_ind = ar.argsort(axis=None)
>>> print(sorted_ind)
[14  2 13  4 15  8  3 11  7  1  5 19 10 16 18  6 17  0 12  9]

しかし、並べ替えられたインデックスを使用して、インデックスを 2 つの単純なサブセットに分割する必要があります。指定されたデータム以下以上です。さらに、マスクされた値は必要ないため、削除する必要があります。たとえば、上の 10 個のインデックスと下の 8 個の値datum = 51.1にフィルターをかけるにはどうすればよいですか? (注:ロジック基準または等しいため、1 つの共有インデックスがあります。3 つのマスクされた値は分析から削除できます)。後で使用するため、平坦化されたインデックス位置を保持する必要があります。sorted_inddatumnp.unravel_index(ind, ar.shape)

4

2 に答える 2

5

使用する場所:

import numpy as np
np.random.seed(0)
# Make a random masked array
ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                     mask=np.random.binomial(1, .2, 20)).reshape((4,5))
# Sort the array from lowest to highest, with a flattened index
sorted_ind = ar.argsort(axis=None)

tmp = ar.flatten()[sorted_ind]
print sorted_ind[np.ma.where(tmp<=51.0)]
print sorted_ind[np.ma.where(tmp>=51.0)]

ただし、tmp はソートされているため、np.searchsorted() を使用できます。

tmp = ar.flatten()[sorted_ind].compressed() # compressed() will delete all invalid data.
idx = np.searchsorted(tmp, 51.0)
print sorted_ind[:idx]
print sorted_ind[idx:len(tmp)]
于 2011-08-23T02:36:37.290 に答える
3

準備:

>>> ar = np.ma.array(np.round(np.random.normal(50, 10, 20), 1),
                     mask=np.random.binomial(1, .2, 20)).reshape((4,5))
>>> print(ar)
[[59.9 51.3 -- 19.7 --]
 [59.1 57.2 48.6 49.8 46.3]
 [51.1 61.6 36.9 52.2 51.7]
 [37.9 -- -- 53.1 57.5]]
>>> sorted_ind = ar.argsort(axis=None)
>>> sorted_ind
array([ 3, 12, 15,  9,  7,  8, 10,  1, 14, 13, 18,  6, 19,  5,  0, 11,  4,
        2, 16, 17])

それから新しいもの

>>> flat = ar.flatten()
>>> leq_ind = filter(lambda x: flat[x] <= 51.1, sorted_ind)
>>> leq_ind
[3, 12, 15, 9, 7, 8, 10]
>>> geq_ind = filter(lambda x: flat[x] >= 51.1, sorted_ind)
>>> geq_ind
[10, 1, 14, 13, 18, 6, 19, 5, 0, 11]
于 2011-08-23T00:48:13.667 に答える