Hagbard
Hagbard

Reputation: 3700

Time Series Classification with WEASEL

The Word ExtrAction for time SEries cLassification (WEASEL) algorithm extracts words from time series and builds features representing frequencies of each word for each time series. This algorithm is included in the pyts library. An example for visualizing the corresponding transform can be found here but I'm struggling to implement a working classifier. This code provides me with a transformed array of the training data:

from pyts.datasets import load_gunpoint
from pyts.transformation import WEASEL

# Toy dataset 
X_train, _, y_train, _ = load_gunpoint(return_X_y=True)

# WEASEL transformation
weasel = WEASEL(word_size=2, n_bins=2, window_sizes=[12, 36], sparse=False)
X_weasel = weasel.fit_transform(X_train, y_train)

But how to proceed from here? What approaches would I used to actually use these transformed data to perform a classification task? How would I feed the transformed testing data into this pipeline?

Upvotes: 0

Views: 316

Answers (1)

flyingdutchman
flyingdutchman

Reputation: 1419

WEASEL transforms the the time series to features. So you can use these features as input for any regular classifier in a following step, e.g. a SVM.

Building upon your example:

from pyts.datasets import load_gunpoint
from pyts.transformation import WEASEL
from sklearn.svm import SVC

# Toy dataset
X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)

# WEASEL transformation
weasel = WEASEL(word_size=2, n_bins=2, window_sizes=[12, 36], sparse=False)
X_weasel = weasel.fit_transform(X_train, y_train)

classifier = SVC()
classifier.fit(X_weasel, y_train)
print(classifier.score(weasel.transform(X_test), y_test))

Better than separating the transformation and classification would be to combine them in a Pipeline. Then your example would look like this:

from pyts.datasets import load_gunpoint
from pyts.transformation import WEASEL
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline

X_train, X_test, y_train, y_test = load_gunpoint(return_X_y=True)
weasel_classifier = make_pipeline(
    WEASEL(word_size=2, n_bins=2, window_sizes=[12, 36], sparse=False), SVC()
)
weasel_classifier.fit(X_train, y_train)
print(weasel_classifier.score(X_test, y_test))

Upvotes: 1

Related Questions