Skip to content

NshanPotikyan/TimeSeriesVisualization

Repository files navigation

import numpy as np 
import pandas as pd
import time
import warnings
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, accuracy_score
from dtw import DTW
from one_nn import OneNN
train = pd.read_csv('datasets/ECG200/ECG200_TRAIN.tsv', 
                   header=None,
                   sep='\t')

test = pd.read_csv('datasets/ECG200/ECG200_TEST.tsv', 
                   header=None,
                   sep='\t')

print('Training set', train.shape)
print('Testing set', test.shape)
Training set (100, 97)
Testing set (100, 97)
train.head() 
# first column is the label
# each row is time series
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
0 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 ... 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
0 -1 0.502055 0.542163 0.722383 1.428885 2.136516 2.281149 1.936274 1.468890 1.008845 0.380282 -0.296780 -0.513929 -0.255645 -0.107203 -0.287827 -0.418009 -0.319163 -0.260379 -0.350357 -0.505486 -0.710887 -0.823920 -0.899702 -1.153950 -1.229831 -1.044091 -1.202031 -1.392195 -1.130108 -1.179867 -1.649272 -1.726575 -1.608370 -1.662802 -1.650672 -1.697309 -1.838697 -1.802596 -1.780536 ... 0.683870 0.474995 0.532797 0.723550 0.664420 0.647936 0.757054 0.732072 0.620215 0.606297 0.677950 0.689084 0.598807 0.542647 0.581807 0.630631 0.664420 0.686318 0.657383 0.650889 0.721738 0.739706 0.691481 0.695229 0.750115 0.853845 0.916019 0.827750 0.880906 1.088831 0.931043 0.610298 0.638894 0.684679 0.583238 0.640522 0.708585 0.705011 0.713815 0.433765
1 1 0.147647 0.804668 0.367771 0.243894 0.026614 -0.274402 0.096731 -0.747731 -1.609777 -1.179593 -1.055926 -1.128777 -0.686039 -0.798789 -1.714279 -1.649858 -1.032699 -1.423918 -1.782433 -1.025126 -1.798909 -1.793614 -1.085478 -1.574390 -1.036059 -2.130021 -2.038621 -0.055013 -1.140195 -0.704275 0.048723 -0.475854 0.525865 0.244778 0.679932 0.928601 0.573041 1.231607 1.043324 ... 0.631120 0.694075 0.515071 0.559229 0.555712 0.469694 0.388821 0.343129 0.535598 0.900708 0.656846 0.690033 0.952596 0.547770 -0.167693 0.011532 -0.176488 -0.277706 -0.224836 -0.910723 -0.037932 0.071668 -1.048920 -0.636218 -0.386637 -0.507512 -0.716656 -0.463758 -0.460442 -0.770792 -0.533503 -0.400228 0.176084 1.111768 2.438428 2.734889 1.736054 0.036857 -1.265074 -0.208024
2 -1 0.316646 0.243199 0.370471 1.063738 1.678187 1.759558 1.697717 1.612159 1.168188 0.499957 -0.022363 -0.235318 -0.118273 -0.071027 -0.322986 -0.433562 -0.318660 -0.295655 -0.417733 -0.682480 -0.881840 -0.843320 -0.883652 -1.030018 -1.055265 -1.168637 -1.363991 -1.337611 -1.261294 -1.409782 -1.634504 -1.703238 -1.677025 -1.782628 -1.996872 -2.056372 -1.937881 -1.879412 -1.915427 ... 0.675666 0.699885 0.732561 0.771665 0.738129 0.723699 0.806876 0.878984 0.855791 0.806384 0.792501 0.803814 0.781938 0.700439 0.735960 0.864162 0.707670 0.458756 0.600146 0.825136 0.800034 0.762821 0.755443 0.653766 0.526441 0.477339 0.574946 0.698425 0.722414 0.771598 0.764229 0.610621 0.552900 0.566786 0.604002 0.777068 0.812345 0.748848 0.818042 0.539347
3 -1 1.168874 2.075901 1.760140 1.606446 1.949046 1.302842 0.459332 0.516412 0.852180 0.989227 0.821253 0.576448 0.743089 0.852970 0.307458 -0.130391 -0.062200 -0.100727 -0.232818 -0.234465 -0.214756 -0.230413 -0.227551 -0.231002 -0.463479 -0.724215 -0.787885 -0.986687 -1.224672 -1.288142 -1.364148 -1.428221 -1.501767 -1.648084 -1.705836 -1.686790 -1.705719 -1.777683 -1.869313 ... 0.405055 0.464974 0.434163 0.486807 0.439388 0.369611 0.413149 0.397895 0.318764 0.263717 0.288863 0.358244 0.326737 0.229215 0.204418 0.244386 0.262632 0.243885 0.227502 0.245785 0.232959 0.164611 0.171538 0.166690 0.099141 0.146937 0.133414 0.125294 0.202713 0.186409 0.419006 0.723888 1.323947 2.136488 1.746597 1.470220 1.893512 1.256949 0.800407 0.731540
4 1 0.648658 0.752026 2.636231 3.455716 2.118157 0.520620 -0.188627 0.780818 0.933775 0.701402 1.006133 0.395784 -0.369703 -0.738202 -0.729084 -0.906940 -0.870962 -1.060217 -1.197958 -0.981602 -1.272497 -1.287380 -1.276698 -1.539702 -1.346610 -1.535217 -1.763418 -1.688821 -1.809369 -1.863891 -1.994453 -1.889810 -1.925713 -1.833629 -1.661830 -1.342510 -1.011317 -0.672717 -0.216157 ... 0.543004 0.453148 0.485785 0.440679 0.539153 0.369571 0.377316 0.470113 0.321448 0.325513 0.251259 0.270571 0.277161 0.155171 0.120954 0.326395 0.473952 0.594736 0.698199 0.826781 0.575166 0.401196 0.349966 0.148053 -0.033206 -0.114266 0.120345 -0.220102 -0.151226 -0.077445 -0.097869 -0.136787 -0.340237 -0.089441 -0.080297 -0.192584 -0.304704 -0.454556 0.314590 0.582190

