我有两个不同长度的时间序列:
x1= [68.754, 68.753, 68.569, 68.549, 68.573, 68.985, 69.173, 69.574, 69.899, 70.262, 70.541, 70.734, 71.141, 71.459, 71.687, 72.124, 72.466, 72.712, 73.034, 73.253, 73.542, 73.844, 74.205, 74.446, 74.738, 74.952, 75.151, 75.383, 75.641, 75.742, 76.035, 76.156, 76.458, 76.575, 76.859, 76.946, 77.188, 77.22, 77.397, 77.531, 77.62, 77.838, 77.816, 77.915, 77.947, 78.093, 78.164, 78.154, 78.355, 78.36, 78.274, 78.386, 78.398, 78.395, 78.389, 78.465, 78.43, 78.571, 78.495, 78.591, 78.467, 78.509, 78.423, 78.403, 78.343, 78.252, 78.315, 78.238, 78.219, 78.058, 77.952, 77.987, 77.875, 77.814, 77.685, 77.654, 77.466, 77.406, 77.31, 77.134, 77.001, 76.988, 76.943, 76.807, 76.712, 76.522, 76.371, 76.259, 76.186, 76.01, 75.871, 75.77, 75.707, 75.533, 75.394, 75.141, 74.957, 74.652, 74.597, 74.359, 74.154, 74.041, 73.961, 73.694, 73.515, 73.369, 73.309, 73.057, 72.838, 72.65, 72.545, 72.247, 72.028, 71.84, 71.683, 71.377, 71.328, 71.127, 70.955, 70.586, 70.476, 70.209, 70.013, 69.845, 69.476, 69.408, 69.137, 68.896, 68.72, 68.342, 68.261, 67.976, 67.721, 67.528, 67.129, 67.026, 66.717, 66.466, 66.245, 66.055, 65.682, 65.339, 65.052, 64.794, 64.568, 64.396, 64.034, 63.701, 63.4, 63.149, 62.93, 62.76, 62.377, 62.025, 61.72, 61.463, 61.237, 61.043, 60.647, 60.283, 59.963, 59.675, 59.43, 58.968, 58.79, 58.41, 58.055, 57.741, 57.466, 57.225, 56.766, 56.345, 55.961, 55.613, 55.3, 55.024, 54.788, 54.34, 54.179, 53.817, 53.513, 53.25, 52.99, 52.758, 52.323, 52.132]
y1= [3.132, 3.273, 3.414, 3.598, 3.743, 3.882, 4.022, 4.148, 4.29, 4.413, 4.573, 4.712, 4.861, 5.007, 5.132, 5.274, 5.415, 5.575, 5.726, 5.868, 6.009, 6.148, 6.29, 6.435, 6.607, 6.741, 6.882, 7.024, 7.163, 7.305, 7.446, 7.59, 7.725, 7.872, 8.007, 8.15, 8.271, 8.415, 8.538, 8.684, 8.838, 8.984, 9.143, 9.275, 9.419, 9.555, 9.693, 9.819, 9.959, 10.084, 10.225, 10.352, 10.491, 10.615, 10.757, 10.89, 11.025, 11.163, 11.304, 11.443, 11.563, 11.709, 11.852, 11.99, 12.114, 12.257, 12.397, 12.538, 12.661, 12.802, 12.929, 13.07, 13.194, 13.337, 13.618, 13.761, 13.945, 14.084, 14.213, 14.351, 14.477, 14.619, 14.775, 14.913, 15.056, 15.203, 15.351, 15.494, 15.634, 15.772, 15.914, 16.039, 16.179, 16.303, 16.451, 16.775, 16.913, 17.04, 17.177, 17.305, 17.443, 17.569, 17.713, 17.857, 17.99, 18.117, 18.256, 18.399, 18.539, 18.678, 18.82, 18.964, 19.099, 19.241, 19.367, 19.507, 19.633, 19.774, 19.903, 20.04, 20.165, 20.305, 20.428, 20.57, 20.711, 20.85, 20.99, 21.132, 21.271, 21.414, 21.554, 21.678, 21.819, 21.944, 22.086, 22.21, 22.351, 22.474, 22.616, 22.744, 22.883, 23.006, 23.148, 23.273, 23.412, 23.539, 23.679, 23.819, 23.963, 24.101, 24.241, 24.383, 24.522, 24.665, 24.789, 24.93, 25.054, 25.193, 25.321, 25.46, 25.597, 25.74, 25.881, 26.022, 26.164, 26.304, 26.445, 26.586, 26.726, 26.865, 27.007, 27.131, 27.273, 27.397, 27.537, 27.664, 27.803, 27.943, 28.084, 28.225, 28.367, 28.507, 28.647, 28.787, 28.912, 29.052]
x2= [68.754, 68.754, 68.572, 68.338, 68.139, 67.889, 67.712, 67.495, 67.228, 67.087, 66.894, 66.75, 66.556, 66.413, 66.221, 66.189, 65.883, 65.633, 65.444, 65.212, 65.006, 64.868, 64.607, 64.277, 64.019, 64.033, 63.822, 63.584, 63.426, 63.127, 62.911, 62.879, 62.628, 62.465, 62.291, 62.206, 62.001, 61.791, 61.579, 61.463, 61.236, 61.017, 60.898, 60.789, 60.691, 60.395, 60.205, 60.122, 59.865, 59.719, 59.407, 59.21, 58.958, 58.825, 58.811, 58.654, 58.397, 58.228, 58.106, 57.809, 57.642, 57.529, 57.379, 57.288, 57.259, 57.066, 56.851, 56.705, 56.631, 56.401, 56.316, 56.159, 55.85, 55.779, 55.562, 55.434, 55.322, 55.17, 55.118, 54.931, 54.72, 54.675, 54.447, 54.264, 54.199, 54.013, 53.949, 53.949, 53.784, 53.797, 53.592, 53.524, 53.35, 53.318, 53.185, 53.142, 52.962, 52.882, 52.959, 52.859, 52.673, 52.572, 52.39, 52.38, 52.293, 52.384, 52.401, 52.348, 52.228, 52.291, 52.252, 52.129, 51.948, 51.925, 51.848, 51.951, 51.758, 51.737, 51.67, 51.769, 51.584, 51.572, 51.499, 51.397, 51.478, 51.525, 51.357, 51.366, 51.34, 51.296, 51.247, 51.196, 51.142, 51.09]
y2= [0.991, 1.147, 1.598, 1.769, 1.9, 2.206, 2.37, 2.528, 2.688, 2.849, 2.996, 3.154, 3.305, 3.448, 3.585, 3.87, 4.048, 4.196, 4.339, 4.619, 4.785, 4.93, 5.216, 5.367, 5.515, 5.664, 5.797, 5.932, 6.214, 6.368, 6.521, 6.663, 6.812, 6.964, 7.118, 7.432, 7.594, 7.741, 7.864, 8.014, 8.301, 8.478, 8.628, 8.776, 8.924, 9.093, 9.252, 9.524, 9.677, 9.974, 10.134, 10.295, 10.448, 10.59, 10.892, 11.057, 11.357, 11.52, 11.671, 11.836, 12.007, 12.311, 12.477, 12.639, 12.798, 13.096, 13.237, 13.384, 13.528, 13.691, 13.977, 14.129, 14.424, 14.563, 14.713, 14.992, 15.259, 15.413, 15.559, 15.843, 15.991, 16.124, 16.399, 16.539, 16.697, 16.848, 17.158, 17.323, 17.492, 17.787, 17.943, 18.111, 18.276, 18.442, 18.733, 18.888, 19.211, 19.366, 19.687, 19.833, 20.115, 20.254, 20.397, 20.532, 20.707, 20.874, 21.038, 21.213, 21.39, 21.654, 21.802, 21.965, 22.249, 22.398, 22.536, 22.681, 22.944, 23.068, 23.332, 23.481, 23.746, 24.011, 24.161, 24.468, 24.615, 24.774, 24.893, 25.144, 25.286, 25.536, 25.665, 25.91, 26.176, 26.318]
由于第一个时间序列的长度为 186,第二个为 134,因此我想重新调整这两个序列的大小,以便将它们描述为长度为 141 的时间序列。
为此,我尝试使用一种称为动态时间扭曲的方法,方法是按照以下步骤操作:
- 我将时间序列重塑为
(-1,1)
- 我使用了长度为 141 的参考系列
ref
,我计算了系列的曼哈顿距离x1
和x2
. - 使用
dtw(x1, ref, dist=manhattan_distance1)
和dtw(x2, ref, dist=manhattan_distance2)
但是,如下所示,绘制的环绕路径没有显示任何对齐(x 中的最大值不同)。
我错过了什么吗?
这是完整的代码:
import matplotlib.pyplot as plt
import select
from dtw import dtw
import numpy as np
x1= [68.754, 68.753, 68.569, 68.549, 68.573, 68.985, 69.173, 69.574, 69.899, 70.262, 70.541, 70.734, 71.141, 71.459, 71.687, 72.124, 72.466, 72.712, 73.034, 73.253, 73.542, 73.844, 74.205, 74.446, 74.738, 74.952, 75.151, 75.383, 75.641, 75.742, 76.035, 76.156, 76.458, 76.575, 76.859, 76.946, 77.188, 77.22, 77.397, 77.531, 77.62, 77.838, 77.816, 77.915, 77.947, 78.093, 78.164, 78.154, 78.355, 78.36, 78.274, 78.386, 78.398, 78.395, 78.389, 78.465, 78.43, 78.571, 78.495, 78.591, 78.467, 78.509, 78.423, 78.403, 78.343, 78.252, 78.315, 78.238, 78.219, 78.058, 77.952, 77.987, 77.875, 77.814, 77.685, 77.654, 77.466, 77.406, 77.31, 77.134, 77.001, 76.988, 76.943, 76.807, 76.712, 76.522, 76.371, 76.259, 76.186, 76.01, 75.871, 75.77, 75.707, 75.533, 75.394, 75.141, 74.957, 74.652, 74.597, 74.359, 74.154, 74.041, 73.961, 73.694, 73.515, 73.369, 73.309, 73.057, 72.838, 72.65, 72.545, 72.247, 72.028, 71.84, 71.683, 71.377, 71.328, 71.127, 70.955, 70.586, 70.476, 70.209, 70.013, 69.845, 69.476, 69.408, 69.137, 68.896, 68.72, 68.342, 68.261, 67.976, 67.721, 67.528, 67.129, 67.026, 66.717, 66.466, 66.245, 66.055, 65.682, 65.339, 65.052, 64.794, 64.568, 64.396, 64.034, 63.701, 63.4, 63.149, 62.93, 62.76, 62.377, 62.025, 61.72, 61.463, 61.237, 61.043, 60.647, 60.283, 59.963, 59.675, 59.43, 58.968, 58.79, 58.41, 58.055, 57.741, 57.466, 57.225, 56.766, 56.345, 55.961, 55.613, 55.3, 55.024, 54.788, 54.34, 54.179, 53.817, 53.513, 53.25, 52.99, 52.758, 52.323, 52.132]
y1= [3.132, 3.273, 3.414, 3.598, 3.743, 3.882, 4.022, 4.148, 4.29, 4.413, 4.573, 4.712, 4.861, 5.007, 5.132, 5.274, 5.415, 5.575, 5.726, 5.868, 6.009, 6.148, 6.29, 6.435, 6.607, 6.741, 6.882, 7.024, 7.163, 7.305, 7.446, 7.59, 7.725, 7.872, 8.007, 8.15, 8.271, 8.415, 8.538, 8.684, 8.838, 8.984, 9.143, 9.275, 9.419, 9.555, 9.693, 9.819, 9.959, 10.084, 10.225, 10.352, 10.491, 10.615, 10.757, 10.89, 11.025, 11.163, 11.304, 11.443, 11.563, 11.709, 11.852, 11.99, 12.114, 12.257, 12.397, 12.538, 12.661, 12.802, 12.929, 13.07, 13.194, 13.337, 13.618, 13.761, 13.945, 14.084, 14.213, 14.351, 14.477, 14.619, 14.775, 14.913, 15.056, 15.203, 15.351, 15.494, 15.634, 15.772, 15.914, 16.039, 16.179, 16.303, 16.451, 16.775, 16.913, 17.04, 17.177, 17.305, 17.443, 17.569, 17.713, 17.857, 17.99, 18.117, 18.256, 18.399, 18.539, 18.678, 18.82, 18.964, 19.099, 19.241, 19.367, 19.507, 19.633, 19.774, 19.903, 20.04, 20.165, 20.305, 20.428, 20.57, 20.711, 20.85, 20.99, 21.132, 21.271, 21.414, 21.554, 21.678, 21.819, 21.944, 22.086, 22.21, 22.351, 22.474, 22.616, 22.744, 22.883, 23.006, 23.148, 23.273, 23.412, 23.539, 23.679, 23.819, 23.963, 24.101, 24.241, 24.383, 24.522, 24.665, 24.789, 24.93, 25.054, 25.193, 25.321, 25.46, 25.597, 25.74, 25.881, 26.022, 26.164, 26.304, 26.445, 26.586, 26.726, 26.865, 27.007, 27.131, 27.273, 27.397, 27.537, 27.664, 27.803, 27.943, 28.084, 28.225, 28.367, 28.507, 28.647, 28.787, 28.912, 29.052]
x2= [68.754, 68.754, 68.572, 68.338, 68.139, 67.889, 67.712, 67.495, 67.228, 67.087, 66.894, 66.75, 66.556, 66.413, 66.221, 66.189, 65.883, 65.633, 65.444, 65.212, 65.006, 64.868, 64.607, 64.277, 64.019, 64.033, 63.822, 63.584, 63.426, 63.127, 62.911, 62.879, 62.628, 62.465, 62.291, 62.206, 62.001, 61.791, 61.579, 61.463, 61.236, 61.017, 60.898, 60.789, 60.691, 60.395, 60.205, 60.122, 59.865, 59.719, 59.407, 59.21, 58.958, 58.825, 58.811, 58.654, 58.397, 58.228, 58.106, 57.809, 57.642, 57.529, 57.379, 57.288, 57.259, 57.066, 56.851, 56.705, 56.631, 56.401, 56.316, 56.159, 55.85, 55.779, 55.562, 55.434, 55.322, 55.17, 55.118, 54.931, 54.72, 54.675, 54.447, 54.264, 54.199, 54.013, 53.949, 53.949, 53.784, 53.797, 53.592, 53.524, 53.35, 53.318, 53.185, 53.142, 52.962, 52.882, 52.959, 52.859, 52.673, 52.572, 52.39, 52.38, 52.293, 52.384, 52.401, 52.348, 52.228, 52.291, 52.252, 52.129, 51.948, 51.925, 51.848, 51.951, 51.758, 51.737, 51.67, 51.769, 51.584, 51.572, 51.499, 51.397, 51.478, 51.525, 51.357, 51.366, 51.34, 51.296, 51.247, 51.196, 51.142, 51.09]
y2= [0.991, 1.147, 1.598, 1.769, 1.9, 2.206, 2.37, 2.528, 2.688, 2.849, 2.996, 3.154, 3.305, 3.448, 3.585, 3.87, 4.048, 4.196, 4.339, 4.619, 4.785, 4.93, 5.216, 5.367, 5.515, 5.664, 5.797, 5.932, 6.214, 6.368, 6.521, 6.663, 6.812, 6.964, 7.118, 7.432, 7.594, 7.741, 7.864, 8.014, 8.301, 8.478, 8.628, 8.776, 8.924, 9.093, 9.252, 9.524, 9.677, 9.974, 10.134, 10.295, 10.448, 10.59, 10.892, 11.057, 11.357, 11.52, 11.671, 11.836, 12.007, 12.311, 12.477, 12.639, 12.798, 13.096, 13.237, 13.384, 13.528, 13.691, 13.977, 14.129, 14.424, 14.563, 14.713, 14.992, 15.259, 15.413, 15.559, 15.843, 15.991, 16.124, 16.399, 16.539, 16.697, 16.848, 17.158, 17.323, 17.492, 17.787, 17.943, 18.111, 18.276, 18.442, 18.733, 18.888, 19.211, 19.366, 19.687, 19.833, 20.115, 20.254, 20.397, 20.532, 20.707, 20.874, 21.038, 21.213, 21.39, 21.654, 21.802, 21.965, 22.249, 22.398, 22.536, 22.681, 22.944, 23.068, 23.332, 23.481, 23.746, 24.011, 24.161, 24.468, 24.615, 24.774, 24.893, 25.144, 25.286, 25.536, 25.665, 25.91, 26.176, 26.318]
ref= [68.754, 69.327, 69.9, 70.473, 71.046, 71.619, 72.192, 72.765, 73.338, 73.911, 74.484, 75.057, 75.63, 76.203, 76.776, 77.113, 77.466, 77.655, 77.859, 78.078, 78.057, 78.157, 78.19, 78.336, 78.517, 78.507, 78.601, 78.713, 78.627, 78.739, 78.65, 78.644, 78.72, 78.685, 78.826, 78.73, 78.703, 78.744, 78.657, 78.636, 78.636, 78.501, 78.424, 78.352, 78.334, 78.221, 78.246, 78.033, 77.952, 77.765, 77.704, 77.527, 77.473, 77.296, 77.159, 77.064, 76.874, 76.65, 76.538, 76.538, 76.292, 76.153, 76.053, 75.842, 75.769, 75.451, 75.33, 74.963, 74.909, 74.671, 74.524, 74.225, 74.014, 73.835, 73.552, 73.248, 73.081, 72.751, 72.452, 72.233, 72.044, 71.738, 71.461, 71.26, 71.088, 70.947, 70.649, 70.452, 70.053, 69.916, 69.577, 69.305, 69.101, 68.925, 68.61, 68.324, 68.069, 67.876, 67.476, 67.136, 66.886, 66.426, 66.235, 65.862, 65.575, 65.342, 64.874, 64.512, 64.179, 63.878, 63.628, 63.163, 62.993, 62.608, 62.255, 61.95, 61.692, 61.466, 61.069, 60.704, 60.383, 60.095, 59.599, 59.177, 58.787, 58.438, 58.122, 57.84, 57.343, 56.884, 56.496, 56.142, 55.824, 55.284, 55.037, 54.566, 54.131, 53.73, 53.365, 53.033, 52.732]
ref_time= [1.949, 2.114, 2.279, 2.456, 2.634, 2.793, 2.979, 3.169, 3.331, 3.504, 3.704, 3.891, 4.072, 4.265, 4.443, 4.625, 4.795, 4.98, 5.171, 5.339, 5.521, 5.699, 5.879, 6.05, 6.22, 6.405, 6.587, 6.773, 6.948, 7.28, 7.439, 7.616, 7.77, 7.941, 8.29, 8.465, 8.649, 8.838, 9.014, 9.372, 9.534, 9.724, 10.055, 10.229, 10.557, 10.725, 10.915, 11.096, 11.437, 11.602, 11.923, 12.092, 12.459, 12.631, 12.783, 12.967, 13.127, 13.3, 13.479, 13.657, 13.843, 14.029, 14.207, 14.56, 14.744, 15.07, 15.25, 15.424, 15.593, 15.767, 16.105, 16.299, 16.463, 16.821, 16.991, 17.162, 17.47, 17.651, 17.834, 18.009, 18.331, 18.512, 18.697, 18.894, 19.074, 19.237, 19.575, 19.736, 19.925, 20.088, 20.261, 20.446, 20.613, 20.957, 21.139, 21.313, 21.493, 21.659, 21.829, 22.16, 22.331, 22.501, 22.674, 22.996, 23.185, 23.366, 23.7, 23.865, 24.053, 24.224, 24.396, 24.591, 24.754, 24.924, 25.104, 25.274, 25.447, 25.777, 25.954, 26.119, 26.294, 26.471, 26.8, 26.979, 27.171, 27.334, 27.503, 27.684, 27.849, 28.197, 28.364, 28.545, 28.714, 28.887, 29.071, 29.259, 29.427, 29.598, 29.775, 29.964, 30.126]
x1= np.reshape(x1,(-1,1))
x2= np.reshape(x2,(-1,1))
ref= np.reshape(ref,(-1,1))
manhattan_distance = lambda x1, ref: np.abs(x1 - ref)
manhattan_distance2 = lambda x2, ref: np.abs(x2 - ref)
d_1, cost_matrix_1, acc_cost_matrix_1, path_1 = dtw(x1, ref, dist=manhattan_distance)
d_2, cost_matrix_2, acc_cost_matrix_2, path_2 = dtw(x2, ref, dist=manhattan_distance2)
plt.plot(path_1[0], path_1[1], 'g')
plt.plot(path_2[0], path_2[1], 'y')
plt.show()