Python networkx.eigenvector_centrality_numpy() Examples

The following are 28 code examples of networkx.eigenvector_centrality_numpy(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: test_eigenvector_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 7 votes vote down vote up
def test_K5(self):
        """Eigenvector centrality: K5"""
        G=networkx.complete_graph(5)
        b=networkx.eigenvector_centrality(G)
        v=math.sqrt(1/5.0)
        b_answer=dict.fromkeys(G,v)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n])
        nstart = dict([(n,1) for n in G])
        b=networkx.eigenvector_centrality(G,nstart=nstart)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n])


        b=networkx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=3) 
Example #2
Source File: perturbation_attack.py    From node_embedding_attack with MIT License 6 votes vote down vote up
def baseline_eigencentrality_top_flips(adj_matrix, candidates, n_flips):
    """Selects the top (n_flips) number of flips using eigencentrality score of the edges.
    Applicable only when removing edges.

    :param adj_matrix: sp.spmatrix
        The graph represented as a sparse scipy matrix
    :param candidates: np.ndarray, shape [?, 2]
        Candidate set of edge flips
    :param n_flips: int
        Number of flips to select
    :return: np.ndarray, shape [?, 2]
        The top edge flips from the candidate set
    """
    edges = np.column_stack(sp.triu(adj_matrix, 1).nonzero())
    line_graph = construct_line_graph(adj_matrix)
    eigcentrality_scores = nx.eigenvector_centrality_numpy(nx.Graph(line_graph))
    eigcentrality_scores = {tuple(edges[k]): eigcentrality_scores[k] for k, v in eigcentrality_scores.items()}
    eigcentrality_scores = np.array([eigcentrality_scores[tuple(cnd)] for cnd in candidates])

    scores_argsrt = eigcentrality_scores.argsort()

    return candidates[scores_argsrt[-n_flips:]] 
Example #3
Source File: test_eigenvector_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_K5(self):
        """Eigenvector centrality: K5"""
        G = nx.complete_graph(5)
        b = nx.eigenvector_centrality(G)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality(G, nstart=nstart)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])

        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #4
Source File: test_eigenvector_centrality.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_K5(self):
        """Eigenvector centrality: K5"""
        G=nx.complete_graph(5)
        b=nx.eigenvector_centrality(G)
        v=math.sqrt(1/5.0)
        b_answer=dict.fromkeys(G,v)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n])
        nstart = dict([(n,1) for n in G])
        b=nx.eigenvector_centrality(G,nstart=nstart)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n])


        b=nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=3) 
Example #5
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_eigenvector_v_katz_random(self):
        G = nx.gnp_random_graph(10, 0.5, seed=1234)
        l = float(max(eigvals(nx.adjacency_matrix(G).todense())))
        e = nx.eigenvector_centrality_numpy(G)
        k = nx.katz_centrality_numpy(G, 1.0/l)
        for n in G:
            assert_almost_equal(e[n], k[n]) 
Example #6
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_K5_unweighted(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha, weight=None)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #7
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #8
Source File: test_eigenvector_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_empty_numpy(self):
        e = nx.eigenvector_centrality_numpy(nx.Graph()) 
Example #9
Source File: test_eigenvector_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_eigenvector_centrality_unweighted_numpy(self):
        G=self.H
        p=nx.eigenvector_centrality_numpy(G)
        for (a,b) in zip(list(p.values()),self.G.evc):
            assert_almost_equal(a,b) 
Example #10
Source File: test_eigenvector_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_eigenvector_centrality_weighted_numpy(self):
        G=self.G
        p=nx.eigenvector_centrality_numpy(G)
        for (a,b) in zip(list(p.values()),self.G.evc):
            assert_almost_equal(a,b) 
Example #11
Source File: test_eigenvector_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_P3_unweighted(self):
        """Eigenvector centrality: P3"""
        G=nx.path_graph(3)
        b_answer={0: 0.5, 1: 0.7071, 2: 0.5}
        b=nx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=4) 
