Exemple #1
0
        def grid_graph(m, corners=False):
            z = graph.grid(m)
            # compute pairwise distance
            dist, idx = graph.distance_sklearn_metrics(z, k=number_edges, metric=metric)
            A = graph.adjacency(dist, idx) # build adjacent matrix
            # Connections are only vertical or horizontal on the grid.
            # Corner vertices are connected to 2 neightbors only.
            if corners:
                A = A.toarray()
                A[A < A.max()/1.5] = 0
                A = scipy.sparse.csr_matrix(A)
                print('{} edges'.format(A.nnz))

            print("{} > {} edges".format(A.nnz//2, number_edges*m**2//2))
            return A
Exemple #2
0
def grid_graph(m, corners=False):
    k=8
    z = graph.grid(m)
    dist, idx = graph.distance_sklearn_metrics(z, k=8, metric='euclidean')
    A = graph.adjacency(dist, idx)

    # Connections are only vertical or horizontal on the grid.
    # Corner vertices are connected to 2 neightbors only.
    if corners:
        import scipy.sparse
        A = A.toarray()
        A[A < A.max()/1.5] = 0
        A = scipy.sparse.csr_matrix(A)
        print('{} edges'.format(A.nnz))

    print("{} > {} edges".format(A.nnz//2, k*m**2//2))
    return A
Exemple #3
0
def grid_graph(m, corners=False):
    """
    返回 graph 的连接矩阵(symmetric, nearest k neighbors)
    """
    num_edges = 8
    z = graph.grid(m)
    dist, idx = graph.distance_sklearn_metrics(z, k=8, metric='euclidean')
    A = graph.adjacency(dist, idx)
    print('A.nnz: {}'.format(A.nnz))
    if corners:
        import scipy.sparse
        A = A.toarray()
        A[A < A.max()/1.5] = 0
        A = scipy.sparse.csr_matrix(A)
        print('{} edges'.format(A.nnz))

    print("{} > {} edges".format(A.nnz // 2, num_edges * (m**2) // 2))
    return A