Created
September 1, 2017 13:23
-
-
Save aldous-rey/e6ee7b0e82e23a686d5440bf3987ee23 to your computer and use it in GitHub Desktop.
NetworkX function to get centralization (for a network) from centrality (degree, closeness, betweenness, eigenvector)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getCentralization(centrality, c_type): | |
c_denominator = float(1) | |
n_val = float(len(centrality)) | |
print (str(len(centrality)) + "," + c_type + "\n") | |
if (c_type=="degree"): | |
c_denominator = (n_val-1)*(n_val-2) | |
if (c_type=="close"): | |
c_top = (n_val-1)*(n_val-2) | |
c_bottom = (2*n_val)-3 | |
c_denominator = float(c_top/c_bottom) | |
if (c_type=="between"): | |
c_denominator = (n_val*n_val*(n_val-2)) | |
if (c_type=="eigen"): | |
''' | |
M = nx.to_scipy_sparse_matrix(G, nodelist=G.nodes(),weight='weight',dtype=float) | |
eigenvalue, eigenvector = linalg.eigs(M.T, k=1, which='LR') | |
largest = eigenvector.flatten().real | |
norm = sp.sign(largest.sum())*sp.linalg.norm(largest) | |
centrality = dict(zip(G,map(float,largest))) | |
''' | |
c_denominator = sqrt(2)/2 * (n_val - 2) | |
#start calculations | |
c_node_max = max(centrality.values()) | |
c_sorted = sorted(centrality.values(),reverse=True) | |
print ("max node" + str(c_node_max) + "\n") | |
c_numerator = 0 | |
for value in c_sorted: | |
if c_type == "degree": | |
#remove normalisation for each value | |
c_numerator += (c_node_max*(n_val-1) - value*(n_val-1)) | |
else: | |
c_numerator += (c_node_max - value) | |
print ('numerator:' + str(c_numerator) + "\n") | |
print ('denominator:' + str(c_denominator) + "\n") | |
network_centrality = float(c_numerator/c_denominator) | |
if c_type == "between": | |
network_centrality = network_centrality * 2 | |
return network_centrality | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment