/
multivariate.py
150 lines (120 loc) · 4.94 KB
/
multivariate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Utility class for multivariate time series classification."""
# Author: Johann Faouzi <johann.faouzi@gmail.com>
# License: BSD-3-Clause
import numpy as np
from numba import njit, prange
from sklearn.base import BaseEstimator, ClassifierMixin, clone
from sklearn.preprocessing import LabelEncoder
from sklearn.utils.validation import check_is_fitted
from ..utils import check_3d_array
@njit()
def _hard_vote(y_pred, weights):
n_samples, n_features = y_pred.shape
maj = np.empty(n_samples, dtype=np.int64)
for i in prange(n_samples):
maj[i] = np.argmax(np.bincount(y_pred[i], weights))
return maj
class MultivariateClassifier(BaseEstimator, ClassifierMixin):
"""Classifier for multivariate time series.
It provides a convenient class to classify multivariate time series with
classifier that can only deal with univariate time series. The labels are
predicted in a hard voting fashion using the predictions for each feature.
Parameters
----------
estimator : estimator object or list thereof
Classifier. If one estimator is provided, it is cloned and each clone
performs prediction for one feature. If a list of estimators is
provided, each estimator performs prediction for one feature.
weights : array-like, shape = (n_classifiers,) or None (default=None)
Sequence of weights (`float` or `int`) to weight the occurrences of
predicted class labels. Uses uniform weights if None.
Attributes
----------
classes_ : array, shape = (n_classes,)
An array of class labels known to the classifier.
estimators_ : list of estimator objects
The collection of fitted classifiers.
Examples
--------
>>> from pyts.classification import BOSSVS
>>> from pyts.datasets import load_basic_motions
>>> from pyts.multivariate.classification import MultivariateClassifier
>>> X_train, X_test, y_train, y_test = load_basic_motions(return_X_y=True)
>>> clf = MultivariateClassifier(BOSSVS())
>>> clf.fit(X_train, y_train)
MultivariateClassifier(...)
>>> clf.score(X_test, y_test)
1.0
"""
def __init__(self, estimator, weights=None):
self.estimator = estimator
self.weights = weights
def fit(self, X, y):
"""Fit each classifier.
Parameters
----------
X : array-like, shape = (n_samples, n_features, n_timestamps)
Multivariate time series.
y : None or array-like, shape = (n_samples,)
Class labels.
Returns
-------
self : object
"""
X = check_3d_array(X)
_, n_features, _ = X.shape
self._check_params(n_features)
if self.weights is None:
self._weights = None
else:
self._weights = np.asarray(self.weights)
self._le = LabelEncoder().fit(y)
self.classes_ = self._le.classes_
y_ind = self._le.transform(y)
for i, clf in enumerate(self.estimators_):
clf.fit(X[:, i, :], y_ind)
return self
def predict(self, X):
"""Predict class labels using hard voting.
Parameters
----------
X : array-like, shape = (n_samples, n_features, n_timestamps)
Multivariate time series.
Returns
-------
y_pred : array, shape = (n_samples,)
Predicted class labels.
"""
X = check_3d_array(X)
check_is_fitted(self, 'estimators_')
n_samples, n_features, _ = X.shape
y_pred = np.empty((n_samples, n_features))
for i, clf in enumerate(self.estimators_):
y_pred[:, i] = clf.predict(X[:, i, :])
maj = _hard_vote(y_pred.astype('int64'), self._weights)
return self._le.inverse_transform(maj)
def _check_params(self, n_features):
"""Check parameters."""
classifier = (isinstance(self.estimator, BaseEstimator)
and hasattr(self.estimator, 'predict'))
if classifier:
self.estimators_ = [clone(self.estimator)
for _ in range(n_features)]
elif isinstance(self.estimator, list):
if len(self.estimator) != n_features:
raise ValueError(
"If 'estimator' is a list, its length must be equal to "
"the number of features ({0} != {1})"
.format(len(self.estimator), n_features)
)
for i, estimator in enumerate(self.estimator):
if not (isinstance(estimator, BaseEstimator)
and hasattr(estimator, 'predict')):
raise ValueError("Estimator {} must be a classifier."
.format(i))
self.estimators_ = self.estimator
else:
raise TypeError(
"'estimator' must be a classifier that inherits from "
"sklearn.base.BaseEstimator or a list thereof."
)