<jupyter_start><jupyter_text>Algebra<jupyter_code>import matplotlib.pyplot as plt
import networkx as nx
import seaborn as sns
sns.set()
%matplotlib inline
import numpy as np
import warnings
import matplotlib.cbook
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
G = nx.karate_club_graph()
nx.draw(G)<jupyter_output><empty_output><jupyter_text>------------  1. Graph MatrixAdjacency matrix and incidence matrix of graphs.- ```adjacency_matrix(G[, nodelist, weight])``` 	Returns adjacency matrix of G.- ```incidence_matrix(G[, nodelist, edgelist, …])``` 	Returns incidence matrix of G.  1.1. Adjacency Matrix<jupyter_code>A = nx.adjacency_matrix(G)
print(A.todense())<jupyter_output>[[0 1 1 ... 1 0 0]
 [1 0 1 ... 0 0 0]
 [1 1 0 ... 0 1 0]
 ...
 [1 0 0 ... 0 1 1]
 [0 0 1 ... 1 0 1]
 [0 0 0 ... 1 1 0]]<jupyter_text>1.2 incidence matrix Returns incidence matrix of G.The incidence matrix assigns each row to a node and each column to an edge. For a standard incidence matrix a 1 appears wherever a row’s node is incident on the column’s edge. For an oriented incidence matrix each edge is assigned an orientation (arbitrarily for undirected and aligning to direction for directed). A -1 appears for the tail of an edge and 1 for the head of the edge. The elements are zero otherwise.<jupyter_code>A = nx.incidence_matrix(G)
print(A.todense())<jupyter_output>[[1. 1. 1. ... 0. 0. 0.]
 [1. 0. 0. ... 0. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 1. 1. 0.]
 [0. 0. 0. ... 1. 0. 1.]
 [0. 0. 0. ... 0. 1. 1.]]<jupyter_text>-----------  2. Laplacian Matrix Laplacian matrix of graphs.- ```laplacian_matrix(G[, nodelist, weight])``` 	Returns the Laplacian matrix of G.- ```normalized_laplacian_matrix(G[, nodelist, …])``` 	Returns the normalized Laplacian matrix of G.- ```directed_laplacian_matrix(G[, nodelist, …])``` 	Returns the directed Laplacian matrix of G.  2.1 Laplacian MatrixReturns the Laplacian matrix of G.The graph Laplacian is the matrix L = D - A, where A is the adjacency matrix and D is the diagonal matrix of node degrees.<jupyter_code>A = nx.laplacian_matrix(G)
print(A.todense())<jupyter_output>[[16 -1 -1 ... -1  0  0]
 [-1  9 -1 ...  0  0  0]
 [-1 -1 10 ...  0 -1  0]
 ...
 [-1  0  0 ...  6 -1 -1]
 [ 0  0 -1 ... -1 12 -1]
 [ 0  0  0 ... -1 -1 17]]<jupyter_text>2.2 Normalized Laplacian Matrix:    Returns the normalized Laplacian matrix of G.The normalized graph Laplacian is the matrix$𝑁=𝐷^{−1/2}𝐿𝐷^{−1/2}$where L is the graph Laplacian and D is the diagonal matrix of node degrees.<jupyter_code>A = nx.normalized_laplacian_matrix(G)
print(A.todense())<jupyter_output>[[ 1.         -0.08333333 -0.07905694 ... -0.10206207  0.
   0.        ]
 [-0.08333333  1.         -0.10540926 ...  0.          0.
   0.        ]
 [-0.07905694 -0.10540926  1.         ...  0.         -0.09128709
   0.        ]
 ...
 [-0.10206207  0.          0.         ...  1.         -0.11785113
  -0.09901475]
 [ 0.          0.         -0.09128709 ... -0.11785113  1.
  -0.070014  ]
 [ 0.          0.          0.         ... -0.09901475 -0.070014
   1.        ]]<jupyter_text>2.3 Directed Laplacian MatrixReturns the directed Laplacian matrix of G.The graph directed Laplacian is the matrix$𝐿=𝐼−(Φ^{1/2}𝑃Φ^{−1/2}+Φ^{−1/2}𝑃^{𝑇}Φ^{1/2})/2$where I is the identity matrix, P is the transition matrix of the graph, and Phi a matrix with the Perron vector of P in the diagonal and zeros elsewhere.Depending on the value of walk_type, P can be the transition matrix induced by a random walk, a lazy random walk, or a random walk with teleportation (PageRank).<jupyter_code>DG = nx.to_directed(G)
