>>> from pyts.classification import SAXVSM
>>> from pyts.datasets import load_gunpoint
>>> from sklearn.model_selection import GridSearchCV
>>> X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
>>> clf = GridSearchCV(
... SAXVSM(),
... {'window_size': (0.3, 0.5, 0.7), 'strategy': ('uniform', 'quantile')},
... iid=False, cv=5
... )
>>> clf.fit(X_train, y_train)
GridSearchCV(...)
>>> clf.best_params_
{'strategy': 'uniform', 'window_size': 0.5}
>>> clf.score(X_test, y_test)
0.846...
Pipeline
Transformers are usually combined with a classifier to build a composite
estimator. It is possible to build such an estimator in scikit-learn using
sklearn.pipeline.Pipeline.
You can use estimators from both pyts and scikit-learn to build your own
composite estimator to classify time series.
We will illustrate this functionality with the following example. Let’s say
that we want to build a composite estimator with the following steps:
1. Standardization of each time series using
pyts.preprocessing.StandardScaler,
2. Feature extraction using
pyts.transformation.WEASEL,
3. Scaling of each feature using
sklearn.preprocessing.MinMaxScaler,
4. Classification using
sklearn.ensemble.RandomForestClassifier.
We just have to create a Pipeline instance with these estimators:
>>> clf = Pipeline([('scaler_1', StandardScaler()),
... ('boss', BOSS(sparse=False)),
... ('scaler_2', MinMaxScaler()),
... ('forest', RandomForestClassifier())])
Then we can simply:
fit on the training set by calling clf.fit(X_train, y_train),
derive predictions on the test set by calling clf.predict(X_test),
directly evaluate the performance on the test set by calling clf.score(X_test, y_test).
Here is a self-contained example:
>>> from pyts.datasets import load_pig_central_venous_pressure
>>> from pyts.preprocessing import StandardScaler
>>> from pyts.transformation import BOSS
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.pipeline import Pipeline
>>> from sklearn.preprocessing import MinMaxScaler
>>> X_train, X_test, y_train, y_test = load_pig_central_venous_pressure(return_X_y=True)
>>> clf = Pipeline([('scaler_1', StandardScaler()),
... ('boss', BOSS(sparse=False)),
... ('scaler_2', MinMaxScaler()),
... ('forest', RandomForestClassifier(random_state=42))])
>>> clf.fit(X_train, y_train)
Pipeline(...)
>>> clf.score(X_test, y_test)
0.543...