美文网首页
时序聚类

时序聚类

作者: venuslf | 来源:发表于2019-09-30 16:30 被阅读0次

    方法使用参见官方文档:
    https://tslearn.readthedocs.io/en/latest/gen_modules/tslearn.clustering.html#module-tslearn.clustering

    from tslearn.clustering import GlobalAlignmentKernelKMeans, TimeSeriesKMeans, KShape
    from tslearn.metrics import sigma_gak
    from tslearn.preprocessing import to_time_series_dataset
    from tslearn.generators import random_walks
    
    def cluster_method(x, method, max_iter, n_cluster=3, seed=100):
        if method == 'KShape':
            x = TimeSeriesScalerMeanVariance(mu=0., std=1.).fit_transform(x)  #数据标准化
            model = KShape(n_clusters=n_cluster, max_iter=max_iter, n_init=1, random_state=seed).fit(x)
        elif method == 'KMeans_euclidean':
            model = TimeSeriesKMeans(n_clusters=n_cluster, metric="euclidean", max_iter=max_iter,
                          random_state=seed).fit(x)
        elif method == 'KMeans_dtw':
            model = TimeSeriesKMeans(n_clusters=n_cluster, metric="dtw", max_iter=max_iter, 
                                     max_iter_barycenter=100,random_state=seed).fit(x)
        elif method == 'KMeans_softdtw':
            model = TimeSeriesKMeans(n_clusters=n_cluster, metric="softdtw", max_iter=max_iter, 
                                     max_iter_barycenter=100,metric_params={"gamma": .5}, random_state=seed).fit(x)
        elif method == 'KernelKMeans':
            model = GlobalAlignmentKernelKMeans(n_clusters=n_cluster,
                                         sigma=sigma_gak(input_data),
                                         n_init=20,
                                         verbose=False,
                                         random_state=seed).fit(x)        
        return model
    
    def input_data_process(method):
        if method in ('KMeans_euclidean','KShape'):  # 要求时序等长
            x = random_walks(n_ts=50, sz=32, d=1)
        else:  # 其他方法序列可不等长
            x = to_time_series_dataset([[1, 2, 3, 4],[1, 2, 3],[2, 5, 6, 7, 8, 9]])  # to_time_series_dataset可将list转换成时序聚类模型需要的输入格式
        return x
    
    if __name__ == '__main__':
        method = 'KMeans_euclidean'
        input_data = input_data_process(method=method)
        model = cluster_method(x=input_data, method=method, n_cluster=2, max_iter=100, seed=100)
        pred = model.predict(input_data)
        pred
    
    

    相关文章

      网友评论

          本文标题:时序聚类

          本文链接:https://www.haomeiwen.com/subject/pqyructx.html