A = nx.directed_laplacian_matrix(DG)
print(A)<jupyter_output>[[ 1.         -0.08333333 -0.07905694 ... -0.10206207  0.
   0.        ]
 [-0.08333333  1.         -0.10540926 ...  0.          0.
   0.        ]
 [-0.07905694 -0.10540926  1.         ...  0.         -0.09128709
   0.        ]
 ...
 [-0.10206207  0.          0.         ...  1.         -0.11785113
  -0.09901475]
 [ 0.          0.         -0.09128709 ... -0.11785113  1.
  -0.070014  ]
 [ 0.          0.          0.         ... -0.09901475 -0.070014
   1.        ]]<jupyter_text>-------------  3.SpectrumEigenvalue spectrum of graphs.- ```laplacian_spectrum(G[, weight])``` 	Returns eigenvalues of the Laplacian of G- ```adjacency_spectrum(G[, weight])``` 	Returns eigenvalues of the adjacency matrix of G.- ```modularity_spectrum(G)``` 	Returns eigenvalues of the modularity matrix of G.  3.1 Laplacian SpectrumReturns eigenvalues of the Laplacian of G<jupyter_code>A = nx.laplacian_spectrum(G)
print(A)<jupyter_output>[-4.69069229e-15  4.68525227e-01  9.09247664e-01  1.12501072e+00
  1.25940411e+00  1.59928308e+00  1.76189862e+00  1.82605521e+00
  1.95505045e+00  2.00000000e+00  2.00000000e+00  2.00000000e+00
  2.00000000e+00  2.00000000e+00  2.48709173e+00  2.74915718e+00
  3.01396297e+00  3.24206748e+00  3.37615409e+00  3.38196601e+00
  3.47218740e+00  4.27587682e+00  4.48000767e+00  4.58079267e+00
  5.37859508e+00  5.61803399e+00  6.33159222e+00  6.51554463e+00
  6.99619703e+00  9.77724095e+00  1.09210675e+01  1.33061223e+01
  1.70551712e+01  1.81366960e+01]<jupyter_text>3.2. Adjacency SpectrumReturns eigenvalues of the adjacency matrix of G.<jupyter_code>A = nx.adjacency_spectrum(G)
