NMFClassifier

Histogram classification based on nonnegative matrix factorization

Specifically intended for 2D histograms, but should in principle work for 1D as well.
It is basically a wrapper for a sklearn.decomposition.NMF instance.



[class] NMFClassifier

comments:

histogram classification based on nonnegative matrix factorization  
specifically intended for 2D histograms, but should in principle work for 1D as well.  
it is basically a wrapper for a sklearn.decomposition.NMF instance.  

⤷ __init__

full signature:

def __init__( self, ncomponents=5, loss_type='mse', nmax=10 )  

comments:

initializer  
input arguments:  
- ncomponents: number of NMF components (aka clusters aka basis vectors) to use in the decomposition  
- loss_type: choose from 'mse' (mean-squared-error) or 'chi2' (chi squared error)  
- nmax: number of largest elements to keep in error calculation  
TODO: add keyword arguments to pass down to sklearn.decomposition.NMF  

⤷ train

full signature:

def train( self, histograms, doplot=True, ncols=None, title=None )  

comments:

train the NMF model on a given set of input histograms  
input arguments:  
- histograms: a numpy array of shape (nhists,nbins) or (nhists,nybins,nxbins) that will be used to fit a NMF model  

⤷ set_nmax

full signature:

def set_nmax( self, nmax )  

comments:

set number of largest elements to keep in mean square error calculation  
useful to quickly re-evaluate the model with different nmax without retraining  
input arguments:  
- nmax: number of largest elements to keep in mean square error calculation  

⤷ set_loss_type

full signature:

def set_loss_type( self, loss_type )  

comments:

set loss type  
useful to quickly re-evaluate the model with different loss without retraining  
input arguments:  
- loss_type: choose from 'mse' (mean-squared-error) or 'chi2' (chi squared error)  

⤷ evaluate

full signature:

def evaluate( self, histograms )  

comments:

classify the given histograms based on the MSE with respect to their reconstructed version  
input arguments:  
- histograms: numpy array of shape (nhists,nbins) or (nhists,nybins,nxbins)  

⤷ get_components

full signature:

def get_components( self )  

comments:

return the NMF components (aka cluster centers aka basis vectors)  
output:  
a numpy array of shape (ncomponents,nbins) or (ncomponents,nybins,nxbins)  

⤷ reconstruct

full signature:

def reconstruct( self, histograms )  

comments:

return the NMF reconstruction for a given set of histograms  
input arguments:  
- histograms: numpy array of shape (nhists,nbins) or (nhists,nybins,nxbins)  

⤷ save

full signature:

def save( self, path )  

comments:

save the underlying NMF model  

⤷ load

full signature:

def load( self, path, **kwargs )  

comments:

get an NMFClassifier instance from a pkl file