@@ -142,7 +142,7 @@ class DPGMM(GMM):
142
142
higher alpha means more clusters, as the expected number
143
143
of clusters is ``alpha*log(N)``.
144
144
145
- thresh : float, default 1e-2
145
+ tol : float, default 1e-3
146
146
Convergence threshold.
147
147
148
148
n_iter : int, default 10
@@ -198,13 +198,13 @@ class DPGMM(GMM):
198
198
"""
199
199
200
200
def __init__ (self , n_components = 1 , covariance_type = 'diag' , alpha = 1.0 ,
201
- random_state = None , thresh = 1e-2 , verbose = False ,
201
+ random_state = None , thresh = None , tol = 1e-3 , verbose = False ,
202
202
min_covar = None , n_iter = 10 , params = 'wmc' , init_params = 'wmc' ):
203
203
self .alpha = alpha
204
204
self .verbose = verbose
205
205
super (DPGMM , self ).__init__ (n_components , covariance_type ,
206
- random_state = random_state ,
207
- thresh = thresh , min_covar = min_covar ,
206
+ random_state = random_state , thresh = thresh ,
207
+ tol = tol , min_covar = min_covar ,
208
208
n_iter = n_iter , params = params ,
209
209
init_params = init_params )
210
210
@@ -503,13 +503,13 @@ def fit(self, X, y=None):
503
503
"""
504
504
self .random_state_ = check_random_state (self .random_state )
505
505
506
- ## initialization step
506
+ # initialization step
507
507
X = check_array (X )
508
508
if X .ndim == 1 :
509
509
X = X [:, np .newaxis ]
510
510
511
- n_features = X .shape [ 1 ]
512
- z = np .ones ((X . shape [ 0 ] , self .n_components ))
511
+ n_samples , n_features = X .shape
512
8000
+ z = np .ones ((n_samples , self .n_components ))
513
513
z /= self .n_components
514
514
515
515
self ._initial_bound = - 0.5 * n_features * np .log (2 * np .pi )
@@ -550,7 +550,7 @@ def fit(self, X, y=None):
550
550
self .dof_ , self .scale_ , self .det_scale_ , n_features )
551
551
self .bound_prec_ -= 0.5 * self .dof_ * np .trace (self .scale_ )
552
552
elif self .covariance_type == 'full' :
553
- self .dof_ = (1 + self .n_components + X . shape [ 0 ] )
553
+ self .dof_ = (1 + self .n_components + n_samples )
554
554
self .dof_ *= np .ones (self .n_components )
555
555
self .scale_ = [2 * np .identity (n_features )
556
556
for _ in range (self .n_components )]
@@ -566,18 +566,31 @@ def fit(self, X, y=None):
566
566
np .trace (self .scale_ [k ]))
567
567
self .bound_prec_ *= 0.5
568
568
569
- logprob = []
569
+ # EM algorithms
570
+ current_log_likelihood = None
570
571
# reset self.converged_ to False
571
572
self .converged_ = False
573
+
574
+ # this line should be removed when 'thresh' is removed in v0.18
575
+ tol = (self .tol if self .thresh is None
576
+ else self .thresh / float (n_samples ))
577
+
572
578
for i in range (self .n_iter ):
579
+ prev_log_likelihood = current_log_likelihood
573
580
# Expectation step
574
581
curr_logprob , z = self .score_samples (X )
575
- logprob .append (curr_logprob .sum () + self ._logprior (z ))
582
+
583
+ current_log_likelihood = (
584
+ curr_logprob .mean () + self ._logprior (z ) / n_samples )
576
585
577
586
# Check for convergence.
578
- if i > 0 and abs (logprob [- 1 ] - logprob [- 2 ]) < self .thresh :
579
- self .converged_ = True
580
- break
587
+ # (should
9E88
compare to self.tol when dreprecated 'thresh' is
588
+ # removed in v0.18)
589
+ if prev_log_likelihood is not None :
590
+ change = abs (current_log_likelihood - prev_log_likelihood )
591
+ if change < tol :
592
+ self .converged_ = True
593
+ break
581
594
582
595
# Maximization step
583
596
self ._do_mstep (X , z , self .params )
@@ -613,7 +626,7 @@ class VBGMM(DPGMM):
613
626
value of alpha the more likely the variational mixture of
614
627
Gaussians model will use all components it can.
615
628
616
- thresh : float, default 1e-2
629
+ tol : float, default 1e-3
617
630
Convergence threshold.
618
631
619
632
n_iter : int, default 10
@@ -671,11 +684,11 @@ class VBGMM(DPGMM):
671
684
"""
672
685
673
686
def __init__ (self , n_components = 1 , covariance_type = 'diag' , alpha = 1.0 ,
674
- random_state = None , thresh = 1e-2 , verbose = False ,
687
+ random_state = None , thresh = None , tol = 1e-3 , verbose = False ,
675
688
min_covar = None , n_iter = 10 , params = 'wmc' , init_params = 'wmc' ):
676
689
super (VBGMM , self ).__init__ (
677
690
n_components , covariance_type , random_state = random_state ,
678
- thresh = thresh , verbose = verbose , min_covar = min_covar ,
691
+ thresh = thresh , tol = tol , verbose = verbose , min_covar = min_covar ,
679
692
n_iter = n_iter , params = params , init_params = init_params )
680
693
self .alpha = float (alpha ) / n_components
681
694
0 commit comments