作家
登录

如何为时间序列数据优化K-均值聚类速度?

作者: 来源: 2017-11-13 14:02:07 阅读 我要评论

"early finish!"
  •           break 
  •        last_centroids = centroids 
  •    return centroids 
    1. t1 = time.time() 
    2. centroids = k_means(signals, 100, 100) 
    3. t2 = time.time() 
    4. print("Took {} seconds".format(t2 - t1)) 

    耗时 3.5 分钟多一点。很不错!但我们还想完成得更快。

    k-means++ 实现

    我们的下一?实现应用了 k-means++ 算法。这个算法的目标是选择更优的初始质心。让我们看看这种优化办法有没有效……

    1. def init_centroids(data, num_clust): 
    2.    centroids = np.zeros([num_clust, data.shape[1]]) 
    3.    centroids[0,:] = data[np.random.randint(0, data.shape[0], 1)] 
    4.    for i in range(1, num_clust): 
    5.        D2 = np.min([np.linalg.norm(data - c, axis = 1)**2 for c in centroids[0:i, :]], axis = 0) 
    6.        probs = D2/D2.sum() 
    7.        cumprobs = probs.cumsum() 
    8.        ind = np.where(cumprobs >= np.random.random())[0][0] 
    9.        centroids[i, :] = np.expand_dims(data[ind], axis = 0) 
    10.    return centroids 
    11. def k_means(data, num_clust, num_iter): 
    12.    centroids = init_centroids(data, num_clust) 
    13.    last_centroids = centroids 
    14.    for n in range(num_iter): 
    15.        closest = closest_centroids(data, centroids) 
    16.        centroids = move_centroids(data, closest, centroids) 
    17.        if not np.any(last_centroids != centroids): 
    18.            print("Early finish!"
    19.            break 
    20.        last_centroids = centroids 
    21.    return centroids 
    1. t1 = time.time() 
    2. centroids = k_means(signals, 100, 100) 
    3. t2 = time.time() 
    4. print("Took {} seconds".format(t2 - t1)) 

    并行实现

    1. import ipyparallel as ipp 

        推荐阅读

        Git的4个阶段的撤销更改命令分析

      Tech Neo技巧沙龙 | 11月25号,九州云/ZStack与您一路商量云时代收集界线治理实践 固然 git 出生距今已有 12 年之久,网上各类关于 git 的介绍文┞仿数不堪数,然则依然有很多人(包含我本>>>详细阅读


      本文标题:如何为时间序列数据优化K-均值聚类速度?

      地址:http://www.17bianji.com/lsqh/38787.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)