1

谢谢回答我的问题。这是我的第三个。

  1. 数据数组的每个元素都是一个坐标 (x, y)。
  2. 每个坐标有 2 个标签
  3. 目标:对具有相同两个标签的元素求和。

例如,如果输入是

data = numpy.array( [ [1, 2], [3,8], [4,5], [2,9], [1, 3], [7, 2] ] )
label1 = numpy.array([0,0,1,1,2,2])
label2 = numpy.array([0,1,0,0,1,1])

应该给:

array([[[ 1 ,  2 ],
        [ 3 ,  8 ]],

       [[ 6 , 14 ],
        [ 0 ,  0 ]],

       [[ 0 ,  0 ],
        [ 8 ,  5 ]]])

这是我当前的代码:

import numpy
import ndimage from scipy

data = numpy.array( [ [1, 2], [3,8], [4,5], [2,9], [1, 3], [7, 2] ] )
label1 = numpy.array([0,0,1,1,2,2])
label2 = numpy.array([0,1,0,0,1,1])

kinds_of_label1 = 3
kinds_of_label2 = 2

label1_l = label1.size
label2_l = label2.size

label12 = label1 * 2 + label2
kinds12_range = range(kinds_of_label1 * kinds_of_label2 )

result = numpy.zeros( (num_frame, num_cluster, 2) )
result_T = result.view().reshape( (num_frame * num_cluster, 2) ).T
result_T[0] = ndimage.measurements.sum( data.T[0], label12, index = kinds12_range )
result_T[1] = ndimage.measurements.sum( data.T[1], label12, index = kinds12_range )
counting = numpy.bincount(label12)

print(result)
print(counting)

这可行,但分别对 x 和 y 坐标求和(如在 result_T[0] 和 result_T[1] 中)似乎很糟糕。此外, ndimage.measurements.sum 给出浮点答案。整数运算更快。

我们能让这个更快更好吗?

4

1 回答 1

0
#### Wrong Answer.  Do Not Use. ####
import numpy
### Input ###
label1 = numpy.array([0,0,1,1,2,2])
kinds_of_label1 = 3

label2 = numpy.array([0,1,0,0,1,1])
kinds_of_label2 = 2

data = numpy.array( [ [1, 2], [3,8], [4,5], [2,9], [1, 3], [7, 2] ] )

### Processing ####
# this assumes label1 and label2 starts are like 0, 1, 2, 3 ...
label1_and_2 = label1*kinds_of_label2 + label2

result = numpy.zeros( (kinds_of_label1 * kinds_of_label2, 2) )
result[ label1_and_2 ] += data

counting = numpy.bincount( label1_and_2 )

### output ###
print( result.view().reshape(kinds_of_label1, kinds_of_label2, 2) )


>>> array([[[ 1.,  2.],
            [ 3.,  8.]],

           [[ 2.,  9.],
            [ 0.,  0.]],

           [[ 0.,  0.],
            [ 7.,  2.]]])
于 2013-06-13T20:54:04.763 回答