graph
spectral_permute(B, row_labels, mode='tw', *, col_labels=None)
Perform spectral reordering of a confusion matrix using graph Laplacian eigenvectors.
This function implements spectral reordering to reveal block structures in confusion matrices by analyzing the eigenvectors of the graph Laplacian. The reordering is based on the Fiedler vector (eigenvector corresponding to the second smallest eigenvalue), which provides an optimal ordering that groups similar classes together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B
|
ndarray
|
Input matrix to be reordered. For |
required |
row_labels
|
ndarray
|
Class labels corresponding to matrix rows, shape |
None
|
mode
|
(tw, fiedler)
|
Spectral reordering method:
|
'tw'
|
col_labels
|
ndarray | None
|
Column labels for rectangular matrices. When provided (not
|
None
|
labels
|
ndarray
|
Deprecated alias for |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
reordered_cm |
ndarray
|
Reordered matrix with revealed block structure. |
reordered_labels |
ndarray
|
Row labels reordered to match the permuted matrix rows. |
_SpectralPermuteResult (when ``col_labels`` is provided)
|
TypedDict with fields:
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
mheatmap.rms_permute : Alternative reordering using merge/split patterns mheatmap.amc_postprocess : Post-processing tools for confusion matrices mheatmap.graph.two_walk_laplacian : Two-walk Laplacian computation
Notes
The algorithm proceeds in the following steps: 1. For mode='tw': - Constructs two-walk Laplacian capturing bipartite graph structure - Handles isolated vertices automatically - Computes separate row and column permutations via co-permutation 2. For mode='fiedler': - Computes standard graph Laplacian L = D - A 3. Finds Fiedler vector (second smallest eigenvector) 4. Sorts vertices based on Fiedler vector components 5. Applies resulting permutation to matrix and labels
References
.. [1] Fiedler, M. (1973). Algebraic connectivity of graphs. Czechoslovak Mathematical Journal, 23(2), 298-305. .. [2] Sun, X. (2024). Matrix, Graph and Network Analysis. CS521 Course Notes, Duke University.
Examples:
>>> import numpy as np
>>> conf_mat = np.array([[5, 2, 0], [2, 3, 1], [0, 1, 4]])
>>> labels = np.array(['A', 'B', 'C'])
>>> reordered_mat, reordered_labs = spectral_permute(conf_mat, labels)
With column labels on a rectangular matrix (mode='tw' only):
>>> import numpy as np
>>> B = np.array([[1, 0.5, 0.2, 0.1], [0.3, 1, 0.8, 0.4], [0.1, 0.2, 1, 0.9]])
>>> row_labels = np.array(['r0', 'r1', 'r2'])
>>> col_labels = np.array(['c0', 'c1', 'c2', 'c3'])
>>> result = spectral_permute(B, row_labels, col_labels=col_labels)
>>> result['reordered_col_labels']
Source code in src/mheatmap/graph/_spectral_permute.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
copermute_from_bipermute(B_sizes, B_subrows, B_subcols, p_Asub)
Copermute from bi-permutation.
Renders row and column permutation of matrix B according to a co-permutation of a submatrix Bsub via a bi-permutation in its symmetric embedding: Asub = [[0, Bsub], [Bsub.T, 0]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B_sizes
|
array_like
|
Array of shape (2,) containing [nrows, ncols] of matrix B |
required |
B_subrows
|
array_like
|
Integer array of shape (nrBsub,) with row indices defining the submatrix Bsub, where nrBsub <= nrows |
required |
B_subcols
|
array_like
|
Integer array of shape (ncBsub,) with column indices defining the submatrix Bsub, where ncBsub <= ncols |
required |
p_Asub
|
array_like
|
Integer array of shape (nrBsub + ncBsub,) with permutation for the symmetric embedding of Bsub |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Examples:
>>> import numpy as np
>>> m, n = 5, 4 # matrix dimensions
>>> B_sizes = [m, n]
>>> # Use entire matrix as submatrix
>>> p_Brows, p_Bcols = copermute_from_bipermute(
... B_sizes,
... np.arange(1,m+1),
... np.arange(1,n+1),
... np.random.permutation(m+n)+1
... )
Notes
Revision of recover_nonsymmetric_perm.m All variables renamed to be self-evident + additional documentation Nov. 22, 2024
Source code in src/mheatmap/graph/_copermute_from_bipermute.py
two_walk_laplacia(B_sub, alpha=1)
Compute the two-walk Laplacian matrix of a bipartite graph.
For a bipartite graph with biadjacency matrix B, constructs the two-walk Laplacian by first forming the two-walk adjacency matrix A_tw and then computing L_tw = D_tw - A_tw, where D_tw is the diagonal degree matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
B_sub
|
ndarray
|
Biadjacency matrix of shape (m, n) representing connections between two vertex sets |
required |
alpha
|
float
|
Scaling factor for the adjacency matrix term in the Laplacian computation |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
L_tw |
ndarray
|
Two-walk Laplacian matrix of shape (m+n, m+n) |
Notes
The two-walk adjacency matrix A_tw has the block structure: [BB^T aB ] [aB^T B^TB ]
where a (alpha) is the scaling factor controlling the influence of direct connections.
The implementation automatically handles isolated vertices by removing rows/columns with all zeros before computation. The returned indices enable mapping back to the original matrix dimensions.
References
.. [1] Sun, X. (2024). Graph Algorithms for Matrix Analysis. CS521 Course Notes, Duke University.
Examples: