2012-06-20 85 views
1

最高值,说我有以下similarity matrix查找不相似矩阵对角线

matrix = [[100.0, 66.666666666666671, 61.539999999999999, 59.260000000000005, 59.260000000000005, 82.61333333333333, 61.539999999999999, 61.539999999999999, 61.539999999999999, 78.259999999999991], 
[66.666666666666671, 100.0, 91.306666666666672, 87.5, 87.5, 69.233333333333334, 91.306666666666672, 91.306666666666672, 91.306666666666672, 65.386666666666656], 
[61.539999999999999, 91.306666666666672, 100.0, 88.0, 88.0, 70.373333333333335, 91.666666666666671, 91.666666666666671, 100.0, 66.666666666666671], 
[59.260000000000005, 87.5, 88.0, 100.0, 84.620000000000005, 74.079999999999998, 95.833333333333329, 95.833333333333329, 88.0, 64.286666666666662], 
[59.260000000000005, 87.5, 88.0, 84.620000000000005, 100.0, 67.859999999999999, 88.0, 88.0, 88.0, 64.286666666666662], 
[82.61333333333333, 69.233333333333334, 70.373333333333335, 74.079999999999998, 67.859999999999999, 100.0, 76.926666666666662, 76.926666666666662, 76.926666666666662, 87.5], 
[61.539999999999999, 91.306666666666672, 91.666666666666671, 95.833333333333329, 88.0, 76.926666666666662, 100.0, 100.0, 91.666666666666671, 66.666666666666671], 
[61.539999999999999, 91.306666666666672, 91.666666666666671, 95.833333333333329, 88.0, 76.926666666666662, 100.0, 100.0, 91.666666666666671, 66.666666666666671], 
[61.539999999999999, 91.306666666666672, 100.0, 88.0, 88.0, 76.926666666666662, 91.666666666666671, 91.666666666666671, 100.0, 66.666666666666671], 
[78.259999999999991, 65.386666666666656, 66.666666666666671, 64.286666666666662, 64.286666666666662, 87.5, 66.666666666666671, 66.666666666666671, 66.666666666666671, 100.0]] 

注意,对角线上的值都为100.0和上三角等于下三角。

我想查找不在对角线上的五个不同最高值的索引。

对于我这样做是蛮力方式的时刻:

from collections import defaultdict 
d = defaultdict(list) 
for i in range(len(matrix)): 
    for j in range(len(matrix[i])): 
     d[matrix[i][j]].append((i,j)) 

for value in sorted(d.keys(), reverse=True)[1:6]: 
    print value, d[value] 

其中给出:

95.8333333333 [(3, 6), (3, 7), (6, 3), (7, 3)] 
91.6666666667 [(2, 6), (2, 7), (6, 2), (6, 8), (7, 2), (7, 8), (8, 6), (8, 7)] 
91.3066666667 [(1, 2), (1, 6), (1, 7), (1, 8), (2, 1), (6, 1), (7, 1), (8, 1)] 
88.0 [(2, 3), (2, 4), (3, 2), (3, 8), (4, 2), (4, 6), (4, 7), (4, 8), (6, 4), (7, 4), (8, 3), (8, 4)] 
87.5 [(1, 3), (1, 4), (3, 1), (4, 1), (5, 9), (9, 5)] 

但是,这是低效的,因为我经历了整个矩阵,而我只需要遍历一半矩阵:为最高值95.8333333333我只关心指数(3,6)(3,7)

有没有更有效的方法来做到这一点,也许使用numpy?

+0

还有100.0个元素也不在对角线上! –

+0

@Antii Haapala Gooed赶上!我也想要这些。 – BioGeek

回答

1
from heapq import nlargest 
from collections import defaultdict 

d = defaultdict(list) 

for i in xrange(len(matrix)): 
    for j in xrange(i): 
     d[matrix[i][j]].append((i, j)) 

for value, positions in nlargest(5, d.items(), key=lambda item: item[0]): 
    print value, positions 
  • 使用的xrange代替范围仅
  • 环J可I - 1(若i = 0,则内循环从未运行...)
  • 为高效使用时,不要对列表进行排序,但使用heapq 中的nlargest,因为它使用堆数据结构。它应该对大矩阵很重要。
1

Numpy会更快。

import numpy as np 

m = np.array(matrix) * np.diag(len(matrix)) # set the upper triangle to zero 
for top_value in sorted((np.unique(m)), reverse=True)[1:6]: 
    print top_value, zip(*np.where(m == top_value))