我在使用tslearn中的TimeSeriesKMeans时,将metric设置为softdtw时出现了问题,即聚类中心在可视化时非常离谱,不符实际。
但是将metric设置为欧式距离就没有问题,请教这是为什么?
def softdtw_KMeans(df_array, number_of_components):
Kmeans = TimeSeriesKMeans(n_clusters=number_of_components,
metric='softdtw', init='random').fit(df_array)
cluster_center = Kmeans.cluster_centers_
return Kmeans, cluster_center
def cluster_center_plotter(df_array, number_of_components, time_series_class, cluster_center):
global time
cluster_10th_percentile = []
cluster_90th_percentile = []
for yi in range(number_of_components):
for time in range(df_array.shape[1]):
class_temporary = df_array[time_series_class == yi]
number_1 = np.percentile(class_temporary[:, time], 10)
number_2 = np.percentile(class_temporary[:, time], 90)
cluster_10th_percentile.append(number_1)
cluster_90th_percentile.append(number_2)
cluster_10th_percentile = np.reshape(cluster_10th_percentile, (number_of_components, df_array.shape[1]))
cluster_90th_percentile = np.reshape(cluster_90th_percentile, (number_of_components, df_array.shape[1]))
plt.figure()
for yi in range(number_of_components):
plt.subplot(3, 3, yi + 1)
for xx in range(3):
plt.plot(cluster_10th_percentile[yi, :], "k-", alpha=.2)
plt.plot(cluster_90th_percentile[yi, :], "k-", alpha=.2)
plt.plot(cluster_center[yi].ravel(), "r-")
return plt.show()
# softdtw_KMeans
km, cluster_center = KMeans_clustering.softdtw_KMeans(dataset, number_of_components)
# Plotting the clusters centre
# taking the 10th and 90th percentile to display along with the clusters centre
KMeans_clustering.cluster_center_plotter(dataset, number_of_components, time_series_class, cluster_center)
上图为使用欧式距离度量,下图为softdtw距离