Python implementation of dtw for time series matching (2)

Introduction

In the previous article, we introduced the use of the dtw library, but it has too many restrictions, is not flexible enough, and is not convenient for drawing, so we introduce a more complex library - dtw-python. It is the python version of dtw in the R language, and the basic API is corresponding. Its advantage is that it can customize the matching mode of points, constraints, and sliding windows. At the same time, it provides convenient drawing and fast calculation (the kernel of C language), click here for the official document .

example

This time the two time series still choose the sequence in the previous article:

import numpy as np
from dtw import *
x=[2., 0., 1., 1., 2., 4., 2., 1., 2., 0.]
y=[1., 1., 2., 4., 2., 1., 2., 0.]

It should be noted that this library uses floating-point numbers by default, so here we use the float type to initialize. If an int integer is used, it will cause a calculation error, as shown in the figure below: you will find that the y-axis percentage of 1e18 appears in the upper left corner, and the black line becomes a long flat line
insert image description here
.
It took me a long time to find out that it was a problem with int and float.
Next, we will explain the calculation and drawing in detail:

ds = dtw(y, x,keep_internals=True, step_pattern=asymmetric)
ds.plot(type="twoway",offset=-2)

The first line of code is calculated to generate a dtw object, and the content can be obtained through each attribute of the object. For example, ds.distance displays the calculated shortest distance, ds.reference displays the value of the template sequence, ds.query displays the value of the query sequence, and ds.index1 and ds.index2 respectively display the corresponding sequence of each sequence, so I won’t go into details here. We only need to know here that our y is a query (query) sequence, x is a template (template) sequence , and we match y to x.
Let’s explain the parameters in the brackets again. Needless to say, x and y, for the sake of comprehensiveness, we will add other parameters:

dtw.dtw(x, y=None, dist_method='euclidean', step_pattern='symmetric2', window_type=None, window_args={}, keep_internals=False, distance_only=False, open_end=False, open_begin=False)

dist_method defines the distance method you use, the default is 'euclidean', which is the Euclidean distance.
keep_internals=True saves internal information including distance matrix and the like, generally we set it to True.
step_pattern defines the matching pattern between points, there are several types, check the official website for details.
window_type indicates global condition constraints, and there are several modes, see the official website as well.
If distance_only is set to True, the calculation will be accelerated, only the distance will be calculated, and the matching function will be ignored.
Open_end and open_begin are set to True to indicate unrestricted matching, that is, complete partial matching, and the default is global matching, which is strictly corresponding to the head and tail.
The second line of code is the drawing code, where the type indicates the mode of the graph. Generally, there are two modes : twoway and threeway . The details are explained below. offset=-2 can be understood as the degree of separation between the two lines. For the convenience of viewing, it can be set larger.
The picture generated by the above code is as follows:
insert image description here
In this picture, we can see that the head and the tail are matched strictly. The black line in the figure represents the query sequence, the blue line represents the template sequence, and the dotted line represents the point matching. We can find that in the 'asymmetric' (step_pattern) mode, although the head and the tail are matched, some points in the middle are ignored. What kind of matching form is it, we can print it out and have a look:

print(asymmetric)
asymmetric.plot()

insert image description here
insert image description here
To be honest, I don’t quite understand it either, but it doesn’t matter if I don’t understand it, we can compare it, right?

ds = dtw(y, x,keep_internals=True, step_pattern=symmetric1)
ds.plot(type="twoway",offset=-2)

Replace step_pattern with symmetric1, and draw the picture as follows:
insert image description here
We can find that the points in the picture have a one-to-one correspondence this time, and look at the pattern diagram of symmetric1:

print(symmetric1)
symmetric1.plot()

insert image description here
Obviously, this mode is more flexible than the previous asy asymmetric mode (there are three directions of up, down, left, and down), which should be the reason why the points can be one-to-one.
At the same time, let's look at another picture:

ds.plot(type="threeway",offset=-2)

insert image description here
The graph drawn in the threeway mode is very similar to the graph in our previous article. It can be seen that the vertical axis matches one by one from the second point.
Since asymmetric cannot be matched globally, can it be partially matched? In fact, it is possible, using the open_end and open_begin parameters we explained earlier:

ds = dtw(y, x,keep_internals=True, step_pattern=asymmetric,open_begin=True,open_end=False)
ds.plot(type="twoway",offset=-2)

Because the x sequence is actually the sequence of the y sequence except the beginning, we set open_begin to True, which means that we let go of the restriction of the beginning, and can make the beginning match arbitrarily. In this case, we can find that it has completed a partial match
insert image description here
.

Summarize

This article mainly introduces the drawing function of the dtw-python library. The definition of the window and the definition of the distance function have not been covered yet. I hope readers can explore and supplement it by themselves. Except for the major bug of int type calculation error, the library is relatively complete. So far, the exploration of dtw has temporarily come to an end, and readers are welcome to leave a message for discussion.

1.https://dynamictimewarping.github.io/py-api/html/api/dtw.dtw.html#dtw.dtw
2.https://dynamictimewarping.github.io/py-api/html/api/dtw.StepPattern.html#dtw.StepPattern
3.https://dynamictimewarping.github.io/
4.https://www.jstatsoft.org/article/view/v031i07

Guess you like

Origin blog.csdn.net/weixin_43945848/article/details/120826885