<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 DTW simple example
Step1:  The dtw module contains a single function named dtw as well.
Step2:  Let's define two sequences
Step3:  Compute DTW
Step4:  You can plot the accumulated cost matrix and the "shortest" wrap path.
Step5:  Using another distance
You can specify your own distance used as the cost measure by the DTW. By default the L1 norm is used.
Step6:  Obviously you can also directly use those defined in numpy.
Step7:  Using subsequences
The sequences used can be of different length. DTW can also be useful to detect subsequences.
<ASSISTANT_TASK:>
Python Code:
%pylab inline
Explanation: DTW simple example
End of explanation
from dtw import dtw
Explanation: The dtw module contains a single function named dtw as well.
End of explanation
x = array([0, 0, 1, 1, 2, 4, 2, 1, 2, 0]).reshape(-1, 1)
y = array([1, 1, 1, 2, 2, 2, 2, 3, 2, 0]).reshape(-1, 1)
plot(x)
plot(y)
Explanation: Let's define two sequences:
End of explanation
dist, cost, acc, path = dtw(x, y, dist=lambda x, y: norm(x - y, ord=1))
print 'Minimum distance found:', dist
Explanation: Compute DTW
End of explanation
imshow(acc.T, origin='lower', cmap=cm.gray, interpolation='nearest')
plot(path[0], path[1], 'w')
xlim((-0.5, acc.shape[0]-0.5))
ylim((-0.5, acc.shape[1]-0.5))
Explanation: You can plot the accumulated cost matrix and the "shortest" wrap path.
End of explanation
def my_custom_norm(x, y):
    return (x * x) + (y * y)
dist, cost, acc, path = dtw(x, y, dist=my_custom_norm)
Explanation: Using another distance
You can specify your own distance used as the cost measure by the DTW. By default the L1 norm is used.
End of explanation
from numpy.linalg import norm
dist, cost, acc, path = dtw(x, y, dist=norm)
Explanation: Obviously you can also directly use those defined in numpy.
End of explanation
x = range(10)
y = [0] * 5 + x
x = array(x).reshape(-1, 1)
y = array(y).reshape(-1, 1)
dist, cost, acc, path = dtw(x, y, dist=lambda x, y: norm(x - y, ord=1))
imshow(acc.T, origin='lower', cmap=cm.gray, interpolation='nearest')
plot(path[0], path[1], 'w')
xlim((-0.5, acc.shape[0]-0.5))
ylim((-0.5, acc.shape[1]-0.5))
Explanation: Using subsequences
The sequences used can be of different length. DTW can also be useful to detect subsequences.
End of explanation
<END_TASK>