def test_fit_invalid(): """Fit the classifier with an invalid""" clf = HMMClassifier() with pytest.raises(TypeError) as e: clf.fit(tuple(hmm_list)) assert str( e.value) == 'Expected models to be a list or dict of HMM objects'
def test_fit_dict_invalid(): """Fit the classifier using a dict of invalid types""" clf = HMMClassifier() with pytest.raises(TypeError) as e: clf.fit({'a': 0, 'b': 0}) assert str(e.value) == 'Expected all models to be HMM objects'
def test_fit_dict_empty(): """Fit the classifier using an empty dict""" clf = HMMClassifier() with pytest.raises(RuntimeError) as e: clf.fit({}) assert str(e.value) == 'Must fit the classifier with at least one HMM'
def test_fit_dict(): """Fit the classifier using a dict of HMMs""" clf = HMMClassifier() clf.fit(hmm_dict) assert clf._models == list(hmm_dict.values())
def test_fit_list(): """Fit the classifier using a list of HMMs""" clf = HMMClassifier() clf.fit(hmm_list) assert clf._models == hmm_list
hmm.set_random_initial() hmm.set_random_transitions() with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=DeprecationWarning) hmm.fit( [np.arange((i + j * 20) * 30).reshape(-1, 3) for j in range(2, 5)]) hmm_list.append(hmm) hmm_dict[label] = hmm # Create some sample test data and labels X = [np.arange((i + 2 * 20) * 30).reshape(-1, 3) for i in range(2, 5)] Y = ['c0', 'c1', 'c1'] x, y = X[0], 'c1' # Fit a classifier hmm_clf = HMMClassifier() hmm_clf.fit(hmm_list) # =================== # # HMMClassifier.fit() # # =================== # def test_fit_list(): """Fit the classifier using a list of HMMs""" clf = HMMClassifier() clf.fit(hmm_list) assert clf._models == hmm_list def test_fit_list_empty():
import numpy as np from sequentia.classifiers import HMM, HMMClassifier # Set of possible labels labels = [f'class{i}' for i in range(5)] # Create and fit some sample HMMs hmms = [] for i, label in enumerate(labels): hmm = HMM(label=label, n_states=(i + 3), topology='left-right') hmm.set_random_initial() hmm.set_random_transitions() hmm.fit([np.arange((i + j * 20) * 30).reshape(-1, 3) for j in range(1, 4)]) hmms.append(hmm) # Create some sample test data and labels X = [np.random.random((10 * i, 3)) for i in range(1, 4)] y = ['class0', 'class1', 'class1'] # Create a classifier and calculate predictions and evaluations clf = HMMClassifier() clf.fit(hmms) predictions = clf.predict(X) accuracy, confusion = clf.evaluate(X, y, labels=labels)