本文整理汇总了Python中munkres.Munkres.pad_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python Munkres.pad_matrix方法的具体用法?Python Munkres.pad_matrix怎么用?Python Munkres.pad_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类munkres.Munkres
的用法示例。
在下文中一共展示了Munkres.pad_matrix方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: str
# 需要导入模块: from munkres import Munkres [as 别名]
# 或者: from munkres.Munkres import pad_matrix [as 别名]
print 'LINE=', line2
matclust2 = scipy.io.loadmat(
clusterpath + 'cluster-' + alg + '-peaks-' + norm + line2 + '-nc' + str(nc2) + '.mat')
centers2 = matclust2['centers']
print centers2.shape
if nc1 <= nc2:
inv = False
dist = euclidean_distances(centers1, centers2)
else:
inv = True
dist = euclidean_distances(centers2, centers1)
print dist.shape
m = Munkres()
a = m.pad_matrix(dist, pad_value=1000)
nm = np.array(a)
print len(a), len(a[0])
indexes = m.compute(dist)
sumdist = 0
for c1, c2 in indexes:
if c1 < centers1.shape[0] and c2 < centers2.shape[0]:
sumdist += euclidean_distances(centers1[c1], centers2[c2])
print sumdist
print indexes
mx = max(np.max(centers1), np.max(centers2))
mn = min(np.min(centers1), np.min(centers2))
if inv:
plotHungarianSignals(indexes, centers2, centers1, mx, mn, 'HNG-' + line2 + line1,
示例2: RequestMatching
# 需要导入模块: from munkres import Munkres [as 别名]
# 或者: from munkres.Munkres import pad_matrix [as 别名]
class RequestMatching():
def __init__(self):
self.days = 20
self.cost_matrix = []
self.categories = {} # maps each category to a list of the names that can provide it.
self.name_row = {} # maps each name to the indices of the rows it corresponds to.
self.count = 0
self.munkres = Munkres()
def main(self):
print('--- snip ---')
for line in stdin:
self.process(line.rstrip('\n'))
self.flush() # don't forget to flush :)
print('--- snip ---')
def process(self, line):
words = line.split(' ')
if words[0] == 'service':
for word in words[1:]:
# remember who does what
self.categories.setdefault(word, []).append(words[1])
# allocate rows
self.cost_matrix += [[] for indices in range(self.days)]
# remember which matrix rows correspond to who
self.name_row.setdefault(words[1], range(self.count, self.count + self.days))
self.count = self.count + self.days
elif words[0] == 'request':
# fill column
for row in self.cost_matrix:
row.append(maxint)
category, days = words[2], words[3].split("-")
# fill range
if len(days) == 1:
days += days
for name in self.categories.get(category, []):
for day in range(int(days[0]), int(days[1]) + 1):
# replace last element
row = self.cost_matrix[self.name_row[name][day-1]]
self.cost_matrix[self.name_row[name][day-1]][len(row)-1] = 1
elif words[0] == '':
self.flush()
else:
pass
def flush(self):
# compute and print total valid assignments using the hungarian algorithm
self.cost_matrix = self.munkres.pad_matrix(self.cost_matrix, pad_value=maxint)
matchings = self.munkres.compute(self.cost_matrix)
print sum(filter(lambda x: x == 1, \
[self.cost_matrix[row][col] for row, col in matchings]))
# reset
self.categories = {}
self.cost_matrix = []
self.name_row = {}
self.count = 0