The world of time series analysis can be complex, and finding the right Python library for Dynamic Time Warping can be even more so.

That’s where this tutorial comes in!

My goal is to provide you with an easy-to-follow guide that will help you understand the various options available and make the right choice for your project.

Whether you are a beginner or an expert, you will find valuable insights here.

Just make sure you have some basic Python knowledge, and you’ll be ready to measure time series similarity with Dynamic Time Warping in no time!

Which DTW Library Is Faster?

Before we dive in, I ran each of the libraries 100 times and calculated the mean execution time and standard deviation for each when computing the DTW distance between two time series of length 100 and 200, respectively.

Mean Execution Time Standard Deviation
dtaidistance distance_fast 0.000110865 0.000315396
TSLearn 0.000214593 0.000419177
PyTS 0.00031039 0.000463163
DTW-Python 0.00144317 0.00049049
dtaidistance 0.0329155 0.00171103
FastDTW 0.0587402 0.00245806

Surprisingly, FastDTW doesn’t perform as well as the other libraries, the distance_fast function from dtaidistance, and TSLearn took the top spots.

This doesn’t mean that FastDTW is a bad choice, though. Always try it in a sample of your data to see if it works well for your use case.

These are the results for 20 runs with time series of length 1000 and 2000:

Mean Execution Time Standard Deviation
dtaidistance distance_fast 0.00970404 0.000795062
TSLearn 0.0164027 0.0011904
PyTS 0.0421196 0.0043183
DTW-Python 0.0878649 0.00643095
FastDTW 0.67578 0.0348886
dtaidistance 3.75402 0.12155

dtaidistance

The dtaidistance library is a great choice when you need to compute DTW distances with window constraints and lower bounds.

This library is optimized for fast computation using Cython and Numba, making it suitable for time-sensitive applications.

It also includes other distance measures like the Longest Common Subsequence (LCSS) and Edit Distance on Real sequence (EDR).

dtaidistance Code Example

To use the dtaidistance library, you need to install it first:

pip install dtaidistance

Then, you can import the dtw function from the library and compute the DTW distance between two time series:

from dtaidistance import dtw
import numpy as np

a = np.random.random(100)
b = np.random.random(200)

distance = dtw.distance(a, b)
print(f"DTW Distance: {distance}")

If this function is too slow, you can use the distance_fast function, which is optimized for speed.

distance = dtw.distance_fast(a, b)
print(f"DTW Distance: {distance}")

TSLearn (Multivariate DTW)

TSLearn is a versatile Python library that offers an extensive set of tools for time series analysis, including Dynamic Time Warping (DTW) with native support for multivariate time series.

When choosing a library for DTW calculations, you should pick TSLearn if you are working with multivariate time series data.

TSLearn makes the process easier by automatically handling multiple variables, so you don’t need to create a custom distance function.

TSLearn Multivariate DTW Code Example

To use the tslearn library, you need to install it first:

pip install tslearn

Then, you can import the DTW function from the library and compute the DTW distance between two multivariate time series:

from tslearn.metrics import dtw as ts_dtw
import numpy as np

a = np.random.random((100, 2))
b = np.random.random((200, 2))

distance = ts_dtw(a, b)
print(f"DTW Distance: {distance}")

PyTS DTW

PyTS is a comprehensive library for time series analysis and classification that offers a range of algorithms, including Dynamic Time Warping.

It stands out among other DTW libraries due to its extensive set of tools, making it an excellent choice when you need to perform various tasks in your time series project.

The library is user-friendly and allows you to adjust the settings for DTW to fit your specific needs.

In addition to supporting multivariate time series and various distance measures, PyTS provides numerous preprocessing techniques, feature extraction methods, and classification algorithms, allowing you to create end-to-end time series workflows using a single library.

PyTS DTW Code Example

Install the pyts library using pip:

pip install pyts

Then, you can import the dtw function from the library:

from pyts.metrics import dtw
import numpy as np

a = np.random.random((100, 2))
b = np.random.random((200, 2))

distance = dtw(a, b)

print(f"DTW Distance: {distance}")

DTW-Python

If you’re looking for a comprehensive implementation of DTW and its variants, along with visualization tools for alignment and warping paths, dtw-python is an excellent choice.

This library also supports multidimensional time series, making it versatile for various applications.

Although it is slower than some approximate methods like fastdtw, it provides a more accurate representation of the classic DTW algorithm.

Additionally, it is well-documented and comes with numerous examples to help you get started.

DTW-Python Code Example

Install the dtw-python library using pip:

pip install dtw-python

Then, you can import the dtw function from the library:

from dtw import dtw
import numpy as np

a = np.random.random((100, 2))
b = np.random.random((200, 2))

alignment = dtw(a, b)

print(f"DTW Distance: {alignment.distance}")

Here, a and b simulate two multivariate time series, each with two dimensions and of lengths 100 and 200, respectively.

DTW-Python allows you to easily customize the distance function, window constraints, and other parameters to fit your specific needs.

To learn more about the different options, consult the official documentation.

FastDTW

The fastdtw library is an implementation of the FastDTW algorithm, which is much more efficient than the standard Dynamic Time Warping algorithm because it only fills in the cells in the cost matrix that are in the neighborhood of the path projected from the previous resolution.

This makes the FastDTW algorithm an O(N) algorithm, meaning that its complexity grows linearly with the size of the input time series.

In contrast, the standard DTW algorithm is an O(N^2) algorithm, meaning that its complexity grows quadratically with the size of the input time series.

Although FastDTW is not guaranteed to always find the optimal warp path, it usually finds a warp path that is very close to optimal.

However, based on the speed comparison results, the dtaidistance library’s distance_fast function is faster than FastDTW.

FastDTW Code Example

To use the fastdtw library, you need to install it first:

pip install fastdtw

Then, you can import the fastdtw function from the library and compute the DTW distance between two time series:

from fastdtw import fastdtw
from scipy.spatial.distance import euclidean
import numpy as np

a = np.random.random(100)
b = np.random.random(200)

distance, path = fastdtw(a, b, dist=euclidean)
print(f"DTW Distance: {distance}")

Here a and b simulate two time series of length 100 and 200, respectively.

You can specify the distance function to use for computing the distance between two points in the time series.

By default, the absolute difference between two points is used.