print(A)<jupyter_output>[ 6.72569773e+00+0.00000000e+00j  4.97707423e+00+0.00000000e+00j
 -4.48722919e+00+0.00000000e+00j -3.44793486e+00+0.00000000e+00j
 -3.11069092e+00+0.00000000e+00j  2.91650670e+00+0.00000000e+00j
 -2.43742443e+00+0.00000000e+00j  2.30908767e+00+0.00000000e+00j
 -2.09082295e+00+0.00000000e+00j -1.68768945e+00+0.00000000e+00j
 -1.44407374e+00+0.00000000e+00j -1.19242425e+00+0.00000000e+00j
 -1.04208785e+00+0.00000000e+00j -7.92406815e-01+0.00000000e+00j
 -4.18818748e-01+0.00000000e+00j  1.48615954e+00+0.00000000e+00j
  1.45305566e+00+0.00000000e+00j  1.08328639e+00+0.00000000e+00j
  1.03145042e+00+0.00000000e+00j  8.34304102e-01+0.00000000e+00j
  2.99410685e-01+0.00000000e+00j  4.19729474e-01+0.00000000e+00j
  6.15840589e-01+0.00000000e+00j -2.00000000e+00+0.00000000e+00j
 -1.84096457e-16+0.00000000e+00j  3.25975263e-16+0.00000000e+00j
  1.19978798e-16+1.01484071e-16j  1.19978798e-16-1.01484071e-16j
 -8.24776675e-17+0.00000000e+00j  4.70919565e-17+6.63862545e-17j
  4.70919565e-17-6.638625[...]<jupyter_text>3.3 Modularity SpectrumReturns eigenvalues of the modularity matrix of G.<jupyter_code>A = nx.modularity_spectrum(G)
print(A)<jupyter_output>[-5.59249634e+00+0.00000000e+00j  4.97708023e+00+0.00000000e+00j
  3.04278102e+00+0.00000000e+00j -3.45111371e+00+0.00000000e+00j
 -3.12663799e+00+0.00000000e+00j  2.32020502e+00+0.00000000e+00j
 -2.43920051e+00+0.00000000e+00j -2.09107775e+00+0.00000000e+00j
  1.48883282e+00+0.00000000e+00j  1.45942182e+00+0.00000000e+00j
 -1.68805729e+00+0.00000000e+00j -1.46632948e+00+0.00000000e+00j
 -1.19260232e+00+0.00000000e+00j -1.07838380e+00+0.00000000e+00j
 -7.92900927e-01+0.00000000e+00j -4.28208059e-01+0.00000000e+00j
  1.08328648e+00+0.00000000e+00j  1.03148518e+00+0.00000000e+00j
  8.38170794e-01+0.00000000e+00j  6.16764429e-01+0.00000000e+00j
  3.00015828e-01+0.00000000e+00j  4.19733784e-01+0.00000000e+00j
 -2.00000000e+00+0.00000000e+00j -4.78820676e-17+1.44776060e-16j
 -4.78820676e-17-1.44776060e-16j -1.42799714e-16+0.00000000e+00j
 -9.64877686e-17+3.20525445e-17j -9.64877686e-17-3.20525445e-17j
  1.19574648e-16+7.45843735e-17j  1.19574648e-16-7.45843735e-17j
  3.47921122e-17+7.438521[...]<jupyter_text>----------  4. Algebraic ConnectivityAlgebraic connectivity and Fiedler vectors of undirected graphs.- ```algebraic_connectivity(G[, weight, …])``` 	Returns the algebraic connectivity of an undirected graph.- ```fiedler_vector(G[, weight, normalized, tol, …])``` 	Returns the Fiedler vector of a connected undirected graph.- ```spectral_ordering(G[, weight, normalized, …])``` 	Compute the spectral_ordering of a graph.  4.1 Algebraic ConnectivityReturns the algebraic connectivity of an undirected graph.The algebraic connectivity of a connected undirected graph is the second smallest eigenvalue of its Laplacian matrix.<jupyter_code>A = nx.algebraic_connectivity(G)
print(A)<jupyter_output>0.4685252267014026<jupyter_text>4.2. Fiedler VectorReturns the Fiedler vector of a connected undirected graph.The Fiedler vector of a connected undirected graph is the eigenvector corresponding to the second smallest eigenvalue of the Laplacian matrix of of the graph.<jupyter_code>A = nx.fiedler_vector(G)
print(A)<jupyter_output>[-0.11213744 -0.0412879   0.02321895 -0.0554998  -0.28460451 -0.32372719
 -0.32372719 -0.05258602  0.05160127  0.09280089 -0.28460451 -0.21099307
 -0.10946132 -0.01474198  0.16275079  0.16275079 -0.42276527 -0.10018145
  0.16275079 -0.01363714  0.16275079 -0.10018145  0.16275079  0.15569458
  0.15302559  0.16096293  0.18710958  0.12766405  0.0951523   0.16765032
  0.07349963  0.09875343  0.13034546  0.11890327]<jupyter_text>4.3 Spectral OrderingCompute the spectral_ordering of a graph.The spectral ordering of a graph is an ordering of its nodes where nodes in the same weakly connected components appear contiguous and ordered by their corresponding elements in the Fiedler vector of the component.<jupyter_code>A = nx.spectral_ordering(G)
print(A)<jupyter_output>[16, 6, 5, 10, 4, 11, 0, 12, 21, 17, 3, 7, 1, 13, 19, 2, 8, 30, 9, 28, 31, 33, 27, 32, 24, 23, 25, 18, 20, 15, 14, 22, 29, 26]<jupyter_text>-----------------  5. Attribute MatricesFunctions for constructing matrix-like objects from graph attributes.- ```attr_matrix(G[, edge_attr, node_attr, …])``` 	Returns a NumPy matrix using attributes from G.- ```attr_sparse_matrix(G[, edge_attr, …])``` 	Returns a SciPy sparse matrix using attributes from G.  5.1  Attribute MatrixReturns a NumPy matrix using attributes from G.If only G is passed in, then the adjacency matrix is constructed.Let A be a discrete set of values for the node attribute node_attr. Then the elements of A represent the rows and columns of the constructed matrix. Now, iterate through every edge e=(u,v) in G and consider the value of the edge attribute edge_attr. If ua and va are the values of the node attribute node_attr for u and v, respectively, then the value of the edge attribute is added to the matrix element at (ua, va).<jupyter_code>A = nx.attr_matrix(G)
print(A)<jupyter_output>(matrix([[0., 1., 1., ..., 1., 0., 0.],
        [1., 0., 1., ..., 0., 0., 0.],
        [1., 1., 0., ..., 0., 1., 0.],
        ...,
        [1., 0., 0., ..., 0., 1., 1.],
        [0., 0., 1., ..., 1., 0., 1.],
        [0., 0., 0., ..., 1., 1., 0.]]), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33])<jupyter_text>5.2 Attribute Sparse MatrixReturns a SciPy sparse matrix using attributes from G.If only G is passed in, then the adjacency matrix is constructed.Let A be a discrete set of values for the node attribute node_attr. Then the elements of A represent the rows and columns of the constructed matrix. Now, iterate through every edge e=(u,v) in G and consider the value of the edge attribute edge_attr. If ua and va are the values of the node attribute node_attr for u and v, respectively, then the value of the edge attribute is added to the matrix element at (ua, va).<jupyter_code>A = nx.attr_sparse_matrix(G)
print(A)<jupyter_output>(<34x34 sparse matrix of type '<class 'numpy.float64'>'
	with 156 stored elements in List of Lists format>, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33])
{'fraction_non_alphanumeric': 0.1410820605746829, 'fraction_numerical': 0.23712140823194408, 'mean_word_length': 3.3555054490792937, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 58, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '376015', 'license': 'MIT', 'n_tokens_mistral': 6399, 'n_tokens_neox': 4607, 'n_words': 1224, 'path': 'Python/2. Python Basics (cont.)/4. Visualizations/Networks Science and Advanced Visualizations/networkx_holoviews_pyvis_bokeh_advanced/3_networkx_Algebra.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '11589'}