(chunks, relations, k=20, debug=False)
return relations, recall
Add edges according to knn of vertexes.
Add edges according to knn of vertexes.
Add edges according to knn of vertexes .
def add_knn_edges(chunks, relations, k=20, debug=False):
  """Add edges according to knn of vertexes.
  """
  edges = set()
  rel_recall = {}
  for i, j, _ in relations:
    edges.add((i, j) if i < j else (j, i))
    rel_recall[(i, j) if i < j else (j, i)] = False
  for i in range(len(chunks)):
    _dis_ij = []
    for j in range(len(chunks)):
      if j == i: continue
      _dis_ij.append((_eul_dis(chunks, i, j), j))
    sorted_dis_ij = sorted(_dis_ij)
    for _, j in sorted_dis_ij[:k]:
      _i, _j = (i, j) if i < j else (j, i)
      if (_i, _j) in rel_recall: rel_recall[(_i, _j)] = True
      if (_i, _j) not in edges:
        edges.add((_i, _j))
        relations.append((_i, _j, 0))
  cnt = 0
  for _, val in rel_recall.items():
    if val: cnt += 1
  recall = 0 if len(rel_recall) == 0 else cnt / len(rel_recall)
  if debug:
    print("add knn edge. recall:%.3f" % recall)
  return relations, recall
def add_knn_edges ( chunks , relations , k = 20 , debug = False ) : edges = set ( ) rel_recall = { } for i , j , _ in relations : edges . add ( ( i , j ) if i < j else ( j , i ) ) rel_recall [ ( i , j ) if i < j else ( j , i ) ] = False for i in range ( len ( chunks ) ) : _dis_ij = [ ] for j in range ( len ( chunks ) ) : if j == i : continue _dis_ij . append ( ( _eul_dis ( chunks , i , j ) , j ) ) sorted_dis_ij = sorted ( _dis_ij ) for _ , j in sorted_dis_ij [ : k ] : _i , _j = ( i , j ) if i < j else ( j , i ) if ( _i , _j ) in rel_recall : rel_recall [ ( _i , _j ) ] = True if ( _i , _j ) not in edges : edges . add ( ( _i , _j ) ) relations . append ( ( _i , _j , 0 ) ) cnt = 0 for _ , val in rel_recall . items ( ) : if val : cnt += 1 recall = 0 if len ( rel_recall ) == 0 else cnt / len ( rel_recall ) if debug : print ( "add knn edge. recall:%.3f" % recall ) return relations , recall