@@ -411,22 +411,44 @@ def sample(self, n_samples=1, random_state=None):
411
411
num_comp_in_X , random_state = random_state ).T
412
412
return X
413
413
414
- def fit (self , X , y = None ):
415
- """Estimate model parameters with the expectation-maximization
416
- algorithm.
414
+ def fit_predict (self , X , y = None ):
415
+ """
416
+ Fit and then predict labels for data.
417
+ Warning: due to the final maximization step in the EM algorithm,
418
+ with low iterations the prediction may not be 100% accurate
417
419
418
- A initialization step is performed before entering the em
419
- algorithm. If you want to avoid this step, set the keyword
420
- argument init_params to the empty string '' when creating the
421
- GMM object. Likewise, if you would like just to do an
422
- initialization, set n_iter=0.
420
+ Parameters
421
+ ----------
422
+ X : array-like, shape = [n_samples, n_features]
423
+
424
+ Returns
425
+ -------
426
+ C : array, shape = (n_samples,)
427
+ """
428
+ return self ._fit (X , y ).argmax (axis = 1 )
429
+
430
+ def _fit (self , X , y = None , do_prediction = False ):
431
+ """Estimate model parameters with the EM algorithm.
432
+
433
+ A initialization step is performed before entering the
434
+ expectation-maximization (EM) algorithm. If you want to avoid
435
+ this step, set the keyword argument init_params to the empty
436
+ string '' when creating the GMM object. Likewise, if you would
437
+ like just to do an initialization, set n_iter=0.
423
438
424
439
Parameters
425
440
----------
426
441
X : array_like, shape (n, n_features)
427
442
List of n_features-dimensional data points. Each row
428
443
corresponds to a single data point.
444
+
445
+ Returns
446
+ -------
447
+ responsibilities : array, shape (n_samples, n_components)
448
+ Posterior probabilities of each mixture component for each
449
+ observation
429
450
"""
451
+
430
452
# initialization step
431
453
X = check_array (X , dtype = np .float64 )
432
454
if X .shape [0 ] < self .n_components :
@@ -501,10 +523,33 @@ def fit(self, X, y=None):
501
523
self .covars_ = best_params ['covars' ]
502
524
self .means_ = best_params ['means' ]
503
525
self .weights_ = best_params ['weights' ]
526
+ else :
527
+ # Need to make sure that there are responsibilities to output
528
+ # Output zeros because it was just a quick initialization
529
+ responsibilities = np .zeros (X .shape [0 ], self .n_components )
530
+
531
+ return responsibilities
532
+
533
+ def fit (self , X , y = None ):
534
+ """Estimate model parameters with the EM algorithm.
535
+
536
+ A initialization step is performed before entering the
537
+ expectation-maximization (EM) algorithm. If you want to avoid
538
+ this step, set the keyword argument init_params to the empty
539
+ string '' when creating the GMM object. Likewise, if you would
540
+ like just to do an initialization, set n_iter=0.
541
+
542
+ Parameters
543
+ ----------
544
+ X : array_like, shape (n, n_features)
545
+ List of n_features-dimensional data points. Each row
546
+ corresponds to a single data point.
547
+ """
548
+ self ._fit (X , y )
504
549
return self
505
550
506
551
def _do_mstep (self , X , responsibilities , params , min_covar = 0 ):
507
- """ Perform the Mstep of the EM algorithm and return the class weihgts.
552
+ """ Perform the Mstep of the EM algorithm and return the class weights
508
553
"""
509
554
weights = responsibilities .sum (axis = 0 )
510
555
weighted_X_sum = np .dot (responsibilities .T , X )
0 commit comments