5 rows × 97 columns

X_train = train.iloc[:, 1:]
y_train = train.iloc[:, 0]
X_test = test.iloc[:, 1:]
y_test = test.iloc[:, 0]

Visualizing with DTW

print('The series have different labels')
DTW(X_train.iloc[0, :], X_train.iloc[1, :]).plot(return_plot=True)
print('The series have the same labels')
DTW(X_train.iloc[0, :], X_train.iloc[2, :]).plot(return_plot=True)
The series have different labels

png

The series have the same labels

png

Interactive plot

Press Play to see the animation

  • hover over the time series to see their values

  • use the slider, located below the main plot, to investigate areas of interest.

DTW(X_train.iloc[0, :], X_train.iloc[2, :]).plot(standard_graph=False, return_plot=True)

png

DTW from audio (from freesound.org)

warnings.filterwarnings("ignore")
audio1 = 'audio_data/28023__percy-duke__birdsong.mp3'
audio2 = 'audio_data/501589__tim-kahn__yellow-headed-blackbird.wav'
DTW(audio1, audio2, audio_files=True).plot(standard_graph=False,
                                           return_plot=True) 

png

Time Series Classification with DTW

model = OneNN()
model.fit(X_train, y_train)
start = time.perf_counter()
prediction = model.predict(X_test)
time_taken = time.perf_counter() - start
actual = y_test.values
accuracy = accuracy_score(actual, prediction)
print('Took {0:.2f} sec'.format(time_taken))
print('Confusion matrix')
print(confusion_matrix(actual, prediction))
print('Accuracy {}'.format(accuracy))
Took 145.97 sec
Confusion matrix
[[23 13]
 [ 7 57]]
Accuracy 0.8

About

Time Series Visualization using Dynamic Time Warping

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published