0

最初の列に従って既にソートされている 2 列の配列があります。私のルールに従っていくつかの要素を削除したい:

1) 要素の値を最初の列の他のすべての値と比較します。他の値との差が所定の値 (たとえば 0.1) より大きい場合は、新しい配列に保持します。そうでなく、ある値よりも他との差が小さいデータであれば、それらの値すべてを比較群とみなすことができ、

2) これらの比較グループでは、2 列目の要素を比較し、グループの 2 列目に最小値を持つ要素のみを保持する必要があります。

例:私の配列が

 a= [[1.2, 3], 
     [2.2, 3], 
     [2.25, 1], 
     [2.28, 3], 
     [3.2, 8], 
     [4.2, 10]]

次に、取得したいのは次のとおりです。

  a=[[1.2, 3],  
     [2.25, 1], 
     [3.2, 8], 
     [4.2, 10]]

2 番目と 4 番目の要素を削除します。これは、最初の要素 2.2、2.25、および 2.28 の差が 0.1 より小さいためですが、2 番目の要素 1 がそれ​​らの中で最小のものです。

ヒントを教えてください。ありがとう

4

1 に答える 1

0
from numpy import *

eps = 0.1
#ASSUMING the second arrow is sorted (otherwise sort it first)
a= array(
    [[1, 1.2, 3],     
    [2, 2.2, 3], 
    [3, 2.25, 1], 
    [4, 2.28, 4],
    [5, 3.2, 8], 
    [6, 4.2, 10],
    [7, 4.21, 3], 
    [8, 4.25, 4], 
    [9, 4.28, 1],
    [10, 5.2, 10],
    ])
# expected result
# a= [[1, 1.2, 3],
#     [3, 2.25, 1],
#     [5, 3.2, 8],
#     [9, 4.28, 1],
#     [10, 5.2, 10],
#     ]

n = shape(a)[0]
b = a[:,1]

a1 = a[ (diff(b)<eps) ]
#indexes of some False that could be True.
#these indexes should be checked backwards
#and evtl. added to a1
indexes = where((diff(b)<eps)==False)[0][1:]
for index in indexes:
    if b[index] - b[index-1]<eps:
        a1 = vstack( (a1,a[index,:]) )

#sort array
a1 = a1[lexsort( (a1[:,1],a1[:,1]))]

groups = where(diff(a1[:,1])>eps)[0]
i = 0
# get min of groups
for g in groups:
    ag = a1[i:g+1,2]
    Ag = a1[i:g+1,:]
    if i == 0:
        a2 = Ag [ ag == min(ag) ]
    else:
        a2 = vstack( (a2, Ag [ ag == min(ag) ] ) )

    i = g+1
#add last group
ag = a1[g+1:,2]
Ag = a1[g+1:,:]    
a2 = vstack( (a2, Ag [ ag == min(ag) ]) )

#the elements that build no groups
result = a[ in1d(a[:,0], [ int(i) for i in a[:,0] if i not in a1[:,0] ])  ] 
# add the elements of a2, these are the minimal elements of each group
result = vstack( (result, a2) )
# sort the result (optional)
result = result[lexsort( (result[:,0], result[:,0]))]
print "final result\n", result

これがこのコードの出力です

In [1]: run filter.py
final result
[[  1.     1.2    3.  ]
 [  3.     2.25   1.  ]
 [  5.     3.2    8.  ]
 [  9.     4.28   1.  ]
 [ 10.     5.2   10.  ]]
于 2013-10-31T13:37:04.110 に答える