Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/linalg/spectrum.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 Eigenvalue spectrum of graphs. | |
3 """ | |
4 import networkx as nx | |
5 | |
6 __all__ = [ | |
7 "laplacian_spectrum", | |
8 "adjacency_spectrum", | |
9 "modularity_spectrum", | |
10 "normalized_laplacian_spectrum", | |
11 "bethe_hessian_spectrum", | |
12 ] | |
13 | |
14 | |
15 def laplacian_spectrum(G, weight="weight"): | |
16 """Returns eigenvalues of the Laplacian of G | |
17 | |
18 Parameters | |
19 ---------- | |
20 G : graph | |
21 A NetworkX graph | |
22 | |
23 weight : string or None, optional (default='weight') | |
24 The edge data key used to compute each value in the matrix. | |
25 If None, then each edge has weight 1. | |
26 | |
27 Returns | |
28 ------- | |
29 evals : NumPy array | |
30 Eigenvalues | |
31 | |
32 Notes | |
33 ----- | |
34 For MultiGraph/MultiDiGraph, the edges weights are summed. | |
35 See to_numpy_array for other options. | |
36 | |
37 See Also | |
38 -------- | |
39 laplacian_matrix | |
40 """ | |
41 from scipy.linalg import eigvalsh | |
42 | |
43 return eigvalsh(nx.laplacian_matrix(G, weight=weight).todense()) | |
44 | |
45 | |
46 def normalized_laplacian_spectrum(G, weight="weight"): | |
47 """Return eigenvalues of the normalized Laplacian of G | |
48 | |
49 Parameters | |
50 ---------- | |
51 G : graph | |
52 A NetworkX graph | |
53 | |
54 weight : string or None, optional (default='weight') | |
55 The edge data key used to compute each value in the matrix. | |
56 If None, then each edge has weight 1. | |
57 | |
58 Returns | |
59 ------- | |
60 evals : NumPy array | |
61 Eigenvalues | |
62 | |
63 Notes | |
64 ----- | |
65 For MultiGraph/MultiDiGraph, the edges weights are summed. | |
66 See to_numpy_array for other options. | |
67 | |
68 See Also | |
69 -------- | |
70 normalized_laplacian_matrix | |
71 """ | |
72 from scipy.linalg import eigvalsh | |
73 | |
74 return eigvalsh(nx.normalized_laplacian_matrix(G, weight=weight).todense()) | |
75 | |
76 | |
77 def adjacency_spectrum(G, weight="weight"): | |
78 """Returns eigenvalues of the adjacency matrix of G. | |
79 | |
80 Parameters | |
81 ---------- | |
82 G : graph | |
83 A NetworkX graph | |
84 | |
85 weight : string or None, optional (default='weight') | |
86 The edge data key used to compute each value in the matrix. | |
87 If None, then each edge has weight 1. | |
88 | |
89 Returns | |
90 ------- | |
91 evals : NumPy array | |
92 Eigenvalues | |
93 | |
94 Notes | |
95 ----- | |
96 For MultiGraph/MultiDiGraph, the edges weights are summed. | |
97 See to_numpy_array for other options. | |
98 | |
99 See Also | |
100 -------- | |
101 adjacency_matrix | |
102 """ | |
103 from scipy.linalg import eigvals | |
104 | |
105 return eigvals(nx.adjacency_matrix(G, weight=weight).todense()) | |
106 | |
107 | |
108 def modularity_spectrum(G): | |
109 """Returns eigenvalues of the modularity matrix of G. | |
110 | |
111 Parameters | |
112 ---------- | |
113 G : Graph | |
114 A NetworkX Graph or DiGraph | |
115 | |
116 Returns | |
117 ------- | |
118 evals : NumPy array | |
119 Eigenvalues | |
120 | |
121 See Also | |
122 -------- | |
123 modularity_matrix | |
124 | |
125 References | |
126 ---------- | |
127 .. [1] M. E. J. Newman, "Modularity and community structure in networks", | |
128 Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006. | |
129 """ | |
130 from scipy.linalg import eigvals | |
131 | |
132 if G.is_directed(): | |
133 return eigvals(nx.directed_modularity_matrix(G)) | |
134 else: | |
135 return eigvals(nx.modularity_matrix(G)) | |
136 | |
137 | |
138 def bethe_hessian_spectrum(G, r=None): | |
139 """Returns eigenvalues of the Bethe Hessian matrix of G. | |
140 | |
141 Parameters | |
142 ---------- | |
143 G : Graph | |
144 A NetworkX Graph or DiGraph | |
145 | |
146 r : float | |
147 Regularizer parameter | |
148 | |
149 Returns | |
150 ------- | |
151 evals : NumPy array | |
152 Eigenvalues | |
153 | |
154 See Also | |
155 -------- | |
156 bethe_hessian_matrix | |
157 | |
158 References | |
159 ---------- | |
160 .. [1] A. Saade, F. Krzakala and L. Zdeborová | |
161 "Spectral clustering of graphs with the bethe hessian", | |
162 Advances in Neural Information Processing Systems. 2014. | |
163 """ | |
164 from scipy.linalg import eigvalsh | |
165 | |
166 return eigvalsh(nx.bethe_hessian_matrix(G, r).todense()) |