2

I have a timeseries of shape(n_samples,window_length,num_features) for binary classification.

However I am unable to apply them in a time series problem because all the readings that I have got contains single feature dtw-knn.

I have already gone through stackoverflow link - How to use Dynamic Time warping with kNN in python

import numpy as np
from scipy.spatial import distance
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report

#toy dataset 
X = np.random.random((100,10))
y = np.random.randint(0,2, (100))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

#custom metric
def DTW(a, b):   
    an = a.size
    bn = b.size
    pointwise_distance = distance.cdist(a.reshape(-1,1),b.reshape(-1,1))
    cumdist = np.matrix(np.ones((an+1,bn+1)) * np.inf)
    cumdist[0,0] = 0

    for ai in range(an):
        for bi in range(bn):
            minimum_cost = np.min([cumdist[ai, bi+1],
                                   cumdist[ai+1, bi],
                                   cumdist[ai, bi]])
            cumdist[ai+1, bi+1] = pointwise_distance[ai,bi] + minimum_cost

    return cumdist[an, bn]

#train
parameters = {'n_neighbors':[2, 4, 8]}
clf = GridSearchCV(KNeighborsClassifier(metric=DTW), parameters, cv=3, verbose=1)
clf.fit(X_train, y_train)



#evaluate
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))

Which has dynamic time wrapping for single feature timeseries classification.

Can someone expert please help to let me know how can I apply dtw-knn for multifeature timeseries.

I have found how to find distance for multi sequence timeseries:-

from dtaidistance import dtw_ndim

series1 = np.array([[0, 0],  # first 2-dim point at t=0
                    [0, 1],  # second 2-dim point at t=1
                    [2, 1],
                    [0, 1],
                    [0, 0]], dtype=np.double)
series2 = np.array([[0, 0],
                    [2, 1],
                    [0, 1],
                    [0, .5],
                    [0, 0]], dtype=np.double)
d = dtw_ndim.distance(series1, series2)
Granth
  • 325
  • 4
  • 17
  • Does this answer your question? [Multidimensional/multivariate dynamic time warping (DTW) library/code in Python](https://stackoverflow.com/questions/37349804/multidimensional-multivariate-dynamic-time-warping-dtw-library-code-in-python) – Hagbard Dec 06 '21 at 07:39

0 Answers0