本文整理汇总了Python中munkres.Munkres.sort方法的典型用法代码示例。如果您正苦于以下问题:Python Munkres.sort方法的具体用法?Python Munkres.sort怎么用?Python Munkres.sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类munkres.Munkres
的用法示例。
在下文中一共展示了Munkres.sort方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: match_rows
# 需要导入模块: from munkres import Munkres [as 别名]
# 或者: from munkres.Munkres import sort [as 别名]
def match_rows(X, Y):
"""
Permute the rows of _X_ to minimize error with Y
@params X numpy.array - input matrix
@params Y numpy.array - comparison matrix
@return numpy.array - X with permuted rows
"""
n, d = X.shape
n_, d_ = Y.shape
assert n == n_ and d == d_
# Create a weight matrix to compare the two
W = zeros((n, n))
for i, j in it.product(xrange(n), xrange(n)):
# Cost of 'assigning' j to i.
W[i, j] = norm(X[j] - Y[i])
matching = Munkres().compute(W)
matching.sort()
_, rowp = zip(*matching)
rowp = array(rowp)
# Permute the rows of B according to Bi
X_ = X[rowp]
return X_
示例2: match_rows_sign
# 需要导入模块: from munkres import Munkres [as 别名]
# 或者: from munkres.Munkres import sort [as 别名]
def match_rows_sign(X, Y):
"""
Permute the rows of _X_ to minimize error with Y, ignoring signs of the columns
@params X numpy.array - input matrix
@params Y numpy.array - comparison matrix
@return numpy.array - X with permuted rows
"""
n, d = X.shape
n_, d_ = Y.shape
assert n == n_ and d == d_
# Create a weight matrix to compare the two
W = zeros((n, n))
for i, j in it.product(xrange(n), xrange(n)):
# Cost of 'assigning' j to i.
W[i, j] = min(norm(X[j] - Y[i]), norm(X[j] + Y[i]))
matching = Munkres().compute(W)
matching.sort()
_, rowp = zip(*matching)
rowp = array(rowp)
# Permute the rows of B according to Bi
X_ = X[rowp]
# Change signs to minimize loss
for row in xrange(n):
if norm(X_[row] + Y[row]) < norm(X_[row] - Y[row]):
X_[row] *= -1
return X_