Search code examples
pythonjupyter-notebooknetwork-programmingnetworkxnode-centrality

Examples of successful implementations of Katz centrality with a weighted network in NetworkX?


I'm having some trouble with successfully calculating Katz centrality in a weighted network and would like to see if anyone has an example of it being implemented in NetworkX. I'm getting negative numbers as outputs. Here's the code I have so far:

#katz centrality
G = nx.from_numpy_matrix(network_matrix)
katz_centrality = nx.katz_centrality_numpy(G, weight = 'weight')

for x in range(16):
  print(katz_centrality[x])

Which outputs:

-0.0884332150881479
-0.32425466748018883
-0.3110317711173531
-0.3242546674801888
-0.04185470336734943
0.09838584696311473
0.09838584696311474
0.059865838163826485
0.16708256470211458
0.3491707134096127
0.3033563463599785
0.1478838644009215
0.329818599950434
0.3771672006736501
0.35188750514365186
0.1478838644009215

And for reference, network_matrix looks like this:

[[0. 5. 5. 5. 9. 3. 3. 3. 2. 3. 0. 0. 2. 0. 0. 0.]
 [5. 0. 7. 9. 4. 2. 2. 2. 1. 0. 1. 0. 0. 0. 0. 0.]
 [5. 7. 0. 7. 4. 1. 1. 1. 0. 0. 1. 0. 0. 0. 0. 0.]
 [5. 9. 7. 0. 4. 2. 2. 2. 1. 0. 1. 0. 0. 0. 0. 0.]
 [9. 4. 4. 4. 0. 2. 2. 2. 1. 4. 0. 0. 2. 0. 0. 0.]
 [3. 2. 1. 2. 2. 0. 5. 2. 3. 1. 0. 0. 0. 0. 0. 0.]
 [3. 2. 1. 2. 2. 5. 0. 2. 3. 1. 0. 0. 0. 0. 0. 0.]
 [3. 2. 1. 2. 2. 2. 2. 0. 2. 1. 0. 0. 0. 0. 0. 0.]
 [2. 1. 0. 1. 1. 3. 3. 2. 0. 1. 0. 0. 0. 0. 0. 0.]
 [3. 0. 0. 0. 4. 1. 1. 1. 1. 0. 1. 0. 3. 1. 1. 0.]
 [0. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0. 0. 1. 3. 2. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [2. 0. 0. 0. 2. 0. 0. 0. 0. 3. 1. 0. 0. 1. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 3. 0. 1. 0. 2. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 2. 0. 1. 2. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

It's strange because from my predictions, my data shows that the centralities towards the top of the outputs should be larger than the ones towards the bottom. I think I am implementing Katz centrality incorrectly and would appreciate any help.


Solution

  • Take a look here: https://centrality.mimuw.edu.pl/centrality/katz - the problem with your code (and what also lead to the error in your other question: Katz Centrality Error: PowerIterationFailedConvergence 'power iteration failed to converge within 1000 iterations')) is that you do not take into account the scaling factor alpha in the calculation of the Katz centrality. Since the default alpha value is 0.1 and the biggest eigenvalue of your matrix is 27.88, the Katz Centralities your are calculating are not converging to a stable point (with the iterative approach) and do not have solutions in the positive space. If you adapt the alpha of the function such that it is smaller than 1/(biggest eigenvalue) it will converge to a positive vector (see the Networkx documentation).

    See the code example below for both analytical approach with numpy function and iterative approach with standard networks function:

    import numpy as np
    import networkx as nx
    
    A=np.array([[0, 5, 5, 5, 9, 3, 3, 3, 2, 3, 0, 0, 2, 0, 0, 0,],
                [5, 0, 7, 9, 4, 2, 2, 2, 1, 0, 1, 0, 0, 0, 0, 0,],
                [5, 7, 0, 7, 4, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,],
                [5, 9, 7, 0, 4, 2, 2, 2, 1, 0, 1, 0, 0, 0, 0, 0,],
                [9, 4, 4, 4, 0, 2, 2, 2, 1, 4, 0, 0, 2, 0, 0, 0,],
                [3, 2, 1, 2, 2, 0, 5, 2, 3, 1, 0, 0, 0, 0, 0, 0,],
                [3, 2, 1, 2, 2, 5, 0, 2, 3, 1, 0, 0, 0, 0, 0, 0,],
                [3, 2, 1, 2, 2, 2, 2, 0, 2, 1, 0, 0, 0, 0, 0, 0,],
                [2, 1, 0, 1, 1, 3, 3, 2, 0, 1, 0, 0, 0, 0, 0, 0,],
                [3, 0, 0, 0, 4, 1, 1, 1, 1, 0, 1, 0, 3, 1, 1, 0,],
                [0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 3, 2, 0,],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
                [2, 0, 0, 0, 2, 0, 0, 0, 0, 3, 1, 0, 0, 1, 1, 0,],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 1, 0, 2, 0,],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 2, 0, 0,],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]])
    
    eig, V = np.linalg.eig(A)
    max_eig=max(eig)
    
    G = nx.from_numpy_matrix(A)
    
    print('alpha < ', 1/max_eig)
    
    katz_centrality = nx.katz_centrality(G, weight= 'weight', alpha = 1/(max_eig+1),max_iter = 100000)
    katz_centrality_numpy = nx.katz_centrality_numpy(G, weight= 'weight', alpha = 1/(max_eig+1))
    
    print("Iteration:",katz_centrality)
    print("Analytical:",katz_centrality_numpy)