当我使用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函数并返回。谢谢!
答案 0 :(得分: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
答案 1 :(得分: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
它似乎有用,但我更喜欢你的。