@@ -207,11 +207,11 @@ def __init__(self, n_components=1, covariance_type='diag', alpha=1.0,
207
207
208
208
def _get_precisions (self ):
209
209
"""Return precisions as a full matrix."""
210
- if self ._covariance_type == 'full' :
210
+ if self .covariance_type == 'full' :
211
211
return self .precs_
212
- elif self ._covariance_type in ['diag' , 'spherical' ]:
212
+ elif self .covariance_type in ['diag' , 'spherical' ]:
213
213
return [np .diag (cov ) for cov in self .precs_ ]
214
- elif self ._covariance_type == 'tied' :
214
+ elif self .covariance_type == 'tied' :
215
215
return [self .precs_ ] * self .n_components
216
216
217
217
def _get_covars (self ):
@@ -261,12 +261,12 @@ def eval(self, X):
261
261
# Free memory and developers cognitive load:
262
262
del dgamma1 , dgamma2 , sd
263
263
264
- if self ._covariance_type not in ['full' , 'tied' , 'diag' , 'spherical' ]:
264
+ if self .covariance_type not in ['full' , 'tied' , 'diag' , 'spherical' ]:
265
265
raise NotImplementedError ("This ctype is not implemented: %s"
266
- % self ._covariance_type )
266
+ % self .covariance_type )
267
267
p = _bound_state_log_lik (X , self ._initial_bound + self .bound_prec_ ,
268
268
self .precs_ , self .means_ ,
269
- self ._covariance_type )
269
+ self .covariance_type )
270
270
z = p + dgamma
271
271
z = log_normalize (z , axis = - 1 )
272
272
bound = np .sum (z * p , axis = - 1 )
@@ -285,13 +285,13 @@ def _update_means(self, X, z):
285
285
"""Update the variational distributions for the means"""
286
286
n_features = X .shape [1 ]
287
287
for k in xrange (self .n_components ):
288
- if self ._covariance_type in ['spherical' , 'diag' ]:
288
+ if self .covariance_type in ['spherical' , 'diag' ]:
289
289
num = np .sum (z .T [k ].reshape ((- 1 , 1 )) * X , axis = 0 )
290
290
num *= self .precs_ [k ]
291
291
den = 1. + self .precs_ [k ] * np .sum (z .T [k ])
292
292
self .means_ [k ] = num / den
293
- elif self ._covariance_type in ['tied' , 'full' ]:
294
- if self ._covariance_type == 'tied' :
293
+ elif self .covariance_type in ['tied' , 'full' ]:
294
+ if self .covariance_type == 'tied' :
295
295
cov = self .precs_
296
296
else :
297
297
cov = self .precs_ [k ]
@@ -303,7 +303,7 @@ def _update_means(self, X, z):
303
303
def _update_precisions (self , X , z ):
304
304
"""Update the variational distributions for the precisions"""
305
305
n_features = X .shape [1 ]
306
- if self ._covariance_type == 'spherical' :
306
+ if self .covariance_type == 'spherical' :
307
307
self .dof_ = 0.5 * n_features * np .sum (z , axis = 0 )
308
308
for k in xrange (self .n_components ):
309
309
# could be more memory efficient ?
@@ -315,7 +315,7 @@ def _update_precisions(self, X, z):
315
315
digamma (self .dof_ [k ]) - np .log (self .scale_ [k ])))
316
316
self .precs_ = np .tile (self .dof_ / self .scale_ , [n_features , 1 ]).T
317
317
318
- elif self ._covariance_type == 'diag' :
318
+ elif self .covariance_type == 'diag' :
319
319
for k in xrange (self .n_components ):
320
320
self .dof_ [k ].fill (1. + 0.5 * np .sum (z .T [k ], axis = 0 ))
321
321
sq_diff = (X - self .means_ [k ]) ** 2 # see comment above
@@ -326,7 +326,7 @@ def _update_precisions(self, X, z):
326
326
- np .log (self .scale_ [k ]))
327
327
self .bound_prec_ [k ] -= 0.5 * np .sum (self .precs_ [k ])
328
328
329
- elif self ._covariance_type == 'tied' :
329
+ elif self .covariance_type == 'tied' :
330
330
self .dof_ = 2 + X .shape [0 ] + n_features
331
331
self .scale_ = (X .shape [0 ] + 1 ) * np .identity (n_features )
332
332
for k in xrange (self .n_components ):
@@ -339,7 +339,7 @@ def _update_precisions(self, X, z):
339
339
self .dof_ , self .scale_ , self .det_scale_ , n_features )
340
340
self .bound_prec_ -= 0.5 * self .dof_ * np .trace (self .scale_ )
341
341
342
- elif self ._covariance_type == 'full' :
342
+ elif self .covariance_type == 'full' :
343
343
for k in xrange (self .n_components ):
344
344
sum_resp = np .sum (z .T [k ])
345
345
self .dof_ [k ] = 2 + sum_resp + n_features
@@ -367,7 +367,7 @@ def _monitor(self, X, z, n, end=False):
367
367
print "Bound after updating %8s: %f" % (n , self .lower_bound (X , z ))
368
368
if end == True :
369
369
print "Cluster proportions:" , self .gamma_ .T [1 ]
370
- print "covariance_type:" , self ._covariance_type
370
+ print "covariance_type:" , self .covariance_type
371
371
372
372
def _do_mstep (self , X , z , params ):
373
373
"""Maximize the variational lower bound
@@ -414,20 +414,20 @@ def _bound_means(self):
414
414
def _bound_precisions (self ):
415
415
"""Returns the bound term related to precisions"""
416
416
logprior = 0.
417
- if self ._covariance_type == 'spherical' :
417
+ if self .covariance_type == 'spherical' :
418
418
logprior += np .sum (gammaln (self .dof_ ))
419
419
logprior -= np .sum (
420
420
(self .dof_ - 1 ) * digamma (np .maximum (0.5 , self .dof_ )))
421
421
logprior += np .sum (- np .log (self .scale_ ) + self .dof_ - \
422
422
self .precs_ [:, 0 ])
423
- elif self ._covariance_type == 'diag' :
423
+ elif self .covariance_type == 'diag' :
424
424
logprior += np .sum (gammaln (self .dof_ ))
425
425
logprior -= np .sum (
426
426
(self .dof_ - 1 ) * digamma (np .maximum (0.5 , self .dof_ )))
427
427
logprior += np .sum (- np .log (self .scale_ ) + self .dof_ - self .precs_ )
428
- elif self ._covariance_type == 'tied' :
428
+ elif self .covariance_type == 'tied' :
429
429
logprior += _bound_wishart (self .dof_ , self .scale_ , self .det_scale_ )
430
- elif self ._covariance_type == 'full' :
430
+ elif self .covariance_type == 'full' :
431
431
for k in xrange (self .n_components ):
432
432
logprior += _bound_wishart (self .dof_ [k ],
433
433
self .scale_ [k ],
@@ -456,16 +456,16 @@ def _logprior(self, z):
456
456
457
457
def lower_bound (self , X , z ):
458
458
"""returns a lower bound on model evidence based on X and membership"""
459
- if self ._covariance_type not in ['full' , 'tied' , 'diag' , 'spherical' ]:
459
+ if self .covariance_type not in ['full' , 'tied' , 'diag' , 'spherical' ]:
460
460
raise NotImplementedError ("This ctype is not implemented: %s"
461
- % self ._covariance_type )
461
+ % self .covariance_type )
462
462
463
463
X = np .asarray (X )
464
464
if X .ndim == 1 :
465
465
X = X [:, np .newaxis ]
466
466
c = np .sum (z * _bound_state_log_lik (
467
467
X , self ._initial_bound + self .bound_prec_ ,
468
- self .precs_ , self .means_ , self ._covariance_type ))
468
+ self .precs_ , self .means_ , self .covariance_type ))
469
469
470
470
return c + self ._logprior (z )
471
471
@@ -526,29 +526,29 @@ def fit(self, X, **kwargs):
526
526
self .weights_ = np .tile (1.0 / self .n_components , self .n_components )
527
527
528
528
if 'c' in self .init_params or not hasattr (self , 'precs_' ):
529
- if self ._covariance_type == 'spherical' :
529
+ if self .covariance_type == 'spherical' :
530
530
self .dof_ = np .ones (self .n_components )
531
531
self .scale_ = np .ones (self .n_components )
532
532
self .precs_ = np .ones ((self .n_components , n_features ))
533
533
self .bound_prec_ = 0.5 * n_features * (
534
534
digamma (self .dof_ ) - np .log (self .scale_ ))
535
- elif self ._covariance_type == 'diag' :
535
+ elif self .covariance_type == 'diag' :
536
536
self .dof_ = 1 + 0.5 * n_features
537
537
self .dof_ *= np .ones ((self .n_components , n_features ))
538
538
self .scale_ = np .ones ((self .n_components , n_features ))
539
539
self .precs_ = np .ones ((self .n_components , n_features ))
540
540
self .bound_prec_ = 0.5 * (np .sum (digamma (self .dof_ ) -
541
541
np .log (self .scale_ ), 1 ))
542
542
self .bound_prec_ -= 0.5 * np .sum (self .precs_ , 1 )
543
- elif self ._covariance_type == 'tied' :
543
+ elif self .covariance_type == 'tied' :
544
544
self .dof_ = 1.
545
545
self .scale_ = np .identity (n_features )
546
546
self .precs_ = np .identity (n_features )
547
547
self .det_scale_ = 1.
548
548
self .bound_prec_ = 0.5 * wishart_log_det (
549
549
self .dof_ , self .scale_ , self .det_scale_ , n_features )
550
550
self .bound_prec_ -= 0.5 * self .dof_ * np .trace (self .scale_ )
551
- elif self ._covariance_type == 'full' :
551
+ elif self .covariance_type == 'full' :
552
552
self .dof_ = (1 + self .n_components + X .shape [0 ])
553
553
self .dof_ *= np .ones (self .n_components )
554
554
self .scale_ = [2 * np .identity (n_features )
@@ -692,12 +692,12 @@ def eval(self, X):
692
692
bound = np .zeros (X .shape [0 ])
693
693
dg = digamma (self .gamma_ ) - digamma (np .sum (self .gamma_ ))
694
694
695
- if self ._covariance_type not in ['full' , 'tied' , 'diag' , 'spherical' ]:
695
+ if self .covariance_type not in ['full' , 'tied' , 'diag' , 'spherical' ]:
696
696
raise NotImplementedError ("This ctype is not implemented: %s"
697
- % self ._covariance_type )
697
+ % self .covariance_type )
698
698
p = _bound_state_log_lik (
699
699
X , self ._initial_bound + self .bound_prec_ ,
700
- self .precs_ , self .means_ , self ._covariance_type )
700
+ self .precs_ , self .means_ , self .covariance_type )
701
701
702
702
z = p + dg
703
703
z = log_normalize (z , axis = - 1 )
@@ -741,4 +741,4 @@ def _monitor(self, X, z, n, end=False):
741
741
print "Bound after updating %8s: %f" % (n , self .lower_bound (X , z ))
742
742
if end == True :
743
743
print "Cluster proportions:" , self .gamma_
744
- print "covariance_type:" , self ._covariance_type
744
+ print "covariance_type:" , self .covariance_type
0 commit comments