Example #1
0
def calculate_neighborhood(X, size, mode="kneighbors"):
    """---------------------------------------------------------------------------------------
    :function:  acquire the neighborhoods of particles
    :parameter: X               : data points
                size            : number of particles in neighborhoods
                mode            : "kneighbors" and "epsilon neighbors"
    :return:    neibrs_indices  : index of particles in neighbors
    ------------------------------------------------------------------------------------------"""
    if mode == "kneighbors":
        neibrs = NearestNeighbors(n_neighbors=size, algorithm="ball_tree").fit(X)
        neibrs_distances, neibrs_indices = neibrs.kneighbors(X)
    else:   # "epsilon neighbors"
        neibrs = radius_neighbors_graph(X, radius=size, mode="distance")
        neibrs_indices = [[i] for i in range(0, len(X))]
        nonzero_indices = neibrs.nonzero()
        for x, y in zip(nonzero_indices[0], nonzero_indices[1]):
            neibrs_indices[x].append(y)
    return neibrs_indices