<s> import numpy as np <EOL> from scipy . ndimage . filters import gaussian_filter <EOL> class ProbDensityHistogram ( object ) : <EOL> def train ( self , training_data , bins = <NUM_LIT:100> , bandwidth = <NUM_LIT:3> , smoothing = <NUM_LIT> ) : <EOL> self . ndim = training_data . shape [ <NUM_LIT:1> ] <EOL> self . bins = bins <EOL> self . hist , self . edges = np . histogramdd ( <EOL> training_data , bins = bins , normed = True ) <EOL> self . hist [ self . hist < smoothing ] = smoothing <EOL> if bandwidth : <EOL> self . hist = gaussian_filter ( self . hist , sigma = bandwidth ) <EOL> self . hist = np . log ( self . hist ) <EOL> def logprob ( self , samples ) : <EOL> indices = [ np . digitize ( samples [ : , i ] , self . edges [ i ] ) <EOL> for i in xrange ( self . ndim ) ] <EOL> for i in xrange ( self . ndim ) : <EOL> np . clip ( indices [ i ] , <NUM_LIT:0> , self . bins - <NUM_LIT:1> , out = indices [ i ] ) <EOL> ret = self . hist [ indices ] <EOL> assert ret . shape [ <NUM_LIT:0> ] == samples . shape [ <NUM_LIT:0> ] <EOL> return ret </s>
