使用dtaidistance实现dtw算法(二)
2021/10/27 11:12:11
本文主要是介绍使用dtaidistance实现dtw算法(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用dtaidistance实现dtw算法(二)
1、实现两两序列之间的距离计算
# DTW Distance Measures Between Set of Series 查看两两序列之间的距离 from dtaidistance import dtw import numpy as np # The distance_matrix method expects a list of lists/arrays: 数据格式 series = [ np.array([0, 0, 1, 2, 1, 0, 1, 0, 0], dtype=np.double), np.array([0.0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0]), np.array([0.0, 0, 1, 2, 1, 0, 0, 0])]
dtw.distance_matrix_fast(series)
array([[0. , 1.41421356, 1. ],
[1.41421356, 0. , 1. ],
[1. , 1. , 0. ]])
两序列之间的距离矩阵,和相关系数矩阵的排列方式是一样的,(1,1)0第一个、第一个序列之间的距离,(2,1)1.41421356第一个、第二个序列之间的距离,(3,1)1第一个、第三个序列之间的距离,(3,2)1第二个、第三个序列之间的距离。
# or a matrix (in case all series have the same length): numpy格式 series = np.matrix([ [0.0, 0, 1, 2, 1, 0, 1, 0, 0], [0.0, 1, 2, 0, 0, 0, 0, 0, 0], [0.0, 0, 1, 2, 1, 0, 0, 0, 0], [0.0, 0, 1, 2, 1, 0, 1, 0, 1] # 多加了一行 ]) dtw.distance_matrix_fast(series)
array([[0. , 1.41421356, 1. , 1. ],
[1.41421356, 0. , 1. , 1.73205081],
[1. , 1. , 0. , 1.41421356],
[1. , 1.73205081, 1.41421356, 0. ]])
2、两两序列之间的对应
# 展示两两之间的对应关系 from dtaidistance import dtw from dtaidistance import dtw_visualisation as dtwvis import numpy as np s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0, 0]) path = dtw.warping_path(s1, s2) dtwvis.plot_warping(s1, s2, path, filename="warp.png")
3、最佳路径
distance, paths = dtw.warping_paths(s1, s2 #, window=25 #, psi=2 ) print(distance) best_path = dtw.best_path(paths)# 最短路径 dtwvis.plot_warpingpaths(s1, s2, paths, best_path)# 制图
这篇关于使用dtaidistance实现dtw算法(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-30uniAPP 实现全屏左右滚动滚动的效果-icode9专业技术文章分享
- 2024-06-30如何在本地使用授权或插件-icode9专业技术文章分享
- 2024-06-30伪静态规则配置方法汇总-icode9专业技术文章分享
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding