Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/eonu/sequentia into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
eonu committed Jan 6, 2021
2 parents 5703f56 + 7704bb1 commit e7f15a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
16 changes: 8 additions & 8 deletions lib/sequentia/classifiers/hmm/hmm_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def fit(self, models):
self._encoder = LabelEncoder()
self._encoder.fit([model.label for model in models])

def predict(self, X, prior='frequency', verbose=True, return_scores=False, original_labels=True, n_jobs=1):
def predict(self, X, prior='frequency', return_scores=False, original_labels=True, verbose=True, n_jobs=1):
"""Predicts the label for an observation sequence (or multiple sequences) according to maximum likelihood or posterior scores.
Parameters
Expand All @@ -62,6 +62,12 @@ def predict(self, X, prior='frequency', verbose=True, return_scores=False, origi
Alternatively, class prior probabilities can be specified in an iterable of floats, e.g. `[0.1, 0.3, 0.6]`.
return_scores: bool
Whether to return the scores of each model on the observation sequence(s).
original_labels: bool
Whether to inverse-transform the labels to their original encoding.
verbose: bool
Whether to display a progress bar or not.
Expand All @@ -70,12 +76,6 @@ def predict(self, X, prior='frequency', verbose=True, return_scores=False, origi
are always displayed in the console, regardless of where you are running this function from
(e.g. a Jupyter notebook).
return_scores: bool
Whether to return the scores of each model on the observation sequence(s).
original_labels: bool
Whether to inverse-transform the labels to their original encoding.
n_jobs: int > 0 or -1
| The number of jobs to run in parallel.
| Setting this to -1 will use all available CPU cores.
Expand Down Expand Up @@ -105,9 +105,9 @@ def predict(self, X, prior='frequency', verbose=True, return_scores=False, origi
assert np.isclose(sum(prior), 1.), 'Class priors must form a probability distribution by summing to one'
else:
self._val.one_of(prior, ['frequency', 'uniform'], desc='prior')
self._val.boolean(verbose, desc='verbose')
self._val.boolean(return_scores, desc='return_scores')
self._val.boolean(original_labels, desc='original_labels')
self._val.boolean(verbose, desc='verbose')
self._val.restricted_integer(n_jobs, lambda x: x == -1 or x > 0, 'number of jobs', '-1 or greater than zero')

# Create look-up for prior probabilities
Expand Down
12 changes: 6 additions & 6 deletions lib/sequentia/classifiers/knn/knn_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ def fit(self, X, y):
self._X, self._y = X, self._encoder.transform(y)
self._n_features = X[0].shape[1]

def predict(self, X, verbose=True, original_labels=True, n_jobs=1):
def predict(self, X, original_labels=True, verbose=True, n_jobs=1):
"""Predicts the label for an observation sequence (or multiple sequences).
Parameters
----------
X: numpy.ndarray (float) or list of numpy.ndarray (float)
An individual observation sequence or a list of multiple observation sequences.
original_labels: bool
Whether to inverse-transform the labels to their original encoding.
verbose: bool
Whether to display a progress bar or not.
Expand All @@ -153,9 +156,6 @@ def predict(self, X, verbose=True, original_labels=True, n_jobs=1):
are always displayed in the console, regardless of where you are running this function from
(e.g. a Jupyter notebook).
original_labels: bool
Whether to inverse-transform the labels to their original encoding.
n_jobs: int > 0 or -1
| The number of jobs to run in parallel.
| Setting this to -1 will use all available CPU cores.
Expand All @@ -174,8 +174,8 @@ def predict(self, X, verbose=True, original_labels=True, n_jobs=1):
raise RuntimeError('The classifier needs to be fitted before predictions are made')

X = self._val.observation_sequences(X, allow_single=True)
self._val.boolean(verbose, desc='verbose')
self._val.boolean(original_labels, desc='original_labels')
self._val.boolean(verbose, desc='verbose')
self._val.restricted_integer(n_jobs, lambda x: x == -1 or x > 0, 'number of jobs', '-1 or greater than zero')

if isinstance(X, np.ndarray):
Expand Down Expand Up @@ -215,7 +215,7 @@ def evaluate(self, X, y, verbose=True, n_jobs=1):
"""
X, y = self._val.observation_sequences_and_labels(X, y)
self._val.boolean(verbose, desc='verbose')
predictions = self.predict(X, verbose=verbose, original_labels=False, n_jobs=n_jobs)
predictions = self.predict(X, original_labels=False, verbose=verbose, n_jobs=n_jobs)
cm = confusion_matrix(self._encoder.transform(y), predictions, labels=self._encoder.transform(self._encoder.classes_))
return np.sum(np.diag(cm)) / np.sum(cm), cm

Expand Down

0 comments on commit e7f15a0

Please sign in to comment.