Example #12
Source File: test_eigenvector_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_P3(self):
        """Eigenvector centrality: P3"""
        G=nx.path_graph(3)
        b_answer={0: 0.5, 1: 0.7071, 2: 0.5}
        b=nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=4)
        b=nx.eigenvector_centrality(G)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=4) 
Example #13
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_eigenvector_v_katz_random(self):
        G = nx.gnp_random_graph(10, 0.5, seed=1234)
        l = float(max(eigvals(nx.adjacency_matrix(G).todense())))
        e = nx.eigenvector_centrality_numpy(G)
        k = nx.katz_centrality_numpy(G, 1.0 / l)
        for n in G:
            assert_almost_equal(e[n], k[n]) 
Example #14
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_K5_unweighted(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha, weight=None)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #15
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #16
Source File: test_eigenvector_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_empty_numpy(self):
        e = nx.eigenvector_centrality_numpy(nx.Graph()) 
Example #17
Source File: test_eigenvector_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_eigenvector_centrality_unweighted_numpy(self):
        G = self.H
        p = nx.eigenvector_centrality_numpy(G)
        for (a, b) in zip(list(p.values()), self.G.evc):
            assert_almost_equal(a, b) 
Example #18
Source File: test_eigenvector_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_eigenvector_centrality_weighted_numpy(self):
        G = self.G
        p = nx.eigenvector_centrality_numpy(G)
        for (a, b) in zip(list(p.values()), self.G.evc):
            assert_almost_equal(a, b) 
Example #19
Source File: test_eigenvector_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_P3_unweighted(self):
        """Eigenvector centrality: P3"""
        G = nx.path_graph(3)
        b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
        b = nx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #20
Source File: test_eigenvector_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_P3(self):
        """Eigenvector centrality: P3"""
        G = nx.path_graph(3)
        b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4)
        b = nx.eigenvector_centrality(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #21
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_eigenvector_v_katz_random(self):
        G = networkx.gnp_random_graph(10,0.5, seed=1234)
        l = float(max(eigvals(networkx.adjacency_matrix(G).todense())))
        e = networkx.eigenvector_centrality_numpy(G)
        k = networkx.katz_centrality_numpy(G, 1.0/l)
        for n in G:
            assert_almost_equal(e[n], k[n]) 
Example #22
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_K5_unweighted(self):
        """Katz centrality: K5"""
        G = networkx.complete_graph(5)
        alpha = 0.1
        b = networkx.katz_centrality(G, alpha, weight=None)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = networkx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #23
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = networkx.complete_graph(5)
        alpha = 0.1
        b = networkx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = networkx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #24
Source File: test_eigenvector_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_empty_numpy(self):
        e = networkx.eigenvector_centrality_numpy(networkx.Graph()) 
Example #25
Source File: test_eigenvector_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_eigenvector_centrality_unweighted_numpy(self):
        G=self.H
        p=networkx.eigenvector_centrality_numpy(G)
        for (a,b) in zip(list(p.values()),self.G.evc):
            assert_almost_equal(a,b) 
Example #26
Source File: test_eigenvector_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_eigenvector_centrality_weighted_numpy(self):
        G=self.G
        p=networkx.eigenvector_centrality_numpy(G)
        for (a,b) in zip(list(p.values()),self.G.evc):
            assert_almost_equal(a,b) 
Example #27
Source File: test_eigenvector_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_P3_unweighted(self):
        """Eigenvector centrality: P3"""
        G=networkx.path_graph(3)
        b_answer={0: 0.5, 1: 0.7071, 2: 0.5}
        b=networkx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=4) 
Example #28
Source File: test_eigenvector_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_P3(self):
        """Eigenvector centrality: P3"""
        G=networkx.path_graph(3)
        b_answer={0: 0.5, 1: 0.7071, 2: 0.5}
        b=networkx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=4)