0

我将 Python 与numpy.

我有 numpy 数组b

 b = np.array([True,True,True,False,False,True,True,False,False,False,True,False,True])

我需要分别替换数组[False][False,False]数组。(我需要排除数组的单盐和双盐)[True][True,True]False

对于此示例:

out= np.array([True,True,True,True, True, True,True,False,False,False,True,True,True])

有人可以建议,我怎么得到out

PS:如果我需要分别替换 arrays [False], [False,False], [False,False,False]and [False,False,False,False]on arrays [True], [True,True], [True,True,True]and [True,True,True,True]

4

3 回答 3

3

怎么用scipy.ndimage.binary_dilation&scipy.ndimage.binary_erosion

import numpy as np
from scipy import ndimage
b = np.array([True,True,True,False,False,True,True,False,False,False,True,False,True])
ndimage.binary_erosion(ndimage.binary_dilation(b), border_value=1)
于 2013-03-01T08:31:46.240 回答
1

这可能不是解决此问题的最佳方法,但请查看以下内容...

In [115]: b
Out[115]:
array([ True,  True,  True, False, False,  True,  True, False, False,
       False,  True, False,  True], dtype=bool)

In [116]: l = [(k,len(list(g))) for k, g in itertools.groupby(b)]

In [117]: l
Out[117]:
[(True, 3),
 (False, 2),
 (True, 2),
 (False, 3),
 (True, 1),
 (False, 1),
 (True, 1)]

In [118]: l2 = [(True, x[1]) if x[1] in [1,2] else x for x in l]

In [119]: l2
Out[119]: [(True, 3), (True, 2), (True, 2), (False, 3), (True, 1), (True, 1), (True, 1)]

In [120]: l3 = [[x[0]] * x[1] for x in l2]

In [121]: l3
Out[121]:
[[True, True, True],
 [True, True],
 [True, True],
 [False, False, False],
 [True],
 [True],
 [True]]

In [122]: l4 = [x for x in itertools.chain(*l3)]

In [123]: l4
Out[123]:
[True,
 True,
 True,
 True,
 True,
 True,
 True,
 False,
 False,
 False,
 True,
 True,
 True]

In [124]: out = np.array(l4)

In [125]: out
Out[125]:
array([ True,  True,  True,  True,  True,  True,  True, False, False,
       False,  True,  True,  True], dtype=bool)
于 2013-03-01T08:28:52.613 回答
1

我还没有安装 numpy,但如果我正确理解你想要的,我认为下面的代码会让你想到在 numpy 中做类似的事情:

b = [True,True,True,False,False,True,True,False,False,False,True,False,True,
     False,False,False,False,True,False,False,True]
print b,'\n'

def grignote(X):
    it = iter(xrange(len(X)))
    for i in it:
        print 'i == %d     %s' % (i,X[i])
        if X[i]==False:
            j = (k for k in it if X[k]==True).next()
            print '  j == %d   %s   X[%d:%d]==%r' % (j,X[j],i,j,X[i:j])
            if j-i<3:
                print '  executing X[%d:%d]==%r' % (i,j,[True for m in xrange(j-i)])
                X[i:j] = [True for m in xrange(j-i)]
            else:
                print '  --- no execution ---   too long'

grignote(b)
print '\n',b

结果

[True, True, True, False, False, True, True, False, False, False, True, False, True, False, False, False, False, True, False, False, True] 

i == 0     True
i == 1     True
i == 2     True
i == 3     False
  j == 5   True   X[3:5]==[False, False]
  executing X[3:5]==[True, True]
i == 6     True
i == 7     False
  j == 10   True   X[7:10]==[False, False, False]
  --- no execution ---   too long
i == 11     False
  j == 12   True   X[11:12]==[False]
  executing X[11:12]==[True]
i == 13     False
  j == 17   True   X[13:17]==[False, False, False, False]
  --- no execution ---   too long
i == 18     False
  j == 20   True   X[18:20]==[False, False]
  executing X[18:20]==[True, True]

[True, True, True, True, True, True, True, False, False, False, True, True, True, False, False, False, False, True, True, True, True]
于 2013-03-01T08:46:45.127 回答