相似度:1表示相等,0表示疏远
给定一组时间序列(每一行是一个),计算基于DTW的逐对相似度
from dtaidistance import dtw, similarity
s = 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]])
dis_matrix=dtw.distance_matrix(s)
dis_matrix
'''
array([[0. , 1.41421356, 2.23606798, 0. , 1.41421356,
2.23606798],
[1.41421356, 0. , 1.73205081, 1.41421356, 0. ,
1.73205081],
[2.23606798, 1.73205081, 0. , 2.23606798, 1.73205081,
0. ],
[0. , 1.41421356, 2.23606798, 0. , 1.41421356,
2.23606798],
[1.41421356, 0. , 1.73205081, 1.41421356, 0. ,
1.73205081],
[2.23606798, 1.73205081, 0. , 2.23606798, 1.73205081,
0. ]])
'''
sim_matrix=similarity.distance_to_similarity(dis_matrix)
sim_matrix
'''
array([[1. , 0.53128561, 0.36787944, 1. , 0.53128561,
0.36787944],
[0.53128561, 1. , 0.46088963, 0.53128561, 1. ,
0.46088963],
[0.36787944, 0.46088963, 1. , 0.36787944, 0.46088963,
1. ],
[1. , 0.53128561, 0.36787944, 1. , 0.53128561,
0.36787944],
[0.53128561, 1. , 0.46088963, 0.53128561, 1. ,
0.46088963],
[0.36787944, 0.46088963, 1. , 0.36787944, 0.46088963,
1. ]])
'''
相似性将高值反转为低值,低值反转为高值。如果要保持方向但将距离压缩到0到1之间,可以使用squash函数
sq_matrix=similarity.squash(dis_matrix)
sq_matrix
'''
array([[1. , 0.53128561, 0.36787944, 1. , 0.53128561,
0.36787944],
[0.53128561, 1. , 0.46088963, 0.53128561, 1. ,
0.46088963],
[0.36787944, 0.46088963, 1. , 0.36787944, 0.46088963,
1. ],
[1. , 0.53128561, 0.36787944, 1. , 0.53128561,
0.36787944],
[0.53128561, 1. , 0.46088963, 0.53128561, 1. ,
0.46088963],
[0.36787944, 0.46088963, 1. , 0.36787944, 0.46088963,
1. ]])
'''