graph
spectral_permute(B, labels, mode='tw')
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 confusion matrix to be reordered, shape (n_classes, n_classes) |
required |
labels
|
ndarray
|
Class labels corresponding to matrix rows/columns, shape (n_classes,) |
required |
mode
|
(tw, fiedler)
|
Spectral reordering method: - 'tw': Use two-walk Laplacian for bipartite graph analysis - 'fiedler': Use standard Fiedler vector approach |
'tw'
|
Returns:
Name | Type | Description |
---|---|---|
reordered_cm |
ndarray
|
Reordered confusion matrix with revealed block structure |
reordered_labels |
ndarray
|
Class labels reordered to match the permuted matrix rows/columns |
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 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)
Source code in mheatmap/graph/_spectral_permute.py
36 37 38 39 40 41 42 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 |
|
copermute_from_bipermute(B_sizes, B_subrows, B_subcols, p_Asub)
Copermute from bi-permutation.
Renders row permutation 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
|
A 1x2 array containing the dimensions of matrix B: [nrows, ncols] |
required |
B_subrows
|
array_like
|
Row indices defining the submatrix Bsub, nrBsub x 1 integer array where nrBsub <= nrB |
required |
B_subcols
|
array_like
|
Column indices defining the submatrix Bsub, ncBsub x 1 integer array where ncBsub <= ncB |
required |
p_Asub
|
array_like
|
Permutation vector for the symmetric embedding of Bsub, (nr+nc)x1 integer array |
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 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) |
Bsub_rows |
ndarray
|
Indices of non-zero rows in the input matrix |
Bsub_cols |
ndarray
|
Indices of non-zero columns in the input matrix |
Notes
The two-walk adjacency matrix A_tw has the block structure: [BB^T αB ] [αB^T B^TB ]
where α 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:
>>> B = np.array([[1, 0], [1, 1]])
>>> L_tw, rows, cols = two_walk_laplacian(B)
>>> print(L_tw.shape)
(4, 4)