<jupyter_start><jupyter_text>Centrality==========This evaluates the Eigenvector Centrality and PageRank implemented in Python against C++-native EVZ and PageRank. The Python implementation uses SciPy (and thus ARPACK) to compute the eigenvectors, while the C++ method implements a power iteration method itself.**Please note**: This notebook requires the pandas package. If you do not have it installed, you can use the "Centrality" notebook instead - but this one will look *much* nicer, so install pandas. ;)<jupyter_code>cd ../../
import networkit
import pandas as pd
import random as rd
G = networkit.graphio.readGraph("input/celegans_metabolic.graph", networkit.Format.METIS)<jupyter_output><empty_output><jupyter_text>First, we just compute the Python EVZ and display a sample. The "scores()" method returns a list of centrality scores in order of the vertices. Thus, what you see below are the (normalized, see the respective argument) centrality scores for G.nodes()[0], G.nodes()[1], ...<jupyter_code>evzSciPy = networkit.centrality.SciPyEVZ(G, normalized=True)
evzSciPy.run()
scoresTableEVZ = pd.DataFrame({"Python EVZ": evzSciPy.scores()[:10]})
scoresTableEVZ<jupyter_output><empty_output><jupyter_text>We now take a look at the 10 most central vertices according to the four heuristics. Here, the centrality algorithms offer the ranking() method that returns a list of (vertex, centrality) ordered by centrality. We first compute the remaining values...<jupyter_code>evz = networkit.centrality.EigenvectorCentrality(G, True)
evz.run()

pageRank = networkit.centrality.PageRank(G, 0.95)
pageRank.run()

pageRankSciPy = networkit.centrality.SciPyPageRank(G, 0.95, normalized=True)
pageRankSciPy.run()<jupyter_output><empty_output><jupyter_text>... then display it. What you will see is a list of the 10 most important vertices and their respective centralities according to the C++ / Python version of eigenvector centrality:<jupyter_code>rankTableEVZ = pd.DataFrame({"Python EVZ": evzSciPy.ranking()[:10], "C++ EVZ": evz.ranking()[:10]})
rankTableEVZ<jupyter_output><empty_output><jupyter_text>If everything went well, this should look at least similar. Now we do the same for the PageRank instead of EVZ:<jupyter_code>rankTablePR = pd.DataFrame({"Python PageRank": pageRankSciPy.ranking()[:10], "C++ PageRank": pageRank.ranking()[:10]})
rankTablePR<jupyter_output><empty_output><jupyter_text>If everything went well, these should look similar, too. To make sure that not just the top scoring vertices are comparable in both implementations, here are 10 randomly selected vertices in comparison:<jupyter_code>vertices = rd.sample(G.nodes(), 10)
randTableEVZ = pd.DataFrame({"Python EVZ": evzSciPy.scores(), "C++ EVZ": evz.scores()})
randTableEVZ.loc[vertices]<jupyter_output><empty_output><jupyter_text>Finally, we take a look at the relative differences between the computed centralities for the vertices:<jupyter_code>differences = [(max(x[0], x[1]) / min(x[0], x[1])) - 1 for x in zip(evz.scores(), evzSciPy.scores())]
print("Average relative difference: {}".format(sum(differences) / len(differences)))
print("Maximum relative difference: {}".format(max(differences)))<jupyter_output>Average relative difference: 0.22655120802802445
Maximum relative difference: 4.1182013118629675
{'fraction_non_alphanumeric': 0.11168437025796661, 'fraction_numerical': 0.020030349013657057, 'mean_word_length': 6.828978622327791, 'pattern_counts': {'":': 7, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '48591', 'license': 'MIT', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1000, 'n_words': 328, 'path': 'Doc/uploads/docs/SpectralCentralityWithPandas.ipynb', 'repo_name': 'vishalbelsare/networkit', 'size': '3295'}