예제 #1
0
def recurse_concat(
    x: sparse.csr_matrix,
    vectors_e: sparse.csr_matrix,
    seq: list,
    maxlen: int = 128,
    top_n: int = 1,
    scale: float = 1.25,
    idx2idx_canon: dict = None,
) -> None:
    if len(seq) < maxlen:
        matrix_dist = metrics.pairwise.cosine_distances(x, vectors_e)
        ranks = [np.argsort(distances) for distances in matrix_dist]
        assert len(ranks) == 1
        rank = ranks[0]
        seen = set(seq)
        count = 0
        for idx in rank:
            if count == top_n:
                break
            idx_canon = idx if idx2idx_canon is None else idx2idx_canon[idx]
            if idx_canon not in seen:
                e = vectors_e[idx] / (scale**len(seq))
                new = x.maximum(e)
                seq.append(idx_canon)
                count += 1
                recurse_concat(new, vectors_e, seq)
예제 #2
0
def directed2undirected(adjacency: sparse.csr_matrix,
                        weighted: bool = True) -> sparse.csr_matrix:
    if weighted:
        return sparse.csr_matrix(adjacency + adjacency.T)
    else:
        return adjacency.maximum(adjacency.T)