如何使用动态时间包装获取距离矩阵? [英] How to get distance matrix using dynamic time wraping?

查看:206
本文介绍了如何使用动态时间包装获取距离矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个时间序列值,如下所示.

I have 6 timeseries values as follows.

import numpy as np
series = np.array([
     [0., 0, 1, 2, 1, 0, 1, 0, 0],
     [0., 1, 2, 0, 0, 0, 0, 0, 0],
     [1., 2, 0, 0, 0, 0, 0, 1, 1],
     [0., 0, 1, 2, 1, 0, 1, 0, 0],
     [0., 1, 2, 0, 0, 0, 0, 0, 0],
     [1., 2, 0, 0, 0, 0, 0, 1, 1]])

假设,我想获得动态时间扭曲的距离矩阵以执行聚类.我为此使用了dtaidistance库.

Suppose, I want to get the distance matrix of dynamic time warping to perform a clustering. I used dtaidistance library for that as follows.

from dtaidistance import dtw
ds = dtw.distance_matrix_fast(series)

我得到的输出如下.

array([[       inf, 1.41421356, 2.23606798, 0.        , 1.41421356, 2.23606798],
       [       inf,        inf, 1.73205081, 1.41421356, 0.        , 1.73205081],
       [       inf,        inf,        inf, 2.23606798, 1.73205081, 0.        ],
       [       inf,        inf,        inf,        inf, 1.41421356, 2.23606798],
       [       inf,        inf,        inf,        inf,        inf, 1.73205081],
       [       inf,        inf,        inf,        inf,        inf,        inf]])

在我看来,我得到的输出是错误的.例如,据我了解,输出的对角线值应为0(因为它们是理想的匹配项).

It seems to me that the output I get is wrong. For instance, as I understand the diagonal values of the ouput should be 0 (since they are ideal matches).

我想知道我在哪里做错了以及如何解决.我也很高兴也可以使用其他python库获得答案.

I want to know where I am making things wrong and how to fix it. I am also happy to get answers using other python libraries too.

很高兴在需要时提供更多详细信息

I am happy to provide more details if needed

推荐答案

一切正确.根据文档 :

结果存储在矩阵表示中. 因为只有上层 需要三角矩阵.此表示会使用更多的内存,然后 必要的.

The result is stored in a matrix representation. Since only the upper triangular matrix is required this representation uses more memory then necessary.

所有对角元素均为0,下三角矩阵与在对角线镜像的上三角矩阵相同.由于所有这些值都可以从上三角矩阵中扣除,因此未在输出中显示.
您甚至可以使用compact=True参数仅从连接到一维数组的上对角矩阵中获取值.

All diagonal elements are 0 the the lower triangular matrix is the the same as the upper triagular matrix mirrored at the diagonal. As all these value can be deducted from the upper triangular matrix they aren't shown in the output.
You can even use the compact=True argument to only get the values from the upper diagonal matrix concatenated into a 1D array.

您可以将结果转换为完整的矩阵,如下所示:

You can convert the result to a full matrix like this:

ds[ds==np.inf] = 0
ds += dt.T

这篇关于如何使用动态时间包装获取距离矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