Package¶
These examples can also be found in the examples subdirectory.
Running COBRAS_kShape¶
import os
import numpy as np
from sklearn import metrics
from cobras_ts.cobras_kshape import COBRAS_kShape
from cobras_ts.querier.labelquerier import LabelQuerier
import random
random.seed(1245)
np.random.seed(1245)
ucr_path = '/home/toon/Downloads/UCR_TS_Archive_2015'
dataset = 'ECG200'
budget = 100
data = np.loadtxt(os.path.join(ucr_path,dataset,dataset + '_TEST'), delimiter=',')
series = data[:,1:]
labels = data[:,0]
clusterer = COBRAS_kShape(series, LabelQuerier(labels), budget)
clustering, intermediate_clusterings, runtimes, ml, cl = clusterer.cluster()
print(metrics.adjusted_rand_score(clustering.construct_cluster_labeling(),labels))
Running COBRAS_dtw¶
This uses the dtaidistance package to compute the DTW distance matrix. Note that constructing this matrix is typically the most time consuming step, and significant speedups can be achieved by using the C implementation in the dtaidistance package.
import os
import numpy as np
from dtaidistance import dtw
from sklearn import metrics
from cobras_ts.cobras_dtw import COBRAS_DTW
from cobras_ts.querier.labelquerier import LabelQuerier
ucr_path = '/home/toon/Downloads/UCR_TS_Archive_2015'
dataset = 'ECG200'
budget = 100
alpha = 0.5
window = 10
# load the data
data = np.loadtxt(os.path.join(ucr_path,dataset,dataset + '_TEST'), delimiter=',')
series = data[:,1:]
labels = data[:,0]
# construct the affinity matrix
dists = dtw.distance_matrix(series, window=int(0.01 * window * series.shape[1]))
dists[dists == np.inf] = 0
dists = dists + dists.T - np.diag(np.diag(dists))
affinities = np.exp(-dists * alpha)
# initialise cobras_dtw with the precomputed affinities
clusterer = COBRAS_DTW(affinities, LabelQuerier(labels), budget)
clustering, intermediate_clusterings, runtimes, ml, cl = clusterer.cluster()
print(metrics.adjusted_rand_score(clustering.construct_cluster_labeling(),labels))