-1

当我使用 map 和 lambda 函数时,我被困在如何遍历配对列表。我想根据中心位置和所选位置(x,y)到中心的距离以及特定距离出现的次数创建一系列直方图,但我不断收到超出范围错误的索引,但我没有不明白为什么。我不确定如何遍历需要指定其中哪两个值的位置。除了 n 部分之外,整个事情都有效。

抱歉没有更清楚,locations=numpy.array((x,y)) 是布尔数组中的位置,它产生我想要测试的特定位置,而不是整个数组。产生的值 (x,y) 是一个两行数组,其中我想要的值按列配对。在此之前的代码是:

     def detect_peaks(data):
        average=numpy.average(data)*2
        local_max = data > average
        return local_max

    (x,y) = numpy.where(detect_peaks(data))

    for test_x in range(0, 8):
       for test_y in range(0,8):
          distances=[]
          locations=numpy.array((x,y))
          central=numpy.array((test_x,test_y))
          [map(lambda x1: distances.append(numpy.linalg.norm(locations[(x1,0),  (x1,1)]-central)), n) for n in locations]
          histogram=numpy.histogram(distances, bins=10)

我将重写 map/lambda 函数并回来。谢谢!

4

2 回答 2

0

想出了这个:

def detect_peaks(arrayfinal):
    average=numpy.average(arrayfinal)
    local_max = arrayfinal > average
    return local_max

def dist(distances, center, n):
   distance=numpy.linalg.norm(n-center)
   distances.append(distance)

def histotest():
   peaks = numpy.where(detect_peaks(arrayfinal))
   ordered= zip(peaks[0],peaks[1])
   for test_x in range(0, 2048):
        for test_y in range(0,2048):
            distances=[]
            center=numpy.array((test_x,test_y))
            [dist(distances, center, n) for n in ordered]
            histogram=numpy.histogram(distances, bins=30)
            print histogram

它似乎有效,但我更喜欢你的。

于 2013-08-30T01:01:06.430 回答
0

这是你想要的吗?x并且y是 的数组int,而不是float。并不是说我喜欢双for循环,而是应该用更矢量化的算法来替换它们,但是为了保持变化最小并突出显示有问题的行,这里是:

>>> a2
array([[ 0.92607265,  1.26155686,  0.31516174,  0.91750943,  0.30713193],
       [ 1.0601752 ,  1.10404664,  0.67766044,  0.36434503,  0.64966887],
       [ 1.29878971,  0.66417322,  0.48084284,  1.0956822 ,  0.27142741],
       [ 0.27654032,  0.29566566,  0.33565457,  0.29749312,  0.34113315],
       [ 0.33608323,  0.25230828,  0.41507646,  0.37872512,  0.60471438]])
>>> numpy.where(detect_peaks(a2))
(array([0, 2]), array([1, 0]))
>>> def func1(locations): #You don't need to unpack the numpy.where result.
    for test_x in range(0, 4):
       for test_y in range(0, 4):
          locations=numpy.array(locations)
          central=numpy.array((test_x,test_y))
          #Vectorization is almost always better.
          #Be careful, iterate an array means iterate its rows, so, transpose it first.
          distances=map(numpy.linalg.norm, (locations-central.reshape((2,-1))).T)
          histogram=numpy.histogram(distances, bins=10)
          print 'cental point:', central
          print 'distance lst:', distances
          print histogram
          print '-------------------------'

结果:

>>> func1(numpy.where(detect_peaks(a2)))
cental point: [0 0]
distance lst: [1.0, 2.0]
(array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1]), array([ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7, 1.8,  1.9,  2. ]))
-------------------------
cental point: [0 1]
distance lst: [0.0, 2.2360679774997898]
(array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1]), array([ 0.        ,  0.2236068 ,  0.4472136 ,  0.67082039,  0.89442719, 1.11803399,  1.34164079,  1.56524758,  1.78885438,  2.01246118,         2.23606798]))
-------------------------#and more
于 2013-08-27T16:18:18.773 回答