本文整理汇总了Python中sklearn.utils.linear_assignment_.linear_assignment函数的典型用法代码示例。如果您正苦于以下问题:Python linear_assignment函数的具体用法?Python linear_assignment怎么用?Python linear_assignment使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了linear_assignment函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_permute
def get_permute(W_r, H_r, W, H, coeff):
T_0 = W_r.shape[1]
T = W.shape[1]
comp = zeros((T_0, T))
for t in xrange(T):
p = sqrt(W_r) - sqrt(tile(W[:, t], (T_0, 1))).T
comp[:, t] = sum(p ** 2, 0)
if T < T_0:
t = array(linear_assignment(comp.T))[:, [1, 0]]
# http://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column
return t[t[:, 0].argsort()]
else:
return array(linear_assignment(comp))
示例2: track
def track(self, original_img, filtered_img, prev_data):
n_objects = self.nObjectsSpinBox.value()
distance_threshold = self.distanceThresholdSpinBox.value()
if self.k_means is None:
self.k_means = cluster.KMeans(n_clusters=n_objects)
non_zero_pos = np.transpose(np.nonzero(filtered_img.T))
try:
center_pos = self.k_means.fit(non_zero_pos).cluster_centers_
except:
if self.ret_pos_old is None:
return {'position': np.full((n_objects, 2), np.nan)}
else:
return {'position': self.ret_pos_old}
if self.ret_pos_old is None:
self.ret_pos_old = center_pos.copy()
self.ret_pos = center_pos
else:
ret_pos_old_repeated = np.repeat(self.ret_pos_old, n_objects, axis=0)
center_pos_tiled = np.tile(center_pos, (n_objects, 1))
cost_mtx = np.linalg.norm(ret_pos_old_repeated - center_pos_tiled, axis=1)
cost_mtx = cost_mtx.reshape((n_objects, n_objects))
idx = linear_assignment(cost_mtx)
idx = idx[cost_mtx[idx[:,0], idx[:,1]]<=distance_threshold]
self.ret_pos[:] = self.ret_pos_old[:]
self.ret_pos[idx[:, 0], :] = center_pos[idx[:, 1], :]
self.ret_pos_old[:] = self.ret_pos[:].copy()
return {'position': self.ret_pos}
示例3: get_indexList
def get_indexList(coord, coord_prev, indexListPrev, totWorms, max_allow_dist = 10.0):
#get the indexes of the next worms from their nearest neightbors using the hungarian algorithm
if coord_prev.size!=0:
costMatrix = cdist(coord_prev, coord);
assigment = linear_assignment(costMatrix)
indexList = np.zeros(coord.shape[0]);
speed = np.zeros(coord.shape[0])
for row, column in assigment: #ll = 1:numel(indexList)
if costMatrix[row,column] < max_allow_dist:
indexList[column] = indexListPrev[row];
speed[column] = costMatrix[row][column];
elif column < coord.shape[0]:
totWorms = totWorms +1;
indexList[column] = totWorms;
for rep_ind in list_duplicates(indexList):
totWorms = totWorms +1; #assign new worm_index to joined trajectories
indexList[rep_ind] = totWorms;
else:
indexList = totWorms + np.arange(1,coord.shape[0]+1);
totWorms = indexList[-1]
speed = totWorms*[None]
#print costMatrix[-1,-1]
return (totWorms, indexList, speed)
示例4: best_map
def best_map(l1, l2):
"""
Permute labels of l2 to match l1 as much as possible
"""
if len(l1) != len(l2):
print "L1.shape must == L2.shape"
exit(0)
label1 = np.unique(l1)
n_class1 = len(label1)
label2 = np.unique(l2)
n_class2 = len(label2)
n_class = max(n_class1, n_class2)
G = np.zeros((n_class, n_class))
for i in range(0, n_class1):
for j in range(0, n_class2):
ss = l1 == label1[i]
tt = l2 == label2[j]
G[i, j] = np.count_nonzero(ss & tt)
A = la.linear_assignment(-G)
new_l2 = np.zeros(l2.shape)
for i in range(0, n_class2):
new_l2[l2 == label2[A[i][1]]] = label1[A[i][0]]
return new_l2.astype(int)
示例5: assignment
def assignment(costMatrix, costOfNonAssignment=140):
Assignment = namedtuple('Assignment', 'trackIndex detectionIndex')
assignments = []
unmatchedTracks = []
unmatchedDetections = []
# print(costMatrix)
# If matrix is rectangular, then pad
rows, cols = costMatrix.shape
diff = rows - cols
if diff != 0:
padValue = costOfNonAssignment + 1
if diff < 0:
pad_width = [(0, np.abs(diff)), (0, 0)]
if diff > 0:
pad_width = [(0, 0), (0, diff)]
costMatrix = np.pad(costMatrix, pad_width, mode='constant',
constant_values=(padValue, padValue))
# Compute the optimal assignment
assign = linear_assignment(costMatrix)
# Throw out any assignments that cost more than the costOfNonAssingment
for row in assign:
trackIndex = row[0]
detectionIndex = row[1]
if costMatrix[trackIndex, detectionIndex] > costOfNonAssignment:
if trackIndex < rows:
unmatchedTracks.append(trackIndex)
if detectionIndex < cols:
unmatchedDetections.append(detectionIndex)
else:
assignments.append(Assignment(trackIndex, detectionIndex))
return assignments, unmatchedTracks, unmatchedDetections
示例6: syntax_similarity_conversation
def syntax_similarity_conversation(self, documents1, average=False): #syntax similarity of each document with its before and after document
global numnodes
documents1parsed = []
for d1 in range(len(documents1)):
sys.stderr.write(str(d1)+"\n")
# print documents1[d1]
tempsents = (self.sent_detector.tokenize(documents1[d1].strip()))
for s in tempsents:
if len(s.split())>100:
documents1parsed.append("NA")
break
else:
temp = list(self.parser.raw_parse_sents((tempsents)))
for i in range(len(temp)):
temp[i] = list(temp[i])[0]
temp[i] = ParentedTree.convert(temp[i])
documents1parsed.append(list(temp))
results = OrderedDict()
for d1 in range(len(documents1parsed)):
d2 = d1+1
if d2 == len(documents1parsed):
break
if documents1parsed[d1] == "NA" or documents1parsed[d2]=="NA":
continue
costMatrix = []
for i in range(len(documents1parsed[d1])):
numnodes = 0
tempnode = Node(documents1parsed[d1][i].root().label())
new_sentencedoc1 = self.convert_mytree(documents1parsed[d1][i],tempnode)
temp_costMatrix = []
sen1nodes = numnodes
for j in range(len(documents1parsed[d2])):
numnodes=0.0
tempnode = Node(documents1parsed[d2][j].root().label())
new_sentencedoc2 = self.convert_mytree(documents1parsed[d2][j],tempnode)
ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
ED = ED / (numnodes + sen1nodes)
temp_costMatrix.append(ED)
costMatrix.append(temp_costMatrix)
costMatrix = np.array(costMatrix)
if average==True:
return 1-np.mean(costMatrix)
else:
indexes = su.linear_assignment(costMatrix)
total = 0
rowMarked = [0] * len(documents1parsed[d1])
colMarked = [0] * len(documents1parsed[d2])
for row, column in indexes:
total += costMatrix[row][column]
rowMarked[row] = 1
colMarked [column] = 1
for k in range(len(rowMarked)):
if rowMarked[k]==0:
total+= np.min(costMatrix[k])
for c in range(len(colMarked)):
if colMarked[c]==0:
total+= np.min(costMatrix[:,c])
maxlengraph = max(len(documents1parsed[d1]),len(documents1parsed[d2]))
results[(d1,d2)] = 1-total/maxlengraph#, minWeight/minlengraph, randtotal/lengraph
return results
示例7: syntax_similarity_two_documents
def syntax_similarity_two_documents(self, doc1, doc2, average=False): #syntax similarity of two single documents
global numnodes
doc1sents = self.sent_detector.tokenize(doc1.strip())
doc2sents = self.sent_detector.tokenize(doc2.strip())
for s in doc1sents: # to handle unusual long sentences.
if len(s.split())>100:
return "NA"
for s in doc2sents:
if len(s.split())>100:
return "NA"
try: #to handle parse errors. Parser errors might happen in cases where there is an unsuall long word in the sentence.
doc1parsed = self.parser.raw_parse_sents((doc1sents))
doc2parsed = self.parser.raw_parse_sents((doc2sents))
except Exception as e:
sys.stderr.write(str(e))
return "NA"
costMatrix = []
doc1parsed = list(doc1parsed)
for i in range(len(doc1parsed)):
doc1parsed[i] = list(doc1parsed[i])[0]
doc2parsed = list(doc2parsed)
for i in range(len(doc2parsed)):
doc2parsed[i] = list(doc2parsed[i])[0]
for i in range(len(doc1parsed)):
numnodes = 0
sentencedoc1 = ParentedTree.convert(doc1parsed[i])
tempnode = Node(sentencedoc1.root().label())
new_sentencedoc1 = self.convert_mytree(sentencedoc1,tempnode)
temp_costMatrix = []
sen1nodes = numnodes
for j in range(len(doc2parsed)):
numnodes=0.0
sentencedoc2 = ParentedTree.convert(doc2parsed[j])
tempnode = Node(sentencedoc2.root().label())
new_sentencedoc2 = self.convert_mytree(sentencedoc2,tempnode)
ED = simple_distance(new_sentencedoc1, new_sentencedoc2)
ED = ED / (numnodes + sen1nodes)
temp_costMatrix.append(ED)
costMatrix.append(temp_costMatrix)
costMatrix = np.array(costMatrix)
if average==True:
return 1-np.mean(costMatrix)
else:
indexes = su.linear_assignment(costMatrix)
total = 0
rowMarked = [0] * len(doc1parsed)
colMarked = [0] * len(doc2parsed)
for row, column in indexes:
total += costMatrix[row][column]
rowMarked[row] = 1
colMarked [column] = 1
for k in range(len(rowMarked)):
if rowMarked[k]==0:
total+= np.min(costMatrix[k])
for c in range(len(colMarked)):
if colMarked[c]==0:
total+= np.min(costMatrix[:,c])
maxlengraph = max(len(doc1parsed),len(doc2parsed))
return 1-(total/maxlengraph)
示例8: accuracy
def accuracy(l,lg):
profitMatrix = confusion_matrix(lg, l)
costMatrix = lg.shape[0] - profitMatrix
ind = linear_assignment(costMatrix)
total = 0.0
for i in ind:
total += profitMatrix[i[0],i[1]]
return total / lg.shape[0]
示例9: get_deci_map
def get_deci_map(self, state_map, w, dnum, tnum):
score_mat = state_map.dot(w)
match_idxs = linear_assignment(-score_mat)
deci_map = np.zeros_like(score_mat)
for m in match_idxs:
if m[0]<dnum or m[1]<tnum:
deci_map[m[0],m[1]] = 1
return match_idxs, deci_map
示例10: accuracy
def accuracy(l,lg):
profitMatrix = confusion_matrix(lg,l)
costMatrix = np.iinfo(np.int64).max - profitMatrix
ind = linear_assignment(costMatrix)
total = 0.0
for i in ind:
total += profitMatrix[tuple(i)]
return total / lg.shape[0]
示例11: calcAssignMtx
def calcAssignMtx(self):
idx = linear_assignment(self.costMtx)
if self.assignMtx is None:
self.assignMtx = np.zeros((self.N, self.M))
else:
self.assignMtx[:] = 0
self.assignMtx[idx[:, 0], idx[:,1]] = 1
示例12: ceafe
def ceafe(clusters, gold_clusters):
clusters = [c for c in clusters if len(c) != 1]
scores = np.zeros((len(gold_clusters), len(clusters)))
for i in range(len(gold_clusters)):
for j in range(len(clusters)):
scores[i, j] = phi4(gold_clusters[i], clusters[j])
matching = linear_assignment(-scores)
similarity = sum(scores[matching[:, 0], matching[:, 1]])
return similarity, len(clusters), similarity, len(gold_clusters)
示例13: cluster_acc
def cluster_acc(Y_pred, Y):
from sklearn.utils.linear_assignment_ import linear_assignment
assert Y_pred.size == Y.size
D = max(Y_pred.max(), Y.max())+1
w = np.zeros((D,D), dtype=np.int64)
for i in range(Y_pred.size):
w[Y_pred[i], Y[i]] += 1
ind = linear_assignment(w.max() - w)
return sum([w[i,j] for i,j in ind])*1.0/Y_pred.size, w
示例14: match_detections
def match_detections(self, old_dets, new_dets, iou_threshold):
if len(old_dets) == 0 or len(new_dets) == 0:
return []
iou_cost = np.array(
[[iou(old, new) for new in new_dets] for old in old_dets],
'float32'
)
match_pairs = linear_assignment(-iou_cost)
return match_pairs
示例15: linear_assignment
def linear_assignment(df):
"""Wrapper of sklearn linear assignment algorithm for DataFrame cost matrix. Returns
DataFrame with columns for matched labels. Minimizes cost.
"""
from sklearn.utils.linear_assignment_ import linear_assignment
x = linear_assignment(df.as_matrix())
y = zip(df.index[x[:, 0]], df.columns[x[:, 1]])
df_out = pd.DataFrame(y, columns=[df.index.name, df.columns.name])
return df_out