#NEWLINE# def _computeDensities( self , sample ) : #NEWLINE# #INDENT# ' compute densities, and normalize ' #NEWLINE# densities = zeros ( self . numberOfCenters ) #NEWLINE# for c in range ( self . numberOfCenters ) : #NEWLINE# #INDENT# if self . diagonalOnly : #NEWLINE# #INDENT# pdf = product ( [ norm . pdf ( x , self . mus [ c ] [ i ] , self . sigmas [ c ] [ i ] ) for ( i , x ) in enumerate ( sample ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pdf = multivariateNormalPdf ( sample , self . mus [ c ] , self . sigmas [ c ] ) #NEWLINE# #UNINDENT# if ( pdf > 1e+40 ) : #NEWLINE# #INDENT# pdf = 1e+40 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( pdf < 1e-40 ) : #NEWLINE# #INDENT# pdf = 1e-40 #NEWLINE# #UNINDENT# #UNINDENT# if isnan ( pdf ) : #NEWLINE# #INDENT# print ( 'NaN!' ) #NEWLINE# pdf = 0.0 #NEWLINE# #UNINDENT# densities [ c ] = ( self . alphas [ c ] * pdf ) #NEWLINE# #UNINDENT# densities /= sum ( densities ) #NEWLINE# return densities