Skip to content

Commit 1a3fd94

Browse files
csytracylarsmans
authored andcommitted
FIX "ValueError: startprob must sum to 1.0" in HMMs
The reason for these error is that the value of self._covars_ is nan. Adding a minimum denominator 1e-5 will solve these.
1 parent ee85da8 commit 1a3fd94

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

sklearn/hmm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ def _init(self, obs, params='stmc'):
786786
cv.shape = (1, 1)
787787
self._covars_ = distribute_covar_matrix_to_match_covariance_type(
788788
cv, self._covariance_type, self.n_components)
789+
self._covars_[self._covars_==0] = 1e-5
789790

790791
def _initialize_sufficient_statistics(self):
791792
stats = super(GaussianHMM, self)._initialize_sufficient_statistics()
@@ -850,7 +851,7 @@ def _do_mstep(self, stats, params):
850851
- 2 * self._means_ * stats['obs']
851852
+ self._means_ ** 2 * denom)
852853
cv_den = max(covars_weight - 1, 0) + denom
853-
self._covars_ = (covars_prior + cv_num) / cv_den
854+
self._covars_ = (covars_prior + cv_num) / np.maximum(cv_den, 1e-5)
854855
if self._covariance_type == 'spherical':
855856
self._covars_ = np.tile(
856857
self._covars_.mean(1)[:, np.newaxis],

0 commit comments

Comments
 (0)