Package gmisclib :: Module matrix_rearrange_labels
[frames] | no frames]

Module matrix_rearrange_labels

source code

This module helps you plot confusion matrices or similar images where the axis labels do not have a natural order. So, the probability of confusing two phonemes is a perfect example: phonemes do not naturally fall onto a 1-dimensional sequence, so one is free to put them in any order one likes. Given that, one might as well put them into an order that reveals something interesting about the probabilities.

All the functions ending in "2" work on rectangular arrays. All the functions without "2" work only on square arrays, and they assume that that both axes remain in the same order.

Classes
  diagfom
A class that defines a figure-of-merit for a matrix.
  diagfom2
This tries to make your matrix approximately diagonal for rectangular matrices.
  blockfom
This tries to make your matrix into a block form.
Functions
 
symm_swap_toward_minimal_fom(m, fom, maxtries)
Used internally.
source code
 
swap_toward_minimal_fom(m, fom, maxtries)
Used internally.
source code
tuple(numpy.ndarray, list(something))
swap_toward_diag(m, lbls, maxtries=None, sign=1)
Swap the rows and columns of a matrix to bring it closer to a diagonal matrix: i.e.
source code
tuple(numpy.ndarray, list(something))
swap_toward_positive(m, lbls, maxtries=None, sign=1)
Swap the rows and columns of a matrix to put the most positive values on the main diagonal.
source code
tuple(numpy.ndarray, list(something), list(something))
swap_toward_diag2(m, lbl1, lbl2, maxtries=None)
Swap the rows and columns of a matrix to make it roughly diagonal: i.e.
source code
 
pos_near_diag2(m, lbl1, lbl2, maxtries=None) source code
 
neg_near_diag2(m, lbl1, lbl2, maxtries=None) source code
tuple(numpy.ndarray, list(something), list(something))
swap_toward_blocks2(m, lbl1, lbl2, maxtries=None)
Swap rows and columns of a matrix to bring it closer to a block form, where similar values occur together in blocks.
source code
tuple(numpy.ndarray), list(something))
swap_toward_blocks(m, lbls, maxtries=None)
Swap rows and columns of a matrix to bring it closer to a block form, where similar values occur together in blocks.
source code
 
test1()
Two test cases on 2x2 matrices.
source code
 
test() source code
Variables
  __package__ = 'gmisclib'

Imports: numpy, die, blue_data_selector


Function Details

symm_swap_toward_minimal_fom(m, fom, maxtries)

source code 

Used internally. Finds an ordering of the labels that minimizes whatever fom you supply. Both axes will have the same order.

swap_toward_minimal_fom(m, fom, maxtries)

source code 

Used internally. Finds an ordering of the labels that minimizes whatever fom you supply. The ordering of the two axes may be different.

swap_toward_diag(m, lbls, maxtries=None, sign=1)

source code 

Swap the rows and columns of a matrix to bring it closer to a diagonal matrix: i.e. entries with large absolute values on the main diagonal and small entries away from the main diagonal. Rows and columns are swapped together, so that the ordering or rows will match the ordering of columns.

Parameters:
  • m (numpy.ndarray) - a 2-dimensional array
  • lbls (list(anything)) - a list of arbitrary labels for each row or column.
  • maxtries (int or None) - how hard to work at optimizing the matrix layout? None gives a resonable default value. Appropriate values are a few times the number of elements in the matrix.
  • sign (int) - (default=1) if sign=-1, do the opposite: put the small absolute values on the main diagonal.
Returns: tuple(numpy.ndarray, list(something))
A tuple containing the swapped matrix and a swapped list of values.

swap_toward_positive(m, lbls, maxtries=None, sign=1)

source code 

Swap the rows and columns of a matrix to put the most positive values on the main diagonal. Rows and columns are swapped together, so that the ordering or rows will match the ordering of columns.

Parameters:
  • m (numpy.ndarray) - a 2-dimensional array
  • lbls (list(anything)) - a list of arbitrary labels for each row or column.
  • maxtries (int or None) - how hard to work at optimizing the matrix layout? None gives a resonable default value. Appropriate values are a few times the number of elements in the matrix.
  • sign (int) - (default=1) if sign=-1, do the opposite: put the negative entries on the main diagonal.
Returns: tuple(numpy.ndarray, list(something))
A tuple containing the swapped matrix and a swapped list of labels.

swap_toward_diag2(m, lbl1, lbl2, maxtries=None)

source code 

Swap the rows and columns of a matrix to make it roughly diagonal: i.e. large entries on the main diagonal and small entries away from the main diagonal. Note that this will work even if the matrix is not square.

Parameters:
  • m (numpy.ndarray) - a 2-dimensional array
  • lbl1 (list(anything)) - a list of arbitrary labels for the first index of the matrix m.
  • lbl2 (list(anything)) - a list of arbitrary labels for the second index of the matrix m.
  • maxtries (int or None) - how hard to work at optimizing the matrix layout? None gives a resonable default value. Appropriate values are a few times the number of elements in the matrix.
Returns: tuple(numpy.ndarray, list(something), list(something))
A tuple containing the swapped matrix and the two swapped lists of labels, one for the first and one for the second axis.

swap_toward_blocks2(m, lbl1, lbl2, maxtries=None)

source code 

Swap rows and columns of a matrix to bring it closer to a block form, where similar values occur together in blocks.

Parameters:
  • m (numpy.ndarray) - a 2-dimensional array
  • lbl1 (list(anything)) - a list of arbitrary labels for the first axis of m
  • lbl2 (list(anything)) - a list of arbitrary labels for the second axis of m
  • maxtries (int or None) - how hard to work at optimizing the matrix layout? None gives a resonable default value. Appropriate values are a few times the number of elements in the matrix.
Returns: tuple(numpy.ndarray, list(something), list(something))
A tuple containing the swapped matrix and the two swapped lists of values, one for the first and one for the second axis.

swap_toward_blocks(m, lbls, maxtries=None)

source code 

Swap rows and columns of a matrix to bring it closer to a block form, where similar values occur together in blocks. It minimizes the sum of changes when proceeding along a row or column.

Parameters:
  • m (numpy.ndarray) - a 2-dimensional array
  • lbls (list(anything)) - a list of arbitrary labels for the first axis of m
  • maxtries (int or None) - how hard to work at optimizing the matrix layout? None gives a resonable default value. Appropriate values are a few times the number of elements in the matrix.
Returns: tuple(numpy.ndarray), list(something))
A tuple containing the swapped matrix and a swapped lists